Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
A
auth
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Цой Кирилл
auth
Commits
d8a0d9c0
Commit
d8a0d9c0
authored
Aug 02, 2024
by
Цой Кирилл
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
БЭм bitch
parent
bf28139e
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
52 additions
and
2 deletions
+52
-2
auth.service.ts
auth-service/src/auth/auth.service.ts
+52
-2
No files found.
auth-service/src/auth/auth.service.ts
View file @
d8a0d9c0
// import { Body, HttpException, HttpStatus, Injectable, UnauthorizedException } from '@nestjs/common';
// import { UserDto } from '../users/models/user.dto';
// import { UsersService } from '../users/users.service';
// import { JwtService } from '@nestjs/jwt';
// import * as bcrypt from "bcryptjs"
// import { User } from '../users/models/user.entity';
// @Injectable()
// export class AuthService {
// constructor(
// private readonly userService: UsersService,
// private readonly jwtService: JwtService
// ) {}
// async auth(userDto: UserDto){
// const user = await this.validateUser(userDto)
// return this.generateToken(user)
// }
// async register(userDto: UserDto){
// const candidate = await this.userService.getUserByEmail(userDto.email)
// if (candidate) throw new HttpException('User already exists', HttpStatus.BAD_REQUEST)
// const hashPassword = await bcrypt.hash(candidate.password, 5)
// const user = await this.userService.createUser({ ...userDto, password: hashPassword})
// return this.generateToken(user)
// }
// async generateToken(user: User) {
// const payload = {
// email: user.email,
// id: user.id,
// name: user.name
// }
// return {
// token: this.jwtService.sign(payload)
// }
// }
// async validateUser(userDto: UserDto){
// const user = await this.userService.getUserByEmail(userDto.email)
// const passwordValid = await bcrypt.compare(userDto.password, user.password)
// if (user && passwordValid) return user
// throw new UnauthorizedException({ messaage: "Invalid password" })
// }
// }
import
{
Body
,
HttpException
,
HttpStatus
,
Injectable
,
UnauthorizedException
}
from
'@nestjs/common'
;
import
{
UserDto
}
from
'../users/models/user.dto'
;
import
{
UsersService
}
from
'../users/users.service'
;
...
...
@@ -23,7 +73,7 @@ export class AuthService {
const
candidate
=
await
this
.
userService
.
getUserByEmail
(
userDto
.
email
)
if
(
candidate
)
throw
new
HttpException
(
'User already exists'
,
HttpStatus
.
BAD_REQUEST
)
const
hashPassword
=
await
bcrypt
.
hash
(
candidate
.
password
,
5
)
const
hashPassword
=
await
bcrypt
.
hash
(
userDto
.
password
,
5
)
const
user
=
await
this
.
userService
.
createUser
({
...
userDto
,
password
:
hashPassword
})
return
this
.
generateToken
(
user
)
}
...
...
@@ -42,7 +92,7 @@ export class AuthService {
async
validateUser
(
userDto
:
UserDto
){
const
user
=
await
this
.
userService
.
getUserByEmail
(
userDto
.
email
)
const
passwordValid
=
await
bcrypt
.
compare
(
userDto
.
password
,
user
.
password
)
const
passwordValid
=
bcrypt
.
compare
(
userDto
.
password
,
user
.
password
)
if
(
user
&&
passwordValid
)
return
user
throw
new
UnauthorizedException
({
messaage
:
"Invalid password"
})
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment