簡體   English   中英

基於 Cookie 的身份驗證適用於桌面瀏覽器,但不適用於移動瀏覽器

[英]Cookie-based authentication works on desktop browsers but not on mobile ones

我在后端使用 express.js 和 Sequelize,我的身份驗證路徑如下所示:

exports.signin = (req, res) => {
    Admin.findOne({
        where: {
            username: req.body.username
        }
    })
        .then(admin => {
            if (!admin) {
                return res.status(404).send({
                    ERR: USER_NOT_FOUND
                });
            }

            var passwordIsValid = bcrypt.compareSync(
                req.body.password,
                admin.password
            );

            if (!passwordIsValid) {
                return res.status(401).send({
                    ERR: WRONG_PASSWORD
                });
            }

            const tokenBody = {
                id: admin.id,
                isMaster: (admin.username == "Master")
            };

            var token = jwt.sign(tokenBody, process.env.JWT_SECRET, {
                expiresIn: tokenExpirationTime
            });

            res.cookie('auth_token', token, {
                // 'tokenExpirationTime' is in seconds (as required for JWT), but maxAge 
                // expects milliseconds, so it must be multiplied by 1000:
                maxAge: tokenExpirationTime * 1000,
                httpOnly: true,
                secure: true
            });

            res.status(200).send({ success: true });
        })
        .catch(err => {
            console.error(err);
            res.status(500).send({
                ERR: INTERNAL_SERVER_ERROR
            });
        });
};

我在前端使用 Ejs,我的登錄代碼是:

const signInUrl = '/api/auth/signin';
let form = document.getElementById('login');

form.onsubmit = async (e) => {

    e.preventDefault();
    let data = new FormData(form);
    data = {
        username: data.get('username'),
        password: data.get('password')
    };

    axios.defaults.withCredentials = true

    axios.post(signInUrl, data, { withCredentials: true })
        .then(response => {
            // Redirect to the home page:
            if (response.data.success)
                window.location.replace('/');
            else // console.log(response.data);
                informError(0);
        })
        .catch(error => {
            console.error(error);
            if (error.response) {
                if (error.response.data && error.response.data.ERR)
                    informError(error.response.data.ERR);
                else
                    informError(0);
            } else {
                informError(1);
            }
        });
}

現在的問題是該系統在桌面瀏覽器上完全正常(我已經使用它一個多月了,它通過了各種測試)但在移動瀏覽器上卻沒有!

在移動設備上,我登錄並成功將我重定向到主頁,但隨后我仍在使用登錄按鈕,表明我未登錄。此外,我無法訪問任何受保護的路由,收到一個“需要登錄”錯誤!

任何幫助將不勝感激!

所以對於那些有同樣問題的人,對我來說,當我嘗試使用如下 URL 從我的手機訪問服務器(在我的筆記本電腦“localhost:3000”上)時出現問題: http://192.168.0.107:3000 : http://192.168.0.107:3000 :3000 . 我發現問題與被保護的 cookie ( secure: true ) 相關,移動瀏覽器沒有使用它,因為我使用的是 http 而不是 https,而是桌面瀏覽器正常使用。 所以我想在 https 協議的域上部署應用程序將解決這個問題。 但出於測試目的,只需刪除secure: true部分,它就會正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM