Skip to content

Instantly share code, notes, and snippets.

@kidap
Created October 7, 2018 02:04
Show Gist options
  • Save kidap/65e67e6446735cecfd0600b348fd2306 to your computer and use it in GitHub Desktop.
Save kidap/65e67e6446735cecfd0600b348fd2306 to your computer and use it in GitHub Desktop.
auth.js
const jwt = require('express-jwt');
const getTokenFromHeaders = (req) => {
const { headers: {authorization} } = req;
if (authorization && authorization.split(' ')[0] === 'Token') {
return authorization.split(' ')[1];
}
return null
}
const auth = {
required: jwt({
secret: 'secret',
userProperty: 'payload',
getToken: getTokenFromHeaders,
}),
optional: jwt({
secret: 'secret',
userProperty: 'payload',
getToken: getTokenFromHeaders,
credentialsRequired: false,
}),
};
module.exports = auth;
@kidap
Copy link
Author

kidap commented Oct 7, 2018

router.get('/current', auth.required, (req, res, next) => {
    console.log(req)
    console.log("-----------kp---------------")
    const { payload: { id }} = req;

    console.log(payload)
    console.log("-----------kp---------------")
    console.log(id)

    return Users.findById(id)
    .then((user => {
        if (!user) {
            return res.sendStatus(400);
        }

        return res.json({ user: user.toAuthJSON() });
    }))
})
/Users/karlopagtakhan/Programming/node/passport/node_modules/express/lib/router/route.js:202
        throw new Error(msg);
        ^

Error: Route.get() requires a callback function but got a [object Undefined]
    at Route.(anonymous function) [as get] (/Users/karlopagtakhan/Programming/node/passport/node_modules/express/lib/router/route.js:202:15)
    at Function.proto.(anonymous function) [as get] (/Users/karlopagtakhan/Programming/node/passport/node_modules/express/lib/router/index.js:510:19)
    at Object.<anonymous> (/Users/karlopagtakhan/Programming/node/passport/routes/api/users.js:80:8)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Module.require (module.js:587:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/Users/karlopagtakhan/Programming/node/passport/routes/api/index.js:4:22)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment