added user info in sign in and sign up return

This commit is contained in:
Kevsl 2024-06-07 14:27:04 +02:00
parent 67d33dce2e
commit b194d084cf
2 changed files with 9 additions and 9 deletions

View File

@ -2,7 +2,7 @@ import { ForbiddenException, Injectable } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
import { AuthLoginDto, AuthRegisterDto } from './dto';
import * as argon from 'argon2';
import { Prisma } from '@prisma/client';
import { Prisma, User } from '@prisma/client';
import { JwtService } from '@nestjs/jwt';
import { ConfigService } from '@nestjs/config';
@ -47,7 +47,7 @@ export class AuthService {
},
});
return this.signToken(user.id, user.email);
return this.signToken(user);
} catch (error) {
if (error instanceof Prisma.PrismaClientKnownRequestError) {
if (error.code === 'P2002') {
@ -69,17 +69,16 @@ export class AuthService {
const pwMatches = await argon.verify(user.hash, dto.password);
if (!pwMatches) throw new ForbiddenException('Credentials incorrect');
return this.signToken(user.id, user.email);
return this.signToken(user);
}
async signToken(
userId: string,
email: string,
): Promise<{ access_token: string }> {
async signToken(user: User): Promise<{ access_token: string; user: User }> {
const payload = {
sub: userId,
email: email,
sub: user.id,
email: user.email,
};
user.hash = null;
const secret = this.config.get('JWT_SECRET');
const token = await this.jwt.signAsync(payload, {
expiresIn: '30d',
@ -88,6 +87,7 @@ export class AuthService {
return {
access_token: token,
user,
};
}
}

0
src/utils/styles.ts Normal file
View File