How to use passport-jwt - 10 common examples

To help you get started, we’ve selected a few passport-jwt examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github DefinitelyTyped / DefinitelyTyped / types / passport-jwt / passport-jwt-tests.ts View on Github external
if (err) {
            return done(err, false);
        }
        if (user) {
            done(null, user);
        } else {
            done(null, false, {message: 'foo'});
            // or you could create a new account
        }
    });
}));

opts.jwtFromRequest = ExtractJwt.fromHeader('x-api-key');
opts.jwtFromRequest = ExtractJwt.fromBodyField('field_name');
opts.jwtFromRequest = ExtractJwt.fromUrlQueryParameter('param_name');
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderWithScheme('param_name');
opts.jwtFromRequest = ExtractJwt.fromExtractors([ExtractJwt.fromHeader('x-api-key'), ExtractJwt.fromBodyField('field_name'), ExtractJwt.fromUrlQueryParameter('param_name')]);
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.jwtFromRequest = (req: Request) => { return req.query.token; };
opts.secretOrKey = new Buffer('secret');

declare function findUser(condition: {id: string}, callback: (error: any, user :any) => void): void;
github DefinitelyTyped / DefinitelyTyped / types / passport-jwt / passport-jwt-tests.ts View on Github external
passport.use(JwtStrategy.name, new JwtStrategy(opts, function(jwt_payload, done) {
    findUser({id: jwt_payload.sub}, function(err, user) {
        if (err) {
            return done(err, false);
        }
        if (user) {
            done(null, user);
        } else {
            done(null, false, {message: 'foo'});
            // or you could create a new account
        }
    });
}));

opts.jwtFromRequest = ExtractJwt.fromHeader('x-api-key');
opts.jwtFromRequest = ExtractJwt.fromBodyField('field_name');
opts.jwtFromRequest = ExtractJwt.fromUrlQueryParameter('param_name');
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderWithScheme('param_name');
opts.jwtFromRequest = ExtractJwt.fromExtractors([ExtractJwt.fromHeader('x-api-key'), ExtractJwt.fromBodyField('field_name'), ExtractJwt.fromUrlQueryParameter('param_name')]);
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.jwtFromRequest = (req: Request) => { return req.query.token; };
opts.secretOrKey = new Buffer('secret');

declare function findUser(condition: {id: string}, callback: (error: any, user :any) => void): void;
github DefinitelyTyped / DefinitelyTyped / types / passport-jwt / passport-jwt-tests.ts View on Github external
return done(err, false);
        }
        if (user) {
            done(null, user);
        } else {
            done(null, false, {message: 'foo'});
            // or you could create a new account
        }
    });
}));

opts.jwtFromRequest = ExtractJwt.fromHeader('x-api-key');
opts.jwtFromRequest = ExtractJwt.fromBodyField('field_name');
opts.jwtFromRequest = ExtractJwt.fromUrlQueryParameter('param_name');
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderWithScheme('param_name');
opts.jwtFromRequest = ExtractJwt.fromExtractors([ExtractJwt.fromHeader('x-api-key'), ExtractJwt.fromBodyField('field_name'), ExtractJwt.fromUrlQueryParameter('param_name')]);
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.jwtFromRequest = (req: Request) => { return req.query.token; };
opts.secretOrKey = new Buffer('secret');

declare function findUser(condition: {id: string}, callback: (error: any, user :any) => void): void;
github DefinitelyTyped / DefinitelyTyped / types / passport-jwt / passport-jwt-tests.ts View on Github external
findUser({id: jwt_payload.sub}, function(err, user) {
        if (err) {
            return done(err, false);
        }
        if (user) {
            done(null, user);
        } else {
            done(null, false, {message: 'foo'});
            // or you could create a new account
        }
    });
}));

opts.jwtFromRequest = ExtractJwt.fromHeader('x-api-key');
opts.jwtFromRequest = ExtractJwt.fromBodyField('field_name');
opts.jwtFromRequest = ExtractJwt.fromUrlQueryParameter('param_name');
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderWithScheme('param_name');
opts.jwtFromRequest = ExtractJwt.fromExtractors([ExtractJwt.fromHeader('x-api-key'), ExtractJwt.fromBodyField('field_name'), ExtractJwt.fromUrlQueryParameter('param_name')]);
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.jwtFromRequest = (req: Request) => { return req.query.token; };
opts.secretOrKey = new Buffer('secret');

declare function findUser(condition: {id: string}, callback: (error: any, user :any) => void): void;
github DefinitelyTyped / DefinitelyTyped / types / passport-jwt / passport-jwt-tests.ts View on Github external
passport.use(JwtStrategy.name, new JwtStrategy(opts, function(jwt_payload, done) {
    findUser({id: jwt_payload.sub}, function(err, user) {
        if (err) {
            return done(err, false);
        }
        if (user) {
            done(null, user);
        } else {
            done(null, false, {message: 'foo'});
            // or you could create a new account
        }
    });
}));

opts.jwtFromRequest = ExtractJwt.fromHeader('x-api-key');
opts.jwtFromRequest = ExtractJwt.fromBodyField('field_name');
opts.jwtFromRequest = ExtractJwt.fromUrlQueryParameter('param_name');
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderWithScheme('param_name');
opts.jwtFromRequest = ExtractJwt.fromExtractors([ExtractJwt.fromHeader('x-api-key'), ExtractJwt.fromBodyField('field_name'), ExtractJwt.fromUrlQueryParameter('param_name')]);
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.jwtFromRequest = (req: Request) => { return req.query.token; };
opts.secretOrKey = new Buffer('secret');

declare function findUser(condition: {id: string}, callback: (error: any, user :any) => void): void;
github DefinitelyTyped / DefinitelyTyped / passport-jwt / passport-jwt-tests.ts View on Github external
passport.use(new JwtStrategy(opts, function(jwt_payload, done) {
    findUser({id: jwt_payload.sub}, function(err, user) {
        if (err) {
            return done(err, false);
        }
        if (user) {
            done(null, user);
        } else {
            done(null, false, {message: 'foo'});
            // or you could create a new account
        }
    });
}));

opts.jwtFromRequest = ExtractJwt.fromHeader('x-api-key');
opts.jwtFromRequest = ExtractJwt.fromBodyField('field_name');
opts.jwtFromRequest = ExtractJwt.fromUrlQueryParameter('param_name');
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderWithScheme('param_name');
opts.jwtFromRequest = (req: Request) => { return req.query.token; };

declare function findUser(condition: {id: string}, callback: (error: any, user :any) => void): void;
github DefinitelyTyped / DefinitelyTyped / types / passport-jwt / passport-jwt-tests.ts View on Github external
/// 
'use strict';

import { Strategy as JwtStrategy, ExtractJwt, StrategyOptions } from 'passport-jwt';
import {Request} from 'express';
import * as passport from 'passport';

let opts: StrategyOptions = {
    jwtFromRequest: ExtractJwt.fromAuthHeader(),
    secretOrKey: 'secret',
    issuer: "accounts.example.com",
    audience: "example.org"
};

passport.use(JwtStrategy.name, new JwtStrategy(opts, function(jwt_payload, done) {
    findUser({id: jwt_payload.sub}, function(err, user) {
        if (err) {
            return done(err, false);
        }
        if (user) {
            done(null, user);
        } else {
            done(null, false, {message: 'foo'});
            // or you could create a new account
        }
    });
}));

opts.jwtFromRequest = ExtractJwt.fromHeader('x-api-key');
opts.jwtFromRequest = ExtractJwt.fromBodyField('field_name');
opts.jwtFromRequest = ExtractJwt.fromUrlQueryParameter('param_name');
github peerism / peerai-api / middleware / auth.js View on Github external
req.user = user;
    next();
  })
}

const jwtOptions = {
  // Authorization: Bearer in request headers
  jwtFromRequest: PassportJwt.ExtractJwt.fromAuthHeaderAsBearerToken(),
  secretOrKey: JWT_SECRET,
  // Algorithms used to sign in
  algorithms: [JWT_ALGORITHM]
}

// Passport JWT Strategy triggered by validateJWTWithPassportJWT 
// https://www.npmjs.com/package/passport-jwt
passport.use(new PassportJwt.Strategy(jwtOptions, 
  // Post-Verified token - https://www.npmjs.com/package/passport-jwt
  (jwtPayload, done) => {
    console.log('PassportJwt Strategy being processed');
    // Find user in MongoDB using the `id` in the JWT
    User.findById(jwtPayload.sub)
    // User.findById(jwtPayload._doc._id)
      .then((user) => {
        if (user) { 
          done(null, user); 
        } else {
          done(null, false); 
        }
      })
      .catch((error) => {
        done(error, false);
      })
github alqamabinsadiq / node-api-kit / server / verify.js View on Github external
let passport = require('passport');
let LocalStrategy = require('passport-local').Strategy;
let User = require('../features/users/user.model');
let JwtStrategy = require('passport-jwt').Strategy;
let ExtractJwt = require('passport-jwt').ExtractJwt;
let jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens
let FacebookTokenStrategy = require('passport-facebook-token');
let config = require('../config/config');
const Iron = require('iron');

// Returns the sign jwt
exports.getToken = (user, expiresIn) => {
  return jwt.sign(user, config.secretKey,
    { expiresIn: expiresIn || 3600 });
};

// Check whether the user has admin privileges or not.
exports.verifyAdmin = (req, res, next) => {
  console.log(req.user);
  if (req.user.admin) {
    next();
github yalla-coop / connect5 / v1 / passport.js View on Github external
const JwtStrategy = require("passport-jwt").Strategy;
const ExtractJwt = require("passport-jwt").ExtractJwt;
const mongoose = require("mongoose");
const Trainer = require("../server/database/models/Trainer");
// const keys = require('../config/keys');
require("env2")("./config/config.env");
// set up options
const opts = {};
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.secretOrKey = process.env.secretOrKey;
// opts.secretOrKey = keys.secretOrKey;

module.exports = (passport) => {
  passport.use(
    // use mongoose findById method and pass it the id stemming from the bearer toke auth object named jwt payload

    new JwtStrategy(opts, (jwt_payload, done) => {
      console.log("JWT", jwt_payload);
      Trainer.findById(jwt_payload.id)
        .then((trainer) => {
          // use done function without error and either user or false

          if (trainer) {
            return done(null, trainer);
          }