-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from panoratech/feat/passport-auth
feat: api auth
- Loading branch information
Showing
16 changed files
with
336 additions
and
11 deletions.
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
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 |
---|---|---|
@@ -1,12 +1,31 @@ | ||
import { Controller, Get } from '@nestjs/common'; | ||
import { Controller, Get, Request, Post, UseGuards } from '@nestjs/common'; | ||
import { AppService } from './app.service'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
import { LocalAuthGuard } from './auth/guards/local-auth.guard'; | ||
import { AuthService } from './auth/auth.service'; | ||
import { JwtAuthGuard } from './auth/guards/jwt-auth.guard'; | ||
|
||
@Controller() | ||
export class AppController { | ||
constructor(private readonly appService: AppService) {} | ||
constructor( | ||
private readonly appService: AppService, | ||
private authService: AuthService, | ||
) {} | ||
|
||
@Get() | ||
getHello(): string { | ||
return this.appService.getHello(); | ||
} | ||
|
||
@UseGuards(JwtAuthGuard) | ||
@Get('profile') | ||
getProfile(@Request() req) { | ||
return req.user; | ||
} | ||
|
||
@UseGuards(LocalAuthGuard) | ||
@Post('auth/login') | ||
async login(@Request() req) { | ||
return this.authService.login(req.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
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,29 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { AuthService } from './auth.service'; | ||
import { UsersModule } from './users/users.module'; | ||
import { LocalStrategy } from './strategies/local.strategy'; | ||
import { PassportModule } from '@nestjs/passport'; | ||
import { JwtModule, JwtService } from '@nestjs/jwt'; | ||
import { jwtConstants } from './utils/constants'; | ||
import { JwtStrategy } from './strategies/jwt.strategy'; | ||
import { UsersService } from './users/users.service'; | ||
|
||
@Module({ | ||
providers: [ | ||
AuthService, | ||
LocalStrategy, | ||
JwtStrategy, | ||
UsersService, | ||
JwtService, | ||
], | ||
imports: [ | ||
UsersModule, | ||
PassportModule, | ||
JwtModule.register({ | ||
secret: jwtConstants.secret, | ||
signOptions: { expiresIn: '60s' }, | ||
}), | ||
], | ||
exports: [UsersService, JwtService], | ||
}) | ||
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 { 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,26 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { UsersService } from './users/users.service'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
|
||
@Injectable() | ||
export class AuthService { | ||
constructor( | ||
private usersService: UsersService, | ||
private jwtService: JwtService, | ||
) {} | ||
|
||
async validateUser(username: string, pass: string): Promise<any> { | ||
const user = await this.usersService.findOne(username); | ||
if (user && user.password === pass) { | ||
const { password, ...result } = user; | ||
return result; | ||
} | ||
return null; | ||
} | ||
async login(user: any) { | ||
const payload = { username: user.username, sub: user.userId }; | ||
return { | ||
access_token: this.jwtService.sign(payload), | ||
}; | ||
} | ||
} |
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,5 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
|
||
@Injectable() | ||
export class JwtAuthGuard extends AuthGuard('jwt') {} |
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,5 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
|
||
@Injectable() | ||
export class LocalAuthGuard extends AuthGuard('local') {} |
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,19 @@ | ||
import { ExtractJwt, Strategy } from 'passport-jwt'; | ||
import { PassportStrategy } from '@nestjs/passport'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { jwtConstants } from '../utils/constants'; | ||
|
||
@Injectable() | ||
export class JwtStrategy extends PassportStrategy(Strategy) { | ||
constructor() { | ||
super({ | ||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), | ||
ignoreExpiration: false, | ||
secretOrKey: jwtConstants.secret, | ||
}); | ||
} | ||
|
||
async validate(payload: any) { | ||
return { userId: payload.sub, username: payload.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,19 @@ | ||
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(); | ||
} | ||
|
||
async validate(username: string, password: string): Promise<any> { | ||
const user = await this.authService.validateUser(username, password); | ||
if (!user) { | ||
throw new UnauthorizedException(); | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { UsersService } from './users.service'; | ||
|
||
@Module({ | ||
providers: [UsersService], | ||
exports: [UsersService], | ||
}) | ||
export class UsersModule {} |
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 { UsersService } from './users.service'; | ||
|
||
describe('UsersService', () => { | ||
let service: UsersService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [UsersService], | ||
}).compile(); | ||
|
||
service = module.get<UsersService>(UsersService); | ||
}); | ||
|
||
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,24 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
// This should be a real class/interface representing a user entity | ||
export type User = any; | ||
|
||
@Injectable() | ||
export class UsersService { | ||
private readonly users = [ | ||
{ | ||
userId: 1, | ||
username: 'john', | ||
password: 'changeme', | ||
}, | ||
{ | ||
userId: 2, | ||
username: 'maria', | ||
password: 'guess', | ||
}, | ||
]; | ||
|
||
async findOne(username: string): Promise<User | undefined> { | ||
return this.users.find((user) => user.username === 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,3 @@ | ||
export const jwtConstants = { | ||
secret: process.env.JWT_SECRET, | ||
}; |
Oops, something went wrong.