auth fixed

This commit is contained in:
dimitar committed 2024-12-13 11:00:04 +01:00
1 parent 787aa97f50
commit a42c875b2c
5 files changed
+27 -8

No files matched your search

+7 -1
View File
@@ -51,7 +51,13 @@ export class AuthService {
}
async login(loginDto: LoginDto) {
const payload = { email: loginDto.email, password: loginDto.password };
const user = await this.validateUser(loginDto.email, loginDto.password);
const payload = {
email: loginDto.email,
password: loginDto.password,
sub: user.id,
role: user.role,
};
console.log(payload);
return {
access_token: this.jwtService.sign(payload),
@@ -1,5 +1,10 @@
import { Injectable } from '@nestjs/common';
import { Injectable, ExecutionContext } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
@Injectable()
export class JwtAuthGuard extends AuthGuard('jwt') {}
export class JwtAuthGuard extends AuthGuard('jwt') {
handleRequest(err, user, info, context: ExecutionContext) {
console.log('JWT Auth Guard:', { err, user, info });
return super.handleRequest(err, user, info, context);
}
}
@@ -18,6 +18,7 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
}
async validate(payload: any) {
console.log('payload', payload);
const user = await this.prisma.user.findUnique({
where: { id: payload.sub },
select: {
@@ -28,6 +29,7 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
role: true,
},
});
console.log('user found:', user);
return user;
}
}
@@ -37,8 +37,10 @@ export class UsersController {
@Get('me')
@UseGuards(JwtAuthGuard)
async getProfile(@GetUser() user: User) {
console.log('Fetching profile for user:', user);
const { password, ...userWithoutPassword } =
await this.usersService.findById(user.id);
console.log('user profile', userWithoutPassword);
return userWithoutPassword;
}