How to use the passport-jwt.ExtractJwt.fromAuthHeaderAsBearerToken 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 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);
          }
github Aionic-Apps / aionic-core / src / services / auth.ts View on Github external
*  - Basic Auth
 *
 * Pass a strategy when initializing module routes to setup this strategy for the complete module: Example: new UserRoutes('jwt')
 *
 * To setup a strategy for individual endpoints in a module pass the strategy on isAuthorized call
 * Example: isAuthorized('basic')
 */
export class AuthService {
	private defaultStrategy: PassportStrategy;
	private jwtStrategy: JwtStrategy;
	private basicStrategy: BasicAuthStrategy;

	private readonly strategyOptions: StrategyOptions = {
		audience: 'aionic-client',
		issuer: 'aionic-core',
		jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
		secretOrKey: 'aionic-secret-api-key'
	};

	// JWT options
	private readonly signOptions: SignOptions = {
		audience: this.strategyOptions.audience,
		expiresIn: '8h',
		issuer: this.strategyOptions.issuer
	};

	public constructor(defaultStrategy: PassportStrategy = 'jwt') {
		// Setup default strategy -> use jwt if none is provided
		this.defaultStrategy = defaultStrategy;

		this.jwtStrategy = new JwtStrategy(this.strategyOptions);
		this.basicStrategy = new BasicAuthStrategy();
github piyush97 / Slika-API / config / passport.js View on Github external
const JwtStrategy = require('passport-jwt').Strategy;

const ExtractJwt = require('passport-jwt').ExtractJwt;

const mongoose = require('mongoose');

const User = mongoose.model('users');

const keys = require('../config/keys');

const opts = {};
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.secretOrKey = keys.secretOrKey;

module.exports = (passport) => {
  passport.use(
    new JwtStrategy(opts, (jwt_payload, done) => {
      User.findById(jwt_payload.id)
        .then((user) => {
          if (user) {
            return done(null, user);
          }
          return done(null, false);
        })
        .catch(err => console.log(err));
    }),
  );
};
github dengue8830 / node-seed / src / components / auth / authService.ts View on Github external
init(app: Application) {
        app.use(passport.initialize());

        passport.use(new Strategy({
            secretOrKey: config.getJwtSecret(),
            jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken()
        }, (payload: any, done: VerifiedCallback) => done(undefined, payload)));
    }
github vellengs / nestx-server / packages / auth / src / jwt.strategy.ts View on Github external
constructor(
    private readonly authService: AuthService,
    configService: ConfigService
  ) {
    super({
      jwtFromRequest: ExtractJwt.fromExtractors([
        ExtractJwt.fromAuthHeaderAsBearerToken(),
        ExtractJwt.fromUrlQueryParameter("access_token"),
        req => {
          const token = req.cookies && req.cookies.access_token;
          return token;
        }
      ]),
      secretOrKey: configService.get("JWT_SECRET_KEY")
    });
  }
github xmlking / ngx-starter-kit / apps / api / src / app / auth / decorators / raw-token.decorator.ts View on Github external
import { createParamDecorator } from '@nestjs/common';
import { ExtractJwt } from 'passport-jwt';
const extractor = ExtractJwt.fromAuthHeaderAsBearerToken();

export const RawToken = createParamDecorator((data, req) => {
  return extractor(req);
});
github nartc / nest-mean / server / src / shared / auth / strategies / jwt-strategy.service.ts View on Github external
constructor(
        private readonly _authService: AuthService,
        private readonly _configurationService: ConfigurationService,
    ) {
        super({
            jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
            secretOrKey: _configurationService.get(Configuration.JWT_KEY),
        });
    }
github tsmean / tsmean / backend / src / auth / passport / jwt.strategy.ts View on Github external
constructor(private readonly authService: AuthService, @Inject(CONFIG_TOKEN) config: AppProperties) {
    super(
      {
        jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
        passReqToCallback: true,
        secretOrKey: config.token.secret,
      },
      async (req, payload, next) => await this.verify(req, payload, next)
    );
    passport.use(this);
  }
github yuchiu / Slack-Clone / server / src / utils / passport.js View on Github external
import passport from "passport";
import { ExtractJwt, Strategy } from "passport-jwt";

import models from "../models";
import config from "../config";

passport.use(
  new Strategy(
    {
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      secretOrKey: config.JWT_SECRET
    },
    async (jwtPayload, done) => {
      try {
        const user = await models.User.findOne({
          where: { id: jwtPayload.id },
          raw: true
        });
        if (!user) {
          return done(new Error(), false);
        }
        return done(null, user);
      } catch (err) {
        return done(new Error(), false);
      }
    }