Template
frontend login forms
This commit is contained in:
1 parent
96e8628dad
commit
787aa97f50
32 files changed
+6506
-3
No files matched your search
@@ -52,6 +52,7 @@ export class AuthService {
|
||||
|
||||
async login(loginDto: LoginDto) {
|
||||
const payload = { email: loginDto.email, password: loginDto.password };
|
||||
console.log(payload);
|
||||
return {
|
||||
access_token: this.jwtService.sign(payload),
|
||||
};
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
import { createParamDecorator, ExecutionContext } from '@nestjs/common';
|
||||
|
||||
export const GetUser = createParamDecorator(
|
||||
(data: unknown, ctx: ExecutionContext) => {
|
||||
const request = ctx.switchToHttp().getRequest();
|
||||
return request.user;
|
||||
},
|
||||
);
|
||||
@@ -2,10 +2,14 @@ import { Injectable } from '@nestjs/common';
|
||||
import { PassportStrategy } from '@nestjs/passport';
|
||||
import { ExtractJwt, Strategy } from 'passport-jwt';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { PrismaService } from 'src/prisma/prisma.service';
|
||||
|
||||
@Injectable()
|
||||
export class JwtStrategy extends PassportStrategy(Strategy) {
|
||||
constructor(private configService: ConfigService) {
|
||||
constructor(
|
||||
private configService: ConfigService,
|
||||
private prisma: PrismaService,
|
||||
) {
|
||||
super({
|
||||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
||||
ignoreExpiration: false,
|
||||
@@ -14,6 +18,16 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
|
||||
}
|
||||
|
||||
async validate(payload: any) {
|
||||
return { id: payload.sub, email: payload.email, role: payload.role };
|
||||
const user = await this.prisma.user.findUnique({
|
||||
where: { id: payload.sub },
|
||||
select: {
|
||||
id: true,
|
||||
email: true,
|
||||
name: true,
|
||||
lastName: true,
|
||||
role: true,
|
||||
},
|
||||
});
|
||||
return user;
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,8 @@ import { Role } from '../../common/enums/role.enum';
|
||||
import { UsersService } from './users.service';
|
||||
import { CreateUserDto } from './dtos/create-user.dto';
|
||||
// import { UpdateUserDto } from './dtos/update-user.dto';
|
||||
import { GetUser } from '../auth/decorators/get-user.decorators';
|
||||
import { User } from '@prisma/client';
|
||||
|
||||
@Controller('users')
|
||||
// @UseGuards(JwtAuthGuard, RolesGuard)
|
||||
@@ -32,6 +34,14 @@ export class UsersController {
|
||||
findAll() {
|
||||
return this.usersService.findAll();
|
||||
}
|
||||
@Get('me')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
async getProfile(@GetUser() user: User) {
|
||||
const { password, ...userWithoutPassword } =
|
||||
await this.usersService.findById(user.id);
|
||||
return userWithoutPassword;
|
||||
}
|
||||
|
||||
@Get()
|
||||
findByEmail(email: string) {
|
||||
return this.usersService.findByEmail(email);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { PrismaService } from 'src/prisma/prisma.service';
|
||||
// import { User, Prisma } from '@prisma/client';
|
||||
import * as bcrypt from 'bcrypt';
|
||||
@@ -38,4 +38,16 @@ export class UsersService {
|
||||
async findAll() {
|
||||
return this.prisma.user.findMany();
|
||||
}
|
||||
|
||||
async findById(id: string) {
|
||||
const user = await this.prisma.user.findUnique({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new NotFoundException(`User with ID ${id} not found`);
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user