Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
more auth
  • Loading branch information
bryopsida committed Jan 7, 2023
commit 47ebacf2f4555410fa2fbd8e9368342ed3ae99ef
170 changes: 170 additions & 0 deletions package-lock.json

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

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"@nestjs/common": "^9.0.0",
"@nestjs/core": "^9.0.0",
"@nestjs/graphql": "^10.1.7",
"@nestjs/jwt": "^10.0.1",
"@nestjs/mercurius": "^10.1.7",
"@nestjs/mongoose": "^9.2.1",
"@nestjs/passport": "^9.0.0",
Expand All @@ -33,6 +34,7 @@
"mongoose": "^6.8.2",
"nestjs-pino": "^3.1.1",
"passport": "^0.6.0",
"passport-jwt": "^4.0.1",
"passport-local": "^1.0.0",
"pino-http": "^8.3.1",
"reflect-metadata": "^0.1.13",
Expand All @@ -44,6 +46,7 @@
"@nestjs/testing": "^9.0.0",
"@types/jest": "29.2.5",
"@types/node": "18.11.18",
"@types/passport-jwt": "^3.0.8",
"@types/passport-local": "^1.0.35",
"@types/supertest": "^2.0.11",
"@typescript-eslint/eslint-plugin": "^5.0.0",
Expand Down
9 changes: 8 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { MessageModule } from './messages/message.module';
import { CommonModule } from './common/common.module';
import { UsersModule } from './users/users.module';
import { AuthenticationModule } from './authentication/authentication.module';
import { APP_GUARD } from '@nestjs/core';
import { JwtAuthGuard } from './authentication/guards/jwt-auth.guard';

@Module({
imports: [
Expand All @@ -26,6 +28,11 @@ import { AuthenticationModule } from './authentication/authentication.module';
UsersModule,
],
controllers: [],
providers: [],
providers: [
{
provide: APP_GUARD,
useClass: JwtAuthGuard,
},
],
})
export class AppModule {}
23 changes: 20 additions & 3 deletions src/authentication/authentication.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,28 @@ import { LocalStrategy } from './strategies/local.strategy';
import { PassportModule } from '@nestjs/passport';
import { LoginController } from './controllers/login.controller';
import { LocalAuthGuard } from './guards/local-auth.guard';
import { JwtModule } from '@nestjs/jwt';
import { jwtConstants } from './constant';
import { JwtStrategy } from './strategies/jwt.strategy';
import { JwtAuthGuard } from './guards/jwt-auth.guard';

@Module({
controllers: [LoginController],
exports: [LocalAuthGuard],
imports: [UsersModule, PassportModule],
providers: [AuthenticationService, LocalStrategy],
exports: [AuthenticationService],
imports: [
UsersModule,
PassportModule,
JwtModule.register({
secret: jwtConstants.secret,
signOptions: { expiresIn: '60s' },
}),
],
providers: [
AuthenticationService,
LocalAuthGuard,
JwtAuthGuard,
LocalStrategy,
JwtStrategy,
],
})
export class AuthenticationModule {}
14 changes: 13 additions & 1 deletion src/authentication/authentication.service.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
import { Injectable } from '@nestjs/common';
import { UsersService } from 'src/users/users.service';
import { JwtService } from '@nestjs/jwt';

@Injectable()
export class AuthenticationService {
constructor(private usersService: UsersService) {}
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) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
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),
};
}
}
Loading