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