Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何向已登录的用户显示特定页面_Javascript_Node.js_Express_Firebase Authentication - Fatal编程技术网

Javascript 如何向已登录的用户显示特定页面

Javascript 如何向已登录的用户显示特定页面,javascript,node.js,express,firebase-authentication,Javascript,Node.js,Express,Firebase Authentication,我正在使用nodejs服务器根据请求呈现ejs文件。我只希望经过身份验证的用户请求他/她的个人资料。如果一个未注册的用户发出该请求,我想将该用户重定向到登录页面 以下是在节点服务器上实现的逻辑 router.get('/profile',function(req,res){ const token = req.header('Authorization').replace('Bearer ','') admin.auth().verifyIdToken(token) .t

我正在使用nodejs服务器根据请求呈现ejs文件。我只希望经过身份验证的用户请求他/她的个人资料。如果一个未注册的用户发出该请求,我想将该用户重定向到登录页面

以下是在节点服务器上实现的逻辑

router.get('/profile',function(req,res){
    const token = req.header('Authorization').replace('Bearer ','')
    admin.auth().verifyIdToken(token)
    .then(function(decodedToken) {
    let uid = decodedToken.uid;
    console.log(uid)
    res.redirect('profile.ejs');
    }).catch(function(error) {
    // Handle error
    console.log("error")
    res.status(401).send({"error":"Please Authenticate first"})
    });
})
以下是客户端如何请求该页面

$('#user-info').click(function () {
    firebase.auth().currentUser.getIdToken(/* forceRefresh */ true).then(function(idToken) {
        // Send token to your backend via HTTPS
        console.log(idToken)
        fetch('/profile',{
            method: "get",
            headers: {
                'Content-type':'application/json',
                'Authorization': `Bearer ${idToken}`
            }
        }).then((data)=>{
            console.log(data)
        })
      }).catch(function(error) {
        // Handle error
        checkIfLoggedIn()
    });
})

调用
getIdToken
时出现现有的catch句柄错误。请添加一个特定的
.catch()
以相应地处理
获取的错误情况,例如:

    fetch('/profile',{
        method: "get",
        headers: {
            'Content-type':'application/json',
            'Authorization': `Bearer ${idToken}`
        }
    }).then((data)=>{
        console.log(data)
    }).catch((err)=>{
        console.log(err);
    }

首先,您不应该设置GET请求的内容类型。你能解释一下实际问题吗?您共享的代码基本上是来自firbase dev docs的代码片段。我希望仅当用户已注册时才呈现profile.ejs页面,否则我希望呈现主页