mirror of
https://github.com/Kevsl/crypto-exchange-api.git
synced 2025-07-09 14:00:12 +02:00
added user info in sign in and sign up return
This commit is contained in:
parent
67d33dce2e
commit
b194d084cf
@ -2,7 +2,7 @@ import { ForbiddenException, Injectable } from '@nestjs/common';
|
|||||||
import { PrismaService } from '../prisma/prisma.service';
|
import { PrismaService } from '../prisma/prisma.service';
|
||||||
import { AuthLoginDto, AuthRegisterDto } from './dto';
|
import { AuthLoginDto, AuthRegisterDto } from './dto';
|
||||||
import * as argon from 'argon2';
|
import * as argon from 'argon2';
|
||||||
import { Prisma } from '@prisma/client';
|
import { Prisma, User } from '@prisma/client';
|
||||||
import { JwtService } from '@nestjs/jwt';
|
import { JwtService } from '@nestjs/jwt';
|
||||||
import { ConfigService } from '@nestjs/config';
|
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) {
|
} catch (error) {
|
||||||
if (error instanceof Prisma.PrismaClientKnownRequestError) {
|
if (error instanceof Prisma.PrismaClientKnownRequestError) {
|
||||||
if (error.code === 'P2002') {
|
if (error.code === 'P2002') {
|
||||||
@ -69,17 +69,16 @@ export class AuthService {
|
|||||||
const pwMatches = await argon.verify(user.hash, dto.password);
|
const pwMatches = await argon.verify(user.hash, dto.password);
|
||||||
if (!pwMatches) throw new ForbiddenException('Credentials incorrect');
|
if (!pwMatches) throw new ForbiddenException('Credentials incorrect');
|
||||||
|
|
||||||
return this.signToken(user.id, user.email);
|
return this.signToken(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
async signToken(
|
async signToken(user: User): Promise<{ access_token: string; user: User }> {
|
||||||
userId: string,
|
|
||||||
email: string,
|
|
||||||
): Promise<{ access_token: string }> {
|
|
||||||
const payload = {
|
const payload = {
|
||||||
sub: userId,
|
sub: user.id,
|
||||||
email: email,
|
email: user.email,
|
||||||
};
|
};
|
||||||
|
user.hash = null;
|
||||||
|
|
||||||
const secret = this.config.get('JWT_SECRET');
|
const secret = this.config.get('JWT_SECRET');
|
||||||
const token = await this.jwt.signAsync(payload, {
|
const token = await this.jwt.signAsync(payload, {
|
||||||
expiresIn: '30d',
|
expiresIn: '30d',
|
||||||
@ -88,6 +87,7 @@ export class AuthService {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
access_token: token,
|
access_token: token,
|
||||||
|
user,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
0
src/utils/styles.ts
Normal file
0
src/utils/styles.ts
Normal file
Loading…
x
Reference in New Issue
Block a user