Skip to content

Commit

Permalink
feat: uncoplying authentication service
Browse files Browse the repository at this point in the history
  • Loading branch information
lucsimao committed Oct 4, 2021
1 parent f3c9f71 commit 4795c2d
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 23 deletions.
22 changes: 22 additions & 0 deletions src/__dependencies__/authentication/JwtAuth.ts
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;
}
}
3 changes: 3 additions & 0 deletions src/__dependencies__/authentication/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import JwtAuth from './JwtAuth';

export default new JwtAuth();
12 changes: 12 additions & 0 deletions src/config/Env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ interface IAppEnv {
database: string;
ssl: string;
};
auth: {
secret: string;
expiration: string;
};
cryptography: { salt: number };
}

class Envs {
Expand All @@ -32,6 +37,13 @@ class Envs {
database: process.env.DATABASE_NAME || 'test',
ssl: process.env.DATABASE_SSL || 'false',
},
auth: {
secret: process.env.AUTH_SECRET || 'secret',
expiration: process.env.AUTH_EXPIRATION || '30m',
},
cryptography: {
salt: Number(process.env.CRYPTOGRAPHY_SALT) || 10,
},
};
}

Expand Down
30 changes: 7 additions & 23 deletions src/services/AuthService.ts
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);
}
}
14 changes: 14 additions & 0 deletions src/services/authentication/IAuth.ts
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;
}
2 changes: 2 additions & 0 deletions src/services/authentication/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import authentication from '../../__dependencies__/authentication';
export default authentication;

0 comments on commit 4795c2d

Please sign in to comment.