add auth Handler
This commit is contained in:
parent
acbbba023c
commit
8ff4cc2afd
@ -6,6 +6,7 @@ import { Service } from "typedi";
|
|||||||
import { validateOrReject } from "class-validator";
|
import { validateOrReject } from "class-validator";
|
||||||
import User from "le-coffre-resources/dist/Notary";
|
import User from "le-coffre-resources/dist/Notary";
|
||||||
import { Users } from "@prisma/client";
|
import { Users } from "@prisma/client";
|
||||||
|
import authHandler from "@App/middlewares/AuthHandler";
|
||||||
|
|
||||||
@Controller()
|
@Controller()
|
||||||
@Service()
|
@Service()
|
||||||
@ -17,7 +18,7 @@ export default class UsersController extends ApiController {
|
|||||||
/**
|
/**
|
||||||
* @description Get all users
|
* @description Get all users
|
||||||
*/
|
*/
|
||||||
@Get("/api/v1/super-admin/users")
|
@Get("/api/v1/super-admin/users", [authHandler])
|
||||||
protected async get(req: Request, response: Response) {
|
protected async get(req: Request, response: Response) {
|
||||||
try {
|
try {
|
||||||
//get query
|
//get query
|
||||||
@ -40,7 +41,7 @@ export default class UsersController extends ApiController {
|
|||||||
/**
|
/**
|
||||||
* @description Create a new user
|
* @description Create a new user
|
||||||
*/
|
*/
|
||||||
@Post("/api/v1/super-admin/users")
|
@Post("/api/v1/super-admin/users", [authHandler])
|
||||||
protected async getAddresses(req: Request, response: Response) {
|
protected async getAddresses(req: Request, response: Response) {
|
||||||
try {
|
try {
|
||||||
//init IUser resource with request body values
|
//init IUser resource with request body values
|
||||||
|
20
src/app/middlewares/AuthHandler.ts
Normal file
20
src/app/middlewares/AuthHandler.ts
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
import HttpCodes from "@Common/system/controller-pattern/HttpCodes";
|
||||||
|
import AuthService from "@Services/private-services/AuthService/AuthService";
|
||||||
|
import { NextFunction, Request, Response } from "express";
|
||||||
|
import Container from "typedi";
|
||||||
|
|
||||||
|
export default function authHandler(req: Request, response: Response, next: NextFunction) {
|
||||||
|
const authHeader = req.headers['authorization'];
|
||||||
|
const token = authHeader && authHeader.split(' ')[1];
|
||||||
|
|
||||||
|
if (!token) return response.sendStatus(HttpCodes.UNAUTHORIZED)
|
||||||
|
|
||||||
|
const authService = Container.get(AuthService);
|
||||||
|
authService.verifyAccessToken(token, (err, userPayload) => {
|
||||||
|
if (err) return response.sendStatus(HttpCodes.UNAUTHORIZED);
|
||||||
|
req.body.user = userPayload;
|
||||||
|
return;
|
||||||
|
})
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
|
@ -3,7 +3,6 @@ import BaseService from "@Services/BaseService";
|
|||||||
import "reflect-metadata";
|
import "reflect-metadata";
|
||||||
import { BackendVariables } from "@Common/config/variables/Variables";
|
import { BackendVariables } from "@Common/config/variables/Variables";
|
||||||
import { Service } from "typedi";
|
import { Service } from "typedi";
|
||||||
//import User from "le-coffre-resources/dist/Notary";
|
|
||||||
|
|
||||||
type IdNotTokens = {
|
type IdNotTokens = {
|
||||||
access_token: string;
|
access_token: string;
|
||||||
@ -12,7 +11,8 @@ type IdNotTokens = {
|
|||||||
|
|
||||||
@Service()
|
@Service()
|
||||||
export default class AuthService extends BaseService {
|
export default class AuthService extends BaseService {
|
||||||
private constructor(protected variables: BackendVariables) {
|
|
||||||
|
constructor(protected variables: BackendVariables) {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user