{"size": 1688, "ext": "js", "max_stars_repo_path": "Src/Config/Passport.js", "max_stars_repo_name": "MinikLambrecht/GiroZillaBackend", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "Src/Config/Passport.js", "max_issues_repo_name": "MinikLambrecht/GiroZillaBackend", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "Src/Config/Passport.js", "max_forks_repo_name": "MinikLambrecht/GiroZillaBackend", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 31.8490566038, "max_line_length": 102, "alphanum_fraction": 0.4792654028}
import { JWTSecret } from './Settings';
import { CustomerModel } from '../Models';
import pool from './Database';
import logger from './Winston';

const JwtStrategy = require('passport-jwt').Strategy;
const { ExtractJwt } = require('passport-jwt');

const opts = {
};

opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.secretOrKey = JWTSecret;

module.exports = (passport) =>
{
    passport.use(
        // eslint-disable-next-line camelcase
        new JwtStrategy(opts, (jwt_payload, done) =>
        {
            // eslint-disable-next-line consistent-return
            pool.query(`SELECT * from customers WHERE Customer_ID='${jwt_payload.id}'`, (err, rows) =>
            {
                const customer = new CustomerModel({
                    id: rows[0].id,
                    firstname: rows[0].firstname,
                    lastname: rows[0].lastname,
                    address: rows[0].address,
                    zipcode: rows[0].zipcode,
                    city: rows[0].city,
                    servicesneeded: rows[0].servicesneeded,
                    comment: rows[0].comment,
                    mobile: rows[0].mobile,
                    home: rows[0].home,
                    email: rows[0].email,
                    month: rows[0].month,
                });

                if (!err)
                {
                    if (rows.length > 0)
                    {
                        return done(null, customer);
                    }

                    return done(null, false);
                }

                logger.error(`${err.code} ${err.errno} (${err.sqlState}): ${err.stack}`);
            });
        }),
    );
};