How to use the passport-jwt.ExtractJwt.fromHeader function in passport-jwt

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
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 stw-services / react-api-server / services / passport.js View on Github external
}
      if (!isMatch)
      {
        return done(null, false);
      }
       console.log('user'+user);
       return done(null,user);


    });
  });
});

// Setup options for JWT Strategy
const jwtOptions = {
  jwtFromRequest: ExtractJwt.fromHeader('authorization'),
  secretOrKey: config.secret
};

// Create JWT strategy
const jwtLogin = new JwtStrategy(jwtOptions, function(payload, done) {
  // See if the user ID in the payload exists in our database
  // If it does, call 'done' with that other
  // otherwise, call done without a user object
  User.findById(payload.sub, function(err, user) {

    if (err) { return done(err, false); }

    if (user) {
      done(null, user);
    } else {
      done(null, false);
github joelseq / SourceGrade / build / config / passport.js View on Github external
}

        if (!isMatch) {
          return done(null, false);
        }

        return done(null, user);
      });
    });
  }));

  //==========================
  // JWT Strategy
  //==========================
  var jwtOptions = {
    jwtFromRequest: ExtractJwt.fromHeader('authorization'),
    secretOrKey: process.env.SECRET
  };

  passport.use(new JwtStrategy(jwtOptions, function (payload, done) {
    User.findById(payload.sub, function (err, user) {
      if (err) {
        return done(err, false);
      }

      if (user) {
        done(null, user);
      } else {
        done(null, false);
      }
    });
  }));
github penta-jelly / re-radio / server / src / radio / auth / jwt.strategy.ts View on Github external
constructor(private readonly configService: ConfigService, private readonly authService: AuthService) {
    super({
      // Only take the JWT token from either "authorization" or "Authorization" headers
      jwtFromRequest: ExtractJwt.fromExtractors([
        ExtractJwt.fromHeader('authorization'),
        ExtractJwt.fromHeader('Authorization'),
      ]),
      secretOrKey: configService.get(EnvVariables.JWT_SECRET),
    });
  }
github strues / boldr / src / api / routes / auth / providers / jwt.js View on Github external
import passport from 'passport';

const JwtStrategy = require('passport-jwt').Strategy;
const ExtractJwt = require('passport-jwt').ExtractJwt;
const config = require('../../../config/config');

const sessionConfig = config.get('session');

const jwtOptions = {
  jwtFromRequest: ExtractJwt.fromHeader('authorization'),
  secretOrKey: sessionConfig.secret,
};

export default function configureJwt(User) {
  passport.use(new JwtStrategy(jwtOptions, async (payload, done) => {
    const NOW = new Date().getTime();
    if (payload.exp < NOW) {
      return done(null, false);
    }
    const user = await User.query().findById(payload.sub).first();
    if (!user) {
      return done(null, false, { message: 'This email is not registered.' });
    } else {
      user.stripPassword();
      return done(null, user);
    }
github TreeGateway / tree-gateway / src / lib / authentication / strategies / jwt.ts View on Github external
function getExtractor(extractor: string, param: string) {
    switch(extractor) {
        case 'header': return ExtractJwt.fromHeader(param);
        case 'queryParam': return ExtractJwt.fromUrlQueryParameter(param);
        case 'authHeader': return ExtractJwt.fromAuthHeaderWithScheme(param);
        case 'bodyField': return ExtractJwt.fromBodyField(param);
        case 'cookie': return (req)=>{
            let token = null;
            if (req && req.cookies)
            {
                token = req.cookies[param];
            }
            return token;
        };            
    }

    return ExtractJwt.fromAuthHeader();
}
github DimiMikadze / node-redux-auth / server / src / services / passport.js View on Github external
if (!user) { return done(null, false); }

    user.comparePassword(password, (err, isMatch) => {
      if (err) { return done(err); }

      if (!isMatch) { return done(null, false); }

      if (user.role < 1) { return done(null, false); }

      return done(null, user);
    });
  });
});

const jwtOptions = {
  jwtFromRequest: ExtractJwt.fromHeader('authorization'),
  secretOrKey: dbConfig.secret,
};

const jwtLogin = new JwtStrategy(jwtOptions, (payload, done) => {
  User.findById(payload.sub, (err, user) => {
    if (err) { return done(err, false); }

    if (user) {
      done(null, user);
    } else {
      done(null, false);
    }
  });
});

passport.use(jwtLogin);
github johnnyvf24 / hellochess-v2 / server / services / passport.js View on Github external
}
            if(!isMatch) {
                return done(null, false);
            }

            return done(null, user);
        });

    }).catch((e) => {
        done(e);
    });
});

//options for JWT strategy
const jwtOptions= {
    jwtFromRequest: ExtractJwt.fromHeader('x-auth'),
    secretOrKey: config.secret
};

const jwtLogin = new JwtStrategy(jwtOptions, function(payload, done) {

    User.findById(payload.sub, function(err, user) {
        if(err) {
            return done(err, false);
        }

        if(user) {
            done(null, user);
        } else {
            done(null, false);
        }
    });