Skip to content

Commit

Permalink
Merge pull request arrdix#23 from arrdix/product-service
Browse files Browse the repository at this point in the history
feat: exclude some routes
  • Loading branch information
arrdix authored Jul 18, 2024
2 parents cb2653c + d1ece49 commit fa501b2
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 141 deletions.
236 changes: 118 additions & 118 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,129 +1,129 @@
import { Injectable } from "@nestjs/common";
import { LoginDto } from "src/auth/dto/login.dto";
import Hasher from "src/utils/Hasher";
import { mailer } from "src/utils/Mailer";
import { RegisterDto } from "src/auth/dto/register.dto";
import { PrismaService } from "src/prisma/prisma.service";
import { CartService } from "src/cart/cart.service";
import { User } from "src/types/user-type";
import { UserService } from "src/user/user.service";
import * as jwt from "jsonwebtoken";
import CONFIG from "src/configs/config";
import { ForgotPasswordDto } from "src/auth/dto/forgot-password.dto";
import { ResetPasswordDto } from "src/auth/dto/reset-password.dto";
import { Injectable } from '@nestjs/common'
import { LoginDto } from 'src/auth/dto/login.dto'
import Hasher from 'src/utils/Hasher'
import { mailer } from 'src/utils/Mailer'
import { RegisterDto } from 'src/auth/dto/register.dto'
import { PrismaService } from 'src/prisma/prisma.service'
import { CartService } from 'src/cart/cart.service'
import { User } from 'src/types/user-type'
import { UserService } from 'src/user/user.service'
import * as jwt from 'jsonwebtoken'
import CONFIG from 'src/configs/config'
import { ForgotPasswordDto } from 'src/auth/dto/forgot-password.dto'
import { ResetPasswordDto } from 'src/auth/dto/reset-password.dto'

@Injectable()
export class AuthService {
constructor(
private readonly prismaService: PrismaService,
private readonly cartService: CartService,
private readonly userService: UserService
) {}

async register(registerDto: RegisterDto) {
// hash the password
const hashedPassword = await Hasher.hashPassword(registerDto.password);

// create new user
const createdUser: User = await this.prismaService.users.create({
data: {
...registerDto,
password: hashedPassword,
},
});

// create profile for new user
this.createProfile(createdUser.id);

// create default cart for new user
this.cartService.create({
price: 0,
discount: 0,
userId: createdUser.id,
storeId: null,
});

return {
status: "Ok!",
};
}

async createProfile(userId: number) {
return await this.prismaService.profile.create({
data: {
userId,
},
});
}

async login(loginDto: LoginDto) {
const requestedUser: User = await this.prismaService.users.findFirst({
where: {
email: loginDto.email,
},
});

if (!requestedUser) {
return {
status: "Wrong username/password.",
};
constructor(
private readonly prismaService: PrismaService,
private readonly cartService: CartService,
private readonly userService: UserService
) {}

async register(registerDto: RegisterDto) {
// hash the password
const hashedPassword = await Hasher.hashPassword(registerDto.password)

// create new user
const createdUser: User = await this.prismaService.users.create({
data: {
...registerDto,
password: hashedPassword,
},
})

// create profile for new user
this.createProfile(createdUser.id)

// create default cart for new user
this.cartService.create({
price: 0,
discount: 0,
userId: createdUser.id,
storeId: null,
})

return {
status: 'Ok!',
}
}

const isPasswordMatch = await Hasher.comparePassword(
loginDto.password,
requestedUser.password
);
async createProfile(userId: number) {
return await this.prismaService.profile.create({
data: {
userId,
},
})
}

if (!isPasswordMatch) {
return {
status: "Wrong username/password.",
};
async login(loginDto: LoginDto) {
const requestedUser: User = await this.prismaService.users.findFirst({
where: {
email: loginDto.email,
},
})

if (!requestedUser) {
return {
status: 'Wrong username/password.',
}
}

const isPasswordMatch = await Hasher.comparePassword(
loginDto.password,
requestedUser.password
)

if (!isPasswordMatch) {
return {
status: 'Wrong username/password.',
}
}

delete requestedUser.email
delete requestedUser.phone
delete requestedUser.password

return {
token: jwt.sign(requestedUser, CONFIG.SECRET_SAUCE),
}
}

delete requestedUser.email;
delete requestedUser.phone;
delete requestedUser.password;

return {
token: jwt.sign(requestedUser, CONFIG.SECRET_SAUCE),
};
}

async forgot(forgotPasswordDto: ForgotPasswordDto) {
const requestedUser = await this.prismaService.users.findFirst({
where: {
email: forgotPasswordDto.email,
},
});

if (!requestedUser) {
return {
error: `User with email ${forgotPasswordDto.email} doesn't exist.`,
};
async forgot(forgotPasswordDto: ForgotPasswordDto) {
const requestedUser = await this.prismaService.users.findFirst({
where: {
email: forgotPasswordDto.email,
},
})

if (!requestedUser) {
return {
error: `User with email ${forgotPasswordDto.email} doesn't exist.`,
}
}

// call send email
mailer(forgotPasswordDto.email)

delete requestedUser.email
delete requestedUser.phone
delete requestedUser.password

return {
token: jwt.sign(requestedUser, CONFIG.SECRET_SAUCE),
}
}

// call send email
mailer(forgotPasswordDto.email);

delete requestedUser.email;
delete requestedUser.phone;
delete requestedUser.password;

return {
token: jwt.sign(requestedUser, CONFIG.SECRET_SAUCE),
};
}

async reset(resetPasswordDto: ResetPasswordDto) {
const requestedUser = await this.prismaService.users.update({
where: {
email: resetPasswordDto.requester,
},
data: {
password: await Hasher.hashPassword(resetPasswordDto.password),
},
});

return requestedUser;
}
async reset(resetPasswordDto: ResetPasswordDto) {
const requestedUser = await this.prismaService.users.update({
where: {
email: resetPasswordDto.requester,
},
data: {
password: await Hasher.hashPassword(resetPasswordDto.password),
},
})

return requestedUser
}
}
43 changes: 20 additions & 23 deletions src/user/user.module.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
import {
MiddlewareConsumer,
Module,
NestModule,
RequestMethod,
} from "@nestjs/common";
import { UserService } from "./user.service";
import { UserController } from "./user.controller";
import { PrismaService } from "src/prisma/prisma.service";
import { CartService } from "src/cart/cart.service";
import { AuthenticationMiddleware } from "src/middlewares/authentication.middleware";
import { MiddlewareConsumer, Module, NestModule, RequestMethod } from '@nestjs/common'
import { UserService } from './user.service'
import { UserController } from './user.controller'
import { PrismaService } from 'src/prisma/prisma.service'
import { CartService } from 'src/cart/cart.service'
import { AuthenticationMiddleware } from 'src/middlewares/authentication.middleware'

@Module({
controllers: [UserController],
providers: [UserService, PrismaService, CartService],
controllers: [UserController],
providers: [UserService, PrismaService, CartService],
})
export class UserModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer
.apply(AuthenticationMiddleware)
.exclude(
{ path: "/auth/login", method: RequestMethod.POST },
{ path: "/auth/register", method: RequestMethod.POST },
{ path: "/auth/forgot", method: RequestMethod.POST }
)
.forRoutes({ path: "*", method: RequestMethod.ALL });
}
configure(consumer: MiddlewareConsumer) {
consumer
.apply(AuthenticationMiddleware)
.exclude(
{ path: '/auth/login', method: RequestMethod.POST },
{ path: '/auth/register', method: RequestMethod.POST },
{ path: '/auth/forgot', method: RequestMethod.POST },
{ path: '/product/id', method: RequestMethod.GET },
{ path: '/product/sku', method: RequestMethod.GET }
)
.forRoutes({ path: '*', method: RequestMethod.ALL })
}
}

0 comments on commit fa501b2

Please sign in to comment.