Skip to content

Commit

Permalink
feat: adding services to project
Browse files Browse the repository at this point in the history
  • Loading branch information
lucsimao committed Oct 4, 2021
1 parent 4b471ad commit 6678071
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/services/AuthService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { IUser } from '../models/user/IUser';
import jwt from 'jsonwebtoken';

export interface IDecodedUser extends Omit<IUser, '_id'> {
id: string;
}

const AUTH_SECRET = 'secret';
const AUTH_EXPIRES_IN = '30m';

export default class AuthService {
public static generateToken(payload: {
[key: string]: string | boolean | number;
}): string {
const token = jwt.sign(payload, AUTH_SECRET, {
expiresIn: AUTH_EXPIRES_IN,
});

return token;
}

public static decodeToken(token: string): IDecodedUser {
return jwt.verify(token, AUTH_SECRET) as IDecodedUser;
}

public static generateUserToken(userData: IUser & { id?: string }): string {
return this.generateToken({
nome: userData.nome,
email: userData.email,
ultimo_login: new Date().toLocaleTimeString(),
});
}
}
17 changes: 17 additions & 0 deletions src/services/EncryptService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import bcrypt from 'bcrypt';

export default class EncryptService {
public static async generateHash(
password: string,
salt = 10
): Promise<string> {
return await bcrypt.hash(password, salt);
}

public static async compareHash(
password: string,
hashedPassword: string
): Promise<boolean> {
return await bcrypt.compare(password, hashedPassword);
}
}

0 comments on commit 6678071

Please sign in to comment.