forked from arrdix/lakoe-app-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request arrdix#23 from arrdix/product-service
feat: exclude some routes
- Loading branch information
Showing
2 changed files
with
138 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
} | ||
} |