我有一个在 www.website.com/users/login 上运行的 api。我想在我的 nodejs 前端使用这个 api。我想将从 /login 收到的令牌传递给所有其他路由。在这里,我试图将登录后收到的令牌传递到注销路由。Api注销代码-router.post('/users/logout',auth, async(req,res)=>{ try{ req.user.tokens=req.user.tokens.filter((token)=>{ return token.token !==req.token }) await req.user.save() res.send() }catch(e){ res.status(500).send() }})客户端代码app.post('/users/login', function (req, res) {var email = req.body.email;var password = req.body.password;var form = { email, password}request.post({ url: "https://website.com/users/login", body: form, json: true}, function (error, response, body) { console.log('error:', error) console.log('body:', body.errmsg); console.log(body); if (body.user) { let auth = body.token console.log(auth) //I want to pass this auth to /logout } else { res.render('login', { email, password }) } })注销路由。我想将令牌传递给注销路由,以便用户可以注销?app.post('/users/logout', function (req, res, next) {request.post({ url: "https://website.com/users/logout", headers: { 'Authorization': auth }, }, function (error, response, body) { console.log('error:', error) console.log('response:', response); console.log('body:',body); })})请帮助我了解如何使用从登录收到的令牌到其他路由。我让邮递员从 auth 继承,但我很难使用这个 api。
添加回答
举报
0/150
提交
取消