在nuxt上有一个现成的站点。我正在寻找一种实现授权的方法。我发现使用express.js steam-login进行授权的包,一切正常..现在如何路由/login / logout
等开始工作以及如何在渲染时使用关于nuxt用户的接收数据?我试过中间件但没有发生任何事。谢谢。
有获取数据的例子
app.use(steam.middleware({
realm: 'http://localhost:3000/',
verify: 'http://localhost:3000/verify',
apiKey: "api key"}));
app.get('/authenticate', steam.authenticate(), function(req, res) {
res.redirect('/');});
在那里我们可以使用nuxt构建器,我没有找到将这些数据传输到nuxt并在模板中使用它的方法。并且路径nuxt重叠路径表达
const nuxt = new Nuxt()
nuxt.build()
.then(() => {
app.use(nuxt.render)
app.listen(3000)})
用serverMiddleware解决了这个问题。
在nuxt.config中添加了新的自定义中间件
serverMiddleware: [
'~/api/index'
]
app.use(require('express-session')({resave:false,saveUninitialized:false,secret:'a secret'}));
app.use(steam.middleware({
realm: 'http://localhost:3000/',
verify: 'http://localhost:3000/login/verify',
apiKey: "apiKey"}
));
app.get('/', function(req, res) {
res.send(req.user == null ? 'no user' : req.user).end();
});
app.get('/authenticate', steam.authenticate(), function(req, res) {
res.redirect('/login/');
});
app.get('/logout', steam.enforceLogin('/'), function(req, res) {
req.logout();
res.redirect('/');
});
module.exports = {
path: '/login/',
handler: app
};
在我的模板中使用axios从/ login route获取此数据。谢谢