-
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.
feat: uncoplying authentication service
- Loading branch information
Showing
6 changed files
with
60 additions
and
23 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,22 @@ | ||
import { | ||
IAuth, | ||
IAuthParams, | ||
IDecodedUser, | ||
} from '../../services/authentication/IAuth'; | ||
|
||
import Env from '../../config/Env'; | ||
import jwt from 'jsonwebtoken'; | ||
|
||
const AUTH_SECRET = Env.app.auth.secret; | ||
const AUTH_EXPIRES_IN = Env.app.auth.expiration; | ||
|
||
export default class JwtAuth implements IAuth { | ||
generateToken(payload: IAuthParams): string { | ||
return jwt.sign(payload, AUTH_SECRET, { | ||
expiresIn: AUTH_EXPIRES_IN, | ||
}); | ||
} | ||
verifyToken(token: string): IDecodedUser { | ||
return jwt.verify(token, AUTH_SECRET) as IDecodedUser; | ||
} | ||
} |
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,3 @@ | ||
import JwtAuth from './JwtAuth'; | ||
|
||
export default new JwtAuth(); |
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
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 |
---|---|---|
@@ -1,33 +1,17 @@ | ||
import { IDecodedUser } from './authentication/IAuth'; | ||
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'; | ||
import authentication from './authentication'; | ||
|
||
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({ | ||
return authentication.generateToken({ | ||
nome: userData.nome, | ||
email: userData.email, | ||
ultimo_login: new Date().toLocaleTimeString(), | ||
}); | ||
} | ||
|
||
public static decodeToken(token: string): IDecodedUser { | ||
return authentication.verifyToken(token); | ||
} | ||
} |
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,14 @@ | ||
import { IUser } from '../../models/user/IUser'; | ||
|
||
export interface IDecodedUser extends Omit<IUser, '_id'> { | ||
id: string; | ||
} | ||
|
||
export interface IAuthParams { | ||
[key: string]: unknown; | ||
} | ||
|
||
export interface IAuth { | ||
generateToken(payload: IAuthParams): string; | ||
verifyToken(token: string): IDecodedUser; | ||
} |
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,2 @@ | ||
import authentication from '../../__dependencies__/authentication'; | ||
export default authentication; |