Skip to content

Commit

Permalink
passport jwt
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkeksz committed Oct 25, 2023
1 parent 7d03693 commit 0d92ef6
Show file tree
Hide file tree
Showing 34 changed files with 477 additions and 278 deletions.
106 changes: 106 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@
"@nestjs/devtools-integration": "^0.1.5",
"@nestjs/jwt": "^10.1.1",
"@nestjs/mapped-types": "*",
"@nestjs/passport": "^10.0.2",
"@nestjs/platform-express": "^10.0.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"joi": "^17.11.0",
"nest-winston": "^1.9.4",
"passport": "^0.6.0",
"passport-jwt": "^4.0.1",
"passport-local": "^1.0.0",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1",
"winston": "^3.11.0"
Expand All @@ -48,6 +52,8 @@
"@types/express": "^4.17.17",
"@types/jest": "^29.5.2",
"@types/node": "^20.3.1",
"@types/passport-jwt": "^3.0.11",
"@types/passport-local": "^1.0.37",
"@types/supertest": "^2.0.12",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
Expand Down
4 changes: 2 additions & 2 deletions src/app.controller.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {Controller, Get} from '@nestjs/common'
import {AppService} from './app.service'
import {SkipAuth} from './auth/decorators/skip-auth.decorator'
import {SkipJwtAuth} from './auth/decorators/skip-jwt-auth.decorator'

@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}

@SkipAuth()
@SkipJwtAuth()
@Get()
getHello(): Promise<string> {
return this.appService.getHello()
Expand Down
4 changes: 3 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ import {TimeoutInterceptor} from './interceptors/timeout.interceptor'
import {NODE_ENV} from './config/environment/environment.config.constants'
import {configModule, configProviders} from './config'
import {AuthModule} from './auth/auth.module'
import {ProfileModule} from './profile/profile.module'

@Module({
imports: [
configModule,
UsersModule,
AuthModule,
...(process.env[NODE_ENV.name] === NODE_ENV.options.DEVELOPMENT
? [DevtoolsModule.register({http: true})]
: []),
AuthModule,
ProfileModule,
],
controllers: [AppController],
providers: [
Expand Down
31 changes: 8 additions & 23 deletions src/auth/auth.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,31 @@ import {Test, TestingModule} from '@nestjs/testing'
import {AuthController} from './auth.controller'
import {AuthModule} from './auth.module'
import {AuthService} from './auth.service'
import {AuthUser} from './entities/auth-user.entity'
import {configModule} from '../config'

describe('AuthController', () => {
let authController: AuthController
let authService: AuthService

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [AuthModule],
imports: [AuthModule, configModule],
}).compile()

authController = module.get(AuthController)
authService = module.get(AuthService)
})

describe('signIn', () => {
describe('login', () => {
it('should return a token', async () => {
jest.spyOn(authService, 'signIn').mockImplementation(async () => {
return {
access_token: 'test',
}
})

const result = {
const expected = {
access_token: 'test',
}
expect(await authController.signIn({email: '[email protected]', password: '123'})).toStrictEqual(
result,
)
})
})

describe('getProfile', () => {
it('should return a user', async () => {
const request = {
user: {sub: 1, email: '[email protected]', iat: 123, exp: 123},
}
expect(authController.getProfile(request as Request & {user: AuthUser})).toStrictEqual(
request.user,
)
jest.spyOn(authService, 'login').mockImplementation(() => expected)

const req = {user: {id: 1}} as Request & {user: {id: number}}
expect(authController.login(req)).toStrictEqual(expected)
})
})
})
20 changes: 8 additions & 12 deletions src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
import {Body, Controller, Get, HttpCode, HttpStatus, Post, Request} from '@nestjs/common'
import {Controller, HttpCode, HttpStatus, Post, Request, UseGuards} from '@nestjs/common'
import {User} from '../users/entities/user.entity'
import {LocalAuthGuard} from './guards/local-auth.guard'
import {AuthService} from './auth.service'
import {SignInDto} from './dto/sign-in.dto'
import {AuthUser} from './entities/auth-user.entity'
import {SkipAuth} from './decorators/skip-auth.decorator'
import {SkipJwtAuth} from './decorators/skip-jwt-auth.decorator'

@Controller('auth')
export class AuthController {
constructor(private authService: AuthService) {}

@SkipAuth()
@SkipJwtAuth()
@UseGuards(LocalAuthGuard)
@HttpCode(HttpStatus.OK)
@Post('login')
signIn(@Body() signInDto: SignInDto) {
return this.authService.signIn(signInDto.email, signInDto.password)
}

@Get('profile')
getProfile(@Request() request: Request & {user: AuthUser}) {
return request.user
login(@Request() req: Request & {user: {id: User['id']}}) {
return this.authService.login(req.user.id)
}
}
43 changes: 0 additions & 43 deletions src/auth/auth.guard.ts

This file was deleted.

18 changes: 12 additions & 6 deletions src/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@ import {Module} from '@nestjs/common'
import {AuthController} from './auth.controller'
import {AuthService} from './auth.service'
import {UsersModule} from '../users/users.module'
import {JwtModule} from '@nestjs/jwt'
import {ConfigService} from '@nestjs/config'
import {ConfigModule} from '@nestjs/config/dist/config.module'
import {ConfigModule, ConfigService} from '@nestjs/config'
import {AuthConfigService} from '../config/auth/auth.config.service'
import {PassportModule} from '@nestjs/passport'
import {LocalStrategy} from './strategies/local.strategy'
import {JwtModule} from '@nestjs/jwt'
import {JwtStrategy} from './strategies/jwt.strategy'
import {APP_GUARD} from '@nestjs/core'
import {AuthGuard} from './auth.guard'
import {JwtAuthGuard} from './guards/jwt-auth.guard'

@Module({
imports: [
UsersModule,
PassportModule,
JwtModule.registerAsync({
global: true,
imports: [ConfigModule],
Expand All @@ -25,11 +28,14 @@ import {AuthGuard} from './auth.guard'
controllers: [AuthController],
providers: [
AuthService,
AuthConfigService,
LocalStrategy,
ConfigService,
AuthConfigService,
JwtStrategy,
AuthService,
{
provide: APP_GUARD,
useClass: AuthGuard,
useClass: JwtAuthGuard,
},
],
})
Expand Down
Loading

0 comments on commit 0d92ef6

Please sign in to comment.