Skip to content

Instantly share code, notes, and snippets.

@omarberrami
Created July 29, 2022 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omarberrami/f91140240bac674e03fdde364a217875 to your computer and use it in GitHub Desktop.
Save omarberrami/f91140240bac674e03fdde364a217875 to your computer and use it in GitHub Desktop.
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