From 78807ffd6124d35c4763ea4bffdebcc9cd511b84 Mon Sep 17 00:00:00 2001 From: Avnyr Date: Mon, 4 Mar 2024 15:24:20 +0100 Subject: [PATCH] Update --- package.json | 6 ++++++ src/app.module.ts | 11 +++++++++-- src/auth/auth.module.ts | 9 +++++---- src/auth/auth.service.ts | 26 +++++++++++++++++++++++--- src/prisma/prisma.service.ts | 23 ++++++++++++----------- 5 files changed, 55 insertions(+), 20 deletions(-) diff --git a/package.json b/package.json index 2494d9e..a7019d7 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/app.module.ts b/src/app.module.ts index ac40790..d9dac07 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -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 {} diff --git a/src/auth/auth.module.ts b/src/auth/auth.module.ts index 9486c15..d42d844 100644 --- a/src/auth/auth.module.ts +++ b/src/auth/auth.module.ts @@ -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 {} \ No newline at end of file +export class AuthModule { } diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index 5eec152..c74533d 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -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 { } } } -} \ No newline at end of file + + 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`, + + }) + } +} diff --git a/src/prisma/prisma.service.ts b/src/prisma/prisma.service.ts index af21ea1..0720ee1 100644 --- a/src/prisma/prisma.service.ts +++ b/src/prisma/prisma.service.ts @@ -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"), + }, + }, + }); + } }