-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add admin login auth flow (#112)
- Loading branch information
Showing
14 changed files
with
217 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { AuthService } from './auth.service'; | ||
import { PassportModule } from '@nestjs/passport'; | ||
import { ConfigModule } from '@nestjs/config'; | ||
import { LocalStrategy } from './strategies/local.strategy'; | ||
import { AuthResolver } from './auth.resolver'; | ||
|
||
@Module({ | ||
imports: [PassportModule, ConfigModule], | ||
providers: [AuthResolver, AuthService, LocalStrategy], | ||
exports: [AuthService], | ||
}) | ||
export class AuthModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { AuthResolver } from './auth.resolver'; | ||
|
||
describe('AuthResolver', () => { | ||
let resolver: AuthResolver; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [AuthResolver], | ||
}).compile(); | ||
|
||
resolver = module.get<AuthResolver>(AuthResolver); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(resolver).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { AuthService } from './auth.service'; | ||
import { Args, Mutation, Resolver } from '@nestjs/graphql'; | ||
import { LoginResponse } from './dto/login-response'; | ||
import { UseGuards } from '@nestjs/common'; | ||
import { GqlLocalAuthGuard } from './guards/gql-local.guard'; | ||
import { LoginUserInput } from './dto/login-user.input'; | ||
|
||
@Resolver() | ||
export class AuthResolver { | ||
constructor(private readonly authService: AuthService) {} | ||
|
||
@Mutation(() => LoginResponse) | ||
@UseGuards(GqlLocalAuthGuard) | ||
async login(@Args('loginUserInput') loginUserInput: LoginUserInput): Promise<LoginResponse> { | ||
return await this.authService.login(loginUserInput); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { AuthService } from './auth.service'; | ||
|
||
describe('AuthService', () => { | ||
let service: AuthService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [AuthService], | ||
}).compile(); | ||
|
||
service = module.get<AuthService>(AuthService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { LoginResponse } from './dto/login-response'; | ||
|
||
@Injectable() | ||
export class AuthService { | ||
constructor(private configService: ConfigService) {} | ||
|
||
async validateUser(username: string, password: string): Promise<string> { | ||
const adminUsername = this.configService.getOrThrow<string>('ADMIN_USERNAME'); | ||
const adminPassword = this.configService.getOrThrow<string>('ADMIN_PASSWORD'); | ||
|
||
if (username === adminUsername && password === adminPassword) { | ||
return username; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
async login(loginUserInput): Promise<LoginResponse> { | ||
const token = this.configService.getOrThrow<string>('SERVER_API_KEY'); | ||
return { | ||
token, | ||
user: loginUserInput.username, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Field, ObjectType } from '@nestjs/graphql'; | ||
|
||
@ObjectType() | ||
export class LoginResponse { | ||
@Field() | ||
token: string; | ||
|
||
@Field() | ||
user: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { InputType, Field } from '@nestjs/graphql'; | ||
import { IsNotEmpty } from 'class-validator'; | ||
|
||
@InputType() | ||
export class LoginUserInput { | ||
@Field() | ||
@IsNotEmpty() | ||
username: string; | ||
|
||
@Field() | ||
@IsNotEmpty() | ||
password: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Injectable, ExecutionContext } from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
import { GqlExecutionContext } from '@nestjs/graphql'; | ||
import { Request } from 'express'; | ||
|
||
@Injectable() | ||
export class GqlLocalAuthGuard extends AuthGuard('local') { | ||
getRequest(context: ExecutionContext): Request { | ||
const ctx = GqlExecutionContext.create(context); | ||
const request = ctx.getContext().req; | ||
request.body = ctx.getArgs().loginUserInput; | ||
return request; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Strategy } from 'passport-local'; | ||
import { PassportStrategy } from '@nestjs/passport'; | ||
import { Injectable, UnauthorizedException } from '@nestjs/common'; | ||
import { AuthService } from '../auth.service'; | ||
|
||
@Injectable() | ||
export class LocalStrategy extends PassportStrategy(Strategy) { | ||
constructor(private authService: AuthService) { | ||
super({ | ||
usernameField: 'username', | ||
passwordField: 'password', | ||
}); | ||
} | ||
|
||
async validate(username: string, password: string): Promise<string> { | ||
const user = await this.authService.validateUser(username, password); | ||
|
||
if (!user) { | ||
throw new UnauthorizedException('Invalid username or password'); | ||
} | ||
|
||
return user; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters