# All files / nodejs-homework-API/config passport.js

80% Statements 12/15
50% Branches 2/4
100% Functions 1/1
80% Lines 12/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    281x
    1x
    1x
    1x
    1x

    1x

    1x

    11x
    11x
    11x

    11x

    11x

     const passport = require("passport");
    const UserModel = require("../model/user");
    const { Strategy, ExtractJwt } = require("passport-jwt");
    require("dotenv").config();
    const SECRET_KEY = process.env.JWT_SECRET;

    const params = {
      secretOrKey: SECRET_KEY,
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
    };

    passport.use(
      new Strategy(params, async (payload, done) => {
        try {
          const user = await UserModel.findById(payload.id);
          Iif (!user) {
            return done(new Error("User not found"));
          }
          Iif (!user.token) {
            return done(null, false);
          }
          return done(null, user);
        } catch (err) {
          done(err);
        }
      })
    );