Skip to content

Instantly share code, notes, and snippets.

View fajardwn7's full-sized avatar
🏠
Working from home

Muhammad Fajar fajardwn7

🏠
Working from home
View GitHub Profile
import { Request, Response, NextFunction } from "express";
import { PassportStatic } from "passport";
export function checkRoleWithPassport(roles: string[], passport: PassportStatic, strategy: string, opts: any) {
return function (req: Request, res: Response, next: NextFunction) {
passport.authenticate(strategy, opts, function (err, user) {
if (err) res.status(401).json({ message: "Unauthenticated" });
else if (!user) res.status(401).json({ message: "Unauthenticated" });
else {
if (roles.length == 0)
import passport from "passport";
import passportLocal from "passport-local";
import passportJwt from "passport-jwt";
import { User } from "../models/User";
import { Request, Response, NextFunction } from "express";
import _ from "lodash";
import { KEY_SECRET } from "../util/secrets";
const LocalStrategy = passportLocal.Strategy;
const JWTstrategy = passportJwt.Strategy;
/**
* POST /signin
* Sign in for Token
*/
export const signIn = async (req: Request, res: Response, next: NextFunction) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(422).json(errors.mapped());
}
passport.authenticate("login", async (err, user, info) => {