# @devts/nestjs-auth

> Oauth2 Auth Module to NestJS

Latest version **1.3.14** (published 2023-04-18) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @devts/nestjs-auth
pnpm add @devts/nestjs-auth
yarn add @devts/nestjs-auth
bun add @devts/nestjs-auth
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.3.14 |
| Published | 2023-04-18 |
| First published | 2023-01-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 93.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | rojiwon0325, rojiwon |
| Keywords | nestjs-auth, nest-auth, auth, Oauth2, passport, authentication |

## Links

- npm: https://www.npmjs.com/package/@devts/nestjs-auth
- Repository: git@github.com-industriously:industriously/nestjs-auth
- Issues: https://github.com/industriously/nestjs-auth/issues
- npm.io page: https://npm.io/package/@devts/nestjs-auth

## Dependencies (1)

- [jsonwebtoken](https://npm.io/package/jsonwebtoken.md) ^9.0.0

## Alternatives

- [@clerk/clerk-expo](https://npm.io/package/@clerk/clerk-expo.md) — 133.6K weekly downloads
- [@pothos/plugin-authz](https://npm.io/package/@pothos/plugin-authz.md) — 12.4K weekly downloads
- [@bounded-sh/client](https://npm.io/package/@bounded-sh/client.md) — 3.2K weekly downloads
- [@luigi-project/plugin-auth-oauth2](https://npm.io/package/@luigi-project/plugin-auth-oauth2.md) — 2.3K weekly downloads
- [@nocobase/plugin-verification](https://npm.io/package/@nocobase/plugin-verification.md) — 2.0K weekly downloads

## Recent versions

- 1.3.14 (latest) — 2023-04-18
- 1.3.13 — 2023-04-13
- 1.3.12 — 2023-04-13
- 1.3.11 — 2023-04-13
- 1.3.10 — 2023-04-11
- 1.3.9 — 2023-04-04
- 1.3.8 — 2023-02-28
- 1.3.7 — 2023-02-05
- 1.3.6 — 2023-01-30
- 1.3.5 — 2023-01-29
- 1.3.4 — 2023-01-26
- 1.3.3 — 2023-01-24
- 1.3.2 — 2023-01-24
- 1.3.1 — 2023-01-24
- 1.3.0 — 2023-01-22
- … 12 more at https://npm.io/package/@devts/nestjs-auth/versions

## README

# nestjs-auth

[![npm version](https://img.shields.io/npm/v/@devts%2Fnestjs-auth.svg)](https://www.npmjs.com/package/@devts/nestjs-auth)
[![Downloads](https://img.shields.io/npm/dm/@devts%2Fnestjs-auth.svg?logo=npm)](https://www.npmjs.com/package/@devts/nestjs-auth)
[![type-coverage](https://img.shields.io/badge/dynamic/json.svg?label=type%20coverage&color=brightgreen&prefix=%E2%89%A5&suffix=%&query=$.typeCoverage.atLeast&uri=https%3A%2F%2Fraw.githubusercontent.com%2Findustriously%2Fnestjs-auth%2Fmain%2Fpackage.json)](https://github.com/industriously/nestjs-auth)

- A way to apply Oauth2 Auth module to nestjs

```sh
  more type-safe than passport
  install only once
```

<details>
  <summary>Table of Contents</summary>
  <ol>
    <li><a href="#installation">Installation</a></li>
    <li><a href="#example">example</a></li>
  </ol>
</details>

<!-- INSTALLATION -->

## Installation

```sh
npm i @devts/nestjs-auth
```

<!-- EXAMPLE -->

## Example

```typescript
import { HttpException, Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Google, StrategyException } from '@devts/nestjs-auth';

interface GoogleProfile {
  username: string;
  email: string;
}

@Injectable()
export class GoogleStrategy extends Google.AbstractStrategy<
  'user',
  "emails" | "profile"
  GoogleProfile
> {
  constructor(configService: ConfigService) {
    super({
      client_id: configService.get('CLIENT_ID'),
      client_secret: configService.get('CLIENT_SECRET'),
      redirect_uri: configService.get('OAUTH_CALLBACK'),
      access_type: "offline",
      prompt: "consent",
      scope: ['email', 'profile'],
      key: 'user',
    });
  }

  protected throw({
    statusCode = 401,
    message = ''
  }:StrategyException): never {
    throw new HttpException(statusCode, message);
  }

  validate(
    identity: Google.IdToken<'email' | 'profile'>,
    credentials: Google.Credentials,
  ): boolean {
    return true;
  }

  transform(
    identity: Google.IdToken<'email' | 'profile'>,
  ): GoogleProfile {
    const { name, email } = identity;
    return { username: name, emails };
  }
}

// in module
@Module({
  providers: [
    {
      provide: 'GoogleStrategy',
      useClass: GoogleStrategy,
    },
  ],
})
export class AppModule {}
```

```typescript
import { AuthGuard } from '@devts/nestjs-auth';
import { Req } from '@nestjs/common';
import type { Request } from 'express';

@Controller('auth')
export class AuthController {
  // Inject decorator get "GoogleStrategy" token
  @Get('sign-in')
  @UseGuards(AuthGuard('GoogleStrategy'))
  signIn() {
    return;
  }

  @Get('YOUR REDIRECT PATH')
  @UseGuards(AuthGuard('GoogleStrategy'))
  callback(@Req() req: Request) {
    return (req as any).user; // you can get data from request[key]
  }
}
```

---
_Source: https://npm.io/package/@devts/nestjs-auth · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
