feat: major code refactor, add login and register endpoints with swagger
All checks were successful
Update changelog / changelog (push) Successful in 27s
All checks were successful
Update changelog / changelog (push) Successful in 27s
This commit is contained in:
50
src/middleware/inferUser.ts
Normal file
50
src/middleware/inferUser.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
// Heavily based on:
|
||||
// https://github.com/TomDoesTech/REST-API-Tutorial-Updated/blob/7b5f040e1acd94d267df585516b33ee7e3b75f70/src/middleware/deserializeUser.ts
|
||||
import { get } from 'lodash';
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { verifyJwt } from '../tools/jwt';
|
||||
|
||||
const inferUser = async (
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
) => {
|
||||
|
||||
const accessToken = get(req, 'headers.authorization', '').replace(
|
||||
/^Bearer\s/,
|
||||
''
|
||||
);
|
||||
|
||||
if (!accessToken) return next();
|
||||
|
||||
const { decoded } = verifyJwt(accessToken, 'accessTokenPublicKey');
|
||||
|
||||
// console.log('decoded user:', decoded);
|
||||
|
||||
if (decoded) {
|
||||
res.locals.user = decoded;
|
||||
return next();
|
||||
}
|
||||
|
||||
/*
|
||||
// refresh token handling is not (yet) implemented
|
||||
const refreshToken = get(req, 'headers.x-refresh');
|
||||
|
||||
if (expired && refreshToken) {
|
||||
const newAccessToken = await reIssueAccessToken({ refreshToken });
|
||||
|
||||
if (newAccessToken) {
|
||||
res.setHeader('x-access-token', newAccessToken);
|
||||
}
|
||||
|
||||
const result = verifyJwt(newAccessToken as string, 'accessTokenPublicKey');
|
||||
|
||||
res.locals.user = result.decoded;
|
||||
return next();
|
||||
}
|
||||
*/
|
||||
|
||||
return next();
|
||||
};
|
||||
|
||||
export default inferUser;
|
||||
Reference in New Issue
Block a user