Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | 1x 1x 1x 1x 1x 1x 1x 4x 4x 3x 3x 2x 2x 1x 1x 1x 1x | import debug from 'debug';
import {User, UserDto} from '@models';
import {UnauthorizedError} from '@errors';
import ModelToDtoConverter from '@util/model-to-dto-converter';
import {RequestHandler} from 'express';
/**
* Log method for normal debug logging
*/
const log = debug('group-car:token:controller:log');
/**
* Log method for error logging
*/
const error = debug('group-car:token:controller:error');
/**
* Check if the a user with the username in the jwt still
* exists and if the user does exist respond with ok, if not
* respond with `Unauthorized`. This route let's the frontend check
* if it's still logged in. Or if for example the token expired.
* @param req - Http request, expects payload of jwt to be in `req.user`
* @param res - Http response
* @param next - The next request handler
*/
const tokenController: RequestHandler = (req, res, next) => {
const username = req.user?.username;
if (username !== undefined) {
User.findByUsername(username)
.then((user: User | null) => {
if (user === null || user.deletedAt !== null) {
error('%s in jwt of IP %s doesn\'t exist', username, req.ip);
next(new UnauthorizedError());
} else {
log('IP %s is logged in', req.ip);
res.status(200).send(
ModelToDtoConverter.convert<UserDto>(
user.get({plain: true}),
UserDto,
),
);
}
});
} else {
throw new UnauthorizedError();
}
};
export default tokenController;
|