import {Strategy as JwtStrategy, ExtractJwt } from 'passport-jwt'
import config from './config'
import { tokenTypes } from './tokens';
import { User } from '../models';


const jwtOptions = {
  secretOrKey: config.jwt.secret,
  jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
};

const jwtVerify = async (payload, done) => {
  try {
    if (payload.type !== tokenTypes.ACCESS) {
      throw new Error('Invalid token type');
    }
    const user = await User.findOne({_id: payload.sub});
    if (!user) {
      return done(null, false);
    }
    done(null, user);
  } catch (error) {
    done(error, false);
  }
};

export default new JwtStrategy(jwtOptions, jwtVerify);
