我一直在
res.jwt is not a function
我已安装jwt-express
并按原样导入
import jwt from 'jwt-express'
这是我的 auth.js
import Account from '../services/account.js'
import env from 'dotenv'
import _ from 'lodash'
const dotenv = env.config();
module.exports = {
/**
* Process the user login, generating and returning a token if successful.
*
* @return {res}
*/
async login(req, res, next) {
try {
let origin = req.headers.origin;
let accounts = await Account.getAccounts();
let account = _.find(accounts, {
'email_address' : req.body.username,
'password' : req.body.password
});
if (!account) {
res.send('Username/Password Wrong');
}
// res.send(account);
let authentication = res.jwt({
'email': account.email_address,
'id': account.account_id
});
res.send(authentication);
} catch (error) {
next(error)
}
}
};
index.js
import express from 'express'
import favicon from 'serve-favicon'
import path from 'path'
import bodyParser from 'body-parser'
import bluebird from 'bluebird'
import jwt from 'jwt-express'
import env from 'dotenv'
//Controllers
import fortinetController from './controllers/fortinet'
import authController from './controllers/auth.js'
//Logger
import logger from './config/logger.js'
//Constant
const router = express.Router();
const app = express();
const PORT = 3000;
const dotenv = env.config();
Promise = bluebird;
app.use(bodyParser.urlencoded({extended: true }));
app.use(bodyParser.json());
app.use(router)
app.use(express.static('public'))
app.use(favicon(path.join(__dirname,'public','favicon.ico')))
app.use(jwt.init('CARWASH', {cookies: false }));
router.get('/', (req,res) => {
res.send('Welcome to the backend provisioning daemon to program FortiManager')
});
router.post('/login', authController.login);
//Fortinet
router.post('/fortinet/login', fortinetController.login);
router.post('/fortinet/getSessionTimeOut', fortinetController.getSessionTimeOut);
router.post('/fortinet/logout', fortinetController.logout);
//Error handling function
app.use((err,req,res,next) => {
console.error(err.stack)
res.status(500).send(`Red alert! Red alert!: ${err.stack}`)
logger.error(`${req.method} ${req.url} - ${err.log || err.message}`);
});
app.listen(PORT, () => {
console.log(`Your server is running on ${PORT}`)
}
);
如何继续进行调试?
我现在对任何建议持开放态度。
任何提示/建议/帮助都将非常感谢!
我试图添加此
console.log(jwt);
我得到了
[nodemon] 1.17.3
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `babel-node ./index.js`
{ active: [Function: active],
clear: [Function: clear],
create: [Function: create],
init: [Function: init],
options:
{ cookie: 'jwt-express',
cookieOptions: { httpOnly: true },
cookies: false,
refresh: true,
reqProperty: 'jwt',
revoke: [Function: revoke],
signOptions: {},
stales: 900000,
verify: [Function: verify],
verifyOptions: {} },
require: [Function: require],
valid: [Function: valid] }
Your server is running on 3000
答案 0 :(得分:2)
express-jwt
。express-jwt
完全错误。让我们逐步了解每一点。
我不确定您认为在文档here声明只需执行jwt.init(...)
时需要致电jwt(...)
的原因。因此,您需要进行以下更改:
更改
app.use(jwt.init('CARWASH', {cookies: false }));
要
app.use(jwt({secret: 'CARWASH'}));
不存在cookies
选项,不确定从哪里获得。
现在express-jwt
只会处理JWT的验证。它确实 生成JWT,正如您在auth.js
中所做的那样。
为了生成JWT,您需要另一个模块:jsonwebtoken
。然后,您将使用auth.js
中的模块,如下所示:
import jwt from "jsonwebtoken";
// ...
module.export = {
async login(req, res, next) {
try {
// ... auth logic omitted
// Here we generate the JWT
// Make sure the JWT secret is the SAME secret you used for express-jwt
let authentication = jwt.sign({
'email': account.email_address,
'id': account.account_id
}, 'CARWASH');
res.send(authentication);
}
catch (error) {
next(error);
}
}
}