-
-
Save omarberrami/f91140240bac674e03fdde364a217875 to your computer and use it in GitHub Desktop.
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
import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; | |
import { User } from 'src/user/entities/user.entity'; | |
import { UserService } from 'src/user/user.service'; | |
import { legacyEncoding } from 'src/utils/encoding/legacy-encoding'; | |
import { JwtService } from '@nestjs/jwt'; | |
@Injectable() | |
export class AuthService { | |
constructor( | |
private userService: UserService, | |
private jwtTokenService: JwtService, | |
) { } | |
async validateCredential(email: string, pass: string) { | |
const user = await this.userService.findByEmail(email); | |
if ( | |
user && | |
user.id && | |
user.password !== '' && | |
[ | |
user.password, | |
legacyEncoding(user.password), | |
legacyEncoding(legacyEncoding(user.password)), | |
].includes(pass) | |
) { | |
return user; | |
} | |
return null; | |
} | |
async loginWithCredentials(email: string, pass: string) { | |
const user = await this.userService.findByEmail(email); | |
if ( | |
user && | |
user.id && | |
user.password !== '' && | |
[ | |
user.password, | |
legacyEncoding(user.password), | |
legacyEncoding(legacyEncoding(user.password)), | |
].includes(pass) | |
) { | |
const payload = { email: user.email, sub: user.id }; | |
return { | |
access_token: this.jwtTokenService.sign(payload), | |
}; | |
} | |
else throw new HttpException('UNAUTHORIZED', HttpStatus.UNAUTHORIZED); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment