Skip to content

Instantly share code, notes, and snippets.

@robozavri
Last active September 10, 2020 13:11
Show Gist options
  • Save robozavri/5e84dac95394865ce45b5d037fd357fa to your computer and use it in GitHub Desktop.
Save robozavri/5e84dac95394865ce45b5d037fd357fa to your computer and use it in GitHub Desktop.
#node.js
import { Request, Response, NextFunction } from 'express';
import { verify, sign } from 'jsonwebtoken';
import * as User from '../api/users/user.dao';
import config from '../config/environment';
import { roles } from '../constants/user';
import { UnauthorizedError, ResourceNotFoundError, ValidationError } from '../errors';
import * as cookie from 'cookie';
export async function setUser(req: Request, res: Response, next: NextFunction) {
try {
const { token }: any = req.headers;
if (!token) {
req.user = { role: roles.GUEST };
return next();
}
const { _id }: any = verify(token, config.jwt.secret);
req.user = await User.getById(_id);
next();
} catch (e) {
if (e.name === 'TokenExpiredError' || e.name === 'JsonWebTokenError' || e instanceof ResourceNotFoundError) {
req.user = { role: roles.GUEST };
next();
} else {
next(e);
}
}
}
export function isSigned(req: Request, res: Response, next: NextFunction) {
const {_id, role} = req.user;
if (role !== roles.GUEST) {
next();
} else {
next(new UnauthorizedError(`User (id ${_id}) is not signed in`));
}
}
export function isAdmin(req: Request, res: Response, next: NextFunction) {
const {_id, role} = req.user;
if (role === roles.ADMIN) {
next();
} else {
next(new UnauthorizedError(`User (id ${_id}) is not admin`));
}
}
export function signToken(data: any) {
return sign({_id: data._id}, config.jwt.secret, {expiresIn: config.jwt.expiresIn});
}
export function socketCookieParser(socket: any, next: NextFunction) {
if (socket.request.headers.cookie && typeof socket.request.headers.cookie === 'string') {
socket.cookie = cookie.parse(socket.request.headers.cookie);
} else {
socket.cookie = socket.request.headers.cookie;
}
next();
}
export async function socketSetUser(socket: any, next: NextFunction) {
try {
const { token }: any = socket.handshake.query;
if (token) {
const { _id }: any = verify(token, config.jwt.secret);
socket.user = await User.getById(_id);
next();
} else {
throw new ValidationError('Unauthorized User');
}
} catch (e) {
next(e);
}
}
////////////////////////
import { Request, Response, NextFunction } from 'express';
import { verify, sign } from 'jsonwebtoken';
import { UserModel } from './user';
// import * as cookie from 'cookie';
export async function setUser(req: any, res: Response, next: NextFunction) {
try {
const { token }: any = req.headers;
if (!token) {
req.user = { role: 'GUEST' };
return next();
}
const { _id }: any = verify(token, '7b7dc72d-7c93-4772-90ed-f2dd65f123ef');
req.user = await UserModel.find({_id: _id});
next();
} catch (e) {
if (e.name === 'TokenExpiredError' || e.name === 'JsonWebTokenError' ) {
next();
} else {
next(e);
}
}
}
export function isSigned(req: any, res: Response, next: NextFunction) {
const {_id, role} = req.user;
if (role !== 'GUEST') {
next();
} else {
res.sendStatus(401);
next();
}
}
export function signToken(data: any) {
return sign({_id: data._id},
'7b7dc72d-7c93-4772-90ed-f2dd65f123ef',
{
expiresIn: 30 * 24 * 60 * 60, // 30 days
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment