Update
This commit is contained in:
parent
d4a9519cbf
commit
78807ffd61
@ -21,12 +21,17 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@nestjs/common": "^10.0.0",
|
||||
"@nestjs/config": "^3.2.0",
|
||||
"@nestjs/core": "^10.0.0",
|
||||
"@nestjs/jwt": "^10.2.0",
|
||||
"@nestjs/passport": "^10.0.3",
|
||||
"@nestjs/platform-express": "^10.0.0",
|
||||
"@prisma/client": "^5.10.2",
|
||||
"argon2": "^0.40.1",
|
||||
"class-transformer": "^0.5.1",
|
||||
"class-validator": "^0.14.1",
|
||||
"passport": "^0.7.0",
|
||||
"passport-jwt": "^4.0.1",
|
||||
"reflect-metadata": "^0.2.0",
|
||||
"rxjs": "^7.8.1"
|
||||
},
|
||||
@ -38,6 +43,7 @@
|
||||
"@types/express": "^4.17.17",
|
||||
"@types/jest": "^29.5.2",
|
||||
"@types/node": "^20.3.1",
|
||||
"@types/passport-jwt": "^4.0.1",
|
||||
"@types/supertest": "^6.0.0",
|
||||
"jest": "^29.5.0",
|
||||
"prisma": "^5.10.2",
|
||||
|
@ -5,10 +5,17 @@ import { BookmarkModule } from './bookmark/bookmark.module';
|
||||
import { AuthService } from './auth/auth.service';
|
||||
import { AuthController } from './auth/auth.controller';
|
||||
import { PrismaModule } from './prisma/prisma.module';
|
||||
import { ConfigModule } from "@nestjs/config";
|
||||
|
||||
@Module({
|
||||
imports: [AuthModule, PrismaModule, UserModule, BookmarkModule],
|
||||
imports: [
|
||||
ConfigModule.forRoot({ isGlobal: true }),
|
||||
AuthModule,
|
||||
PrismaModule,
|
||||
UserModule,
|
||||
BookmarkModule,
|
||||
],
|
||||
providers: [AuthService],
|
||||
controllers: [AuthController]
|
||||
controllers: [AuthController],
|
||||
})
|
||||
export class AppModule {}
|
||||
|
@ -2,10 +2,11 @@ import { Module } from "@nestjs/common";
|
||||
import { PrismaModule } from "src/prisma/prisma.module";
|
||||
import { AuthController } from "./auth.controller";
|
||||
import { AuthService } from "./auth.service";
|
||||
import { JwtModule } from "@nestjs/jwt";
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule],
|
||||
controllers: [AuthController],
|
||||
providers: [AuthService]
|
||||
imports: [PrismaModule, JwtModule.register({})],
|
||||
controllers: [AuthController],
|
||||
providers: [AuthService]
|
||||
})
|
||||
export class AuthModule {}
|
||||
export class AuthModule { }
|
||||
|
@ -3,10 +3,16 @@ import { PrismaService } from "src/prisma/prisma.service";
|
||||
import { AuthDto } from "./dto";
|
||||
import * as argon from "argon2";
|
||||
import { PrismaClientKnownRequestError } from "@prisma/client/runtime/library";
|
||||
import { JwtService } from "@nestjs/jwt";
|
||||
import { User } from "@prisma/client";
|
||||
import { time } from "console";
|
||||
|
||||
@Injectable({})
|
||||
export class AuthService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
constructor(
|
||||
private prisma: PrismaService,
|
||||
private jwt: JwtService,
|
||||
) {}
|
||||
|
||||
async login(dto: AuthDto) {
|
||||
const User = await this.prisma.user.findUnique({
|
||||
@ -19,7 +25,7 @@ export class AuthService {
|
||||
throw new ForbiddenException("Credential(s) invalid.");
|
||||
}
|
||||
|
||||
const pwMatches = await argon.verify(User.hash, dto.password);
|
||||
const pwMatches: boolean = await argon.verify(User.hash, dto.password);
|
||||
if (!pwMatches) {
|
||||
console.warn(
|
||||
`ACCESS: Refused login for "${dto.email}" (invalid password)`,
|
||||
@ -57,4 +63,18 @@ export class AuthService {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async generateAuthToken(targetUser: User, dayToLive: number) {
|
||||
const timestamp = Date.now();
|
||||
|
||||
const jwtPayload = {
|
||||
sub: targetUser.id,
|
||||
iat: timestamp,
|
||||
};
|
||||
|
||||
return this.jwt.signAsync(jwtPayload, {
|
||||
expiresIn: `${dayToLive}d`,
|
||||
|
||||
})
|
||||
}
|
||||
}
|
@ -1,15 +1,16 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
import { PrismaClient } from "@prisma/client";
|
||||
|
||||
@Injectable()
|
||||
export class PrismaService extends PrismaClient {
|
||||
constructor() {
|
||||
super({
|
||||
datasources: {
|
||||
db: {
|
||||
url: "mysql://avnyr:orpmocclealis8havele@127.0.0.1:3306/bookmarks"
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
constructor(config: ConfigService) {
|
||||
super({
|
||||
datasources: {
|
||||
db: {
|
||||
url: config.get("DATABASE_URL"),
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user