feat: generate jwt on login
This commit is contained in:
@@ -21,10 +21,11 @@ import configuration from './config/configuration';
|
||||
.valid('development', 'production', 'test', 'provision')
|
||||
.default('development'),
|
||||
PORT: Joi.number().default(3000),
|
||||
DATABASE_CLIENT: Joi.valid('mysql', 'postgres'),
|
||||
DATABASE_HOST: Joi.string(),
|
||||
DATABASE_NAME: Joi.string(),
|
||||
DATABASE_USERNAME: Joi.string(),
|
||||
SECRET: Joi.string().required(),
|
||||
DATABASE_CLIENT: Joi.valid('mysql', 'postgres').required(),
|
||||
DATABASE_HOST: Joi.string().required(),
|
||||
DATABASE_NAME: Joi.string().required(),
|
||||
DATABASE_USERNAME: Joi.string().required(),
|
||||
DATABASE_PASSWORD: Joi.string().empty('').default(''),
|
||||
DATABASE_PORT: Joi.number().default(5432),
|
||||
}),
|
||||
|
||||
@@ -1,14 +1,17 @@
|
||||
import { Controller, Post, UseGuards, Request } from '@nestjs/common';
|
||||
import { LocalAuthGuard } from './local-auth.guard';
|
||||
import { AuthService } from './auth.service';
|
||||
|
||||
@Controller({
|
||||
path: 'auth',
|
||||
version: '1',
|
||||
})
|
||||
export class AuthController {
|
||||
constructor(private authService: AuthService) {}
|
||||
|
||||
@UseGuards(LocalAuthGuard)
|
||||
@Post('login')
|
||||
async login(@Request() req) {
|
||||
return req.user;
|
||||
return this.authService.login(req.user);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,9 +4,24 @@ import { UsersModule } from '../users/users.module';
|
||||
import { PassportModule } from '@nestjs/passport';
|
||||
import { LocalStrategy } from './local.strategy';
|
||||
import { AuthController } from './auth.controller';
|
||||
import { JwtModule } from '@nestjs/jwt';
|
||||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||
|
||||
@Module({
|
||||
imports: [UsersModule, PassportModule],
|
||||
imports: [
|
||||
UsersModule,
|
||||
PassportModule,
|
||||
JwtModule.registerAsync({
|
||||
imports: [ConfigModule],
|
||||
inject: [ConfigService],
|
||||
useFactory: (configService: ConfigService) => {
|
||||
return {
|
||||
secret: configService.get<string>('secret'),
|
||||
signOptions: { expiresIn: '1d' },
|
||||
};
|
||||
},
|
||||
}),
|
||||
],
|
||||
providers: [AuthService, LocalStrategy],
|
||||
controllers: [AuthController],
|
||||
})
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { UsersService } from '../users/users.service';
|
||||
import { hashPassword } from '../helper/hash_password';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
|
||||
@Injectable()
|
||||
export class AuthService {
|
||||
constructor(private usersService: UsersService) {}
|
||||
constructor(
|
||||
private usersService: UsersService,
|
||||
private jwtService: JwtService,
|
||||
) {}
|
||||
|
||||
async validateUser(username: string, pass: string): Promise<any> {
|
||||
const user = await this.usersService.findOneByUsername(username);
|
||||
@@ -17,4 +21,15 @@ export class AuthService {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
async login(user: any) {
|
||||
const payload = {
|
||||
username: user.username,
|
||||
sub: user.userId,
|
||||
};
|
||||
|
||||
return {
|
||||
access_token: this.jwtService.sign(payload),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
export default () => {
|
||||
return {
|
||||
port: parseInt(process.env.PORT, 10) || 3000,
|
||||
secret: process.env.SECRET,
|
||||
database: {
|
||||
client: process.env.DATABASE_CLIENT,
|
||||
host: process.env.DATABASE_HOST,
|
||||
|
||||
Reference in New Issue
Block a user