diff --git a/src/Middlewares/authChecker.ts b/src/Middlewares/authChecker.ts index be27d2c6b6d952c4d0d8072439a01d300a7c3783..90a6db3663de64087217dd26d355d1a0a739752f 100644 --- a/src/Middlewares/authChecker.ts +++ b/src/Middlewares/authChecker.ts @@ -2,15 +2,15 @@ import { Response, NextFunction } from 'express'; import { validate } from '../Services/UserAPI'; // eslint-disable-next-line @typescript-eslint/no-explicit-any -const authChecker = (req: any, res: Response, next: NextFunction): void => { - const token = req.headers.authorization; - const userId = validate(token); - if (userId === -1) { +const authChecker = async (req: any, res: Response, next: NextFunction): void => { + try { + const token = req.headers.authorization; + const userId = await validate(token); + req.user_id = userId; + next(); + } catch (error) { res.status(401).send({ message: 'auth failed' }); - return; } - req.user_id = userId; - next(); }; export default authChecker; diff --git a/src/Services/UserAPI.ts b/src/Services/UserAPI.ts index faa16e92eba0a048aedfc305b9999781c254c643..adf284a58c70ee81e29c4f896629e5477d35ad6c 100644 --- a/src/Services/UserAPI.ts +++ b/src/Services/UserAPI.ts @@ -11,16 +11,8 @@ const instance = axios.create({ baseURL: process.env.AUTH_BASE_URL, }); -export const validate = (token: string): number => { - let id = -1; - instance.post('/validate', { token }) - .then((res) => { - id = (res.data as any).userId as number; - }) - .catch((err) => { - throw (err); - // if needed implement later - }); +export const validate = async (token: string) => { + const id = await instance.post('/validate', { token }); return id; };