-
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.
- Loading branch information
Showing
2 changed files
with
50 additions
and
0 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 |
---|---|---|
@@ -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(), | ||
}); | ||
} | ||
} |
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 |
---|---|---|
@@ -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); | ||
} | ||
} |