忧伤的王尓德 2022-11-22 18:32 采纳率: 33.3%
浏览 9
已结题

Express框架登陆成功后获取到res的数据,并将它存到sessionStorage

Express框架,我一个登录请求,用的是表单提交的请求,我想在登陆成功后获取到res的数据,并将它存到sessionStorage中,怎么操作?第一次接触express,真的烦啊
<form name="myform" action="/login?clang=<%= lang %>" method="post" class="login-rt" onsubmit="return sub()">
            <div class="loginList">
                <div style="text-align: right">
                   <select id="lang" name="lang">
                    </select>
                
<div class="loginListRow">
<div class="loginListRow">
<div class="loginListRow">
<div class="pt20"> <input type="submit" class="btn btn-blue btn-login" id="btn" style="word-break:break-all" value=""/>
</form>
用代码块功能插入代码,请勿粘贴截图
router.post('/login', passport.authenticate('local', {
    failureRedirect: '/login',
    failureFlash: true
}), function (req, res, next) {
    let body = {};
    body.operate_name = req.i18n_texts.index.login;
    body.operate_time = new Date();
    body.remark = getClientIp(req);
    body.username = req.user.username;
    knex('t_operate_record').insert(body).then(function (data) {
        //添加网页token
        let token = uuid();
        knex('t_user').update({'token': token}).where({'username': body.username}).then(function (out) {
        })
        console.log(token + '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')
        req.user.token = token;
        const obj = {};
        obj.token = token;
        obj.roleId = req.user.role;
        res.setHeader('rid', req.user.role);
        res.setHeader('tok', token);
        res.redirect('/?clang=' + req.i18n_lang);
    }).catch(function (err) {
        res.send({code: -1, text: err.message});
    })
});

2条回答 默认 最新

  • CSDN专家-showbo 2022-11-22 21:13
    关注

    客户端怎么调用login接口的?ajax还是表单?表单输出js来设置sessionStorage后js进行跳转。而且要存res什么数据?token和role?

                const obj = {};
                obj.token = token;
                obj.roleId = req.user.role;
                res.send(`<script>sessionStorage.set('rid','${req.user.role}');sessionStorage.set('tok','${token}');location.href='/?clang=${ req.i18n_lang}'</script>`)
                //res.setHeader('rid', req.user.role);
                //res.setHeader('tok', token);
                //res.redirect('' + req.i18n_lang);
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

悬赏问题