简体   繁体   English

成功登录Angular-node API后,即使使用CORS也会返回401(未经授权)

[英]401 (Unauthorized) after successful login Angular-node API even using CORS

I am working in Angular,node-express API web app. 我正在使用Angular,node-express API Web应用程序。 I am getting Unauthorized even using cors . 即使使用cors我也会获得未经授权的cors I am very new to asynchronous world. 我对异步世界非常陌生。 I show My code snippet below. 我在下面显示我的代码段。

node-login API 节点登录API

app.post('/login',(req, res, next)=>{
    var userCredentials={
        email:req.body.email,
        password:req.body.password
    }
    auth.login(userCredentials,(err,result)=> {
        if(err) {
            if(err=="notExist") {
                res.json({
                    success:false,
                    message:"notExist"});
                next()
            }
            else if(result=="success") {
            let payload={
                email: userCredentials.email,
            }
            let token = jwt.sign(payload, jwtOpts.secretOrKey);

            res.json({
                success:true,
                message:"success",token});
            // next()
        }

app.get('/dashboard',passport.authenticate('jwt', {session: false}),tournament.getAllTournaments);

CORS-Config CORS-配置

let cors = require('cors');
app.use(cors());
app.options('*', cors());

also in same route file. 也在同一路由文件中。

Angular-service 角服务

login(credentials) {
        return this.http.post('http://localhost:8000/login',credentials)
        .subscribe(
            response=> {
                console.log(response)
                if(response['success']) { /*note:OUTPUT*/
                    console.log(response)
                    this.token=response['token'];
                    this.http.get('http://localhost:8000/dashboard')
                    .subscribe(
                        response=> {
                            if(response['success']){
                                this.router.navigate(['/dashboard']);/*401 occurs here*/
                            }
                            else {
                                /*Error handler*/
                            }
                        }
                    )
                }
                else {
                    /*Error handler*/
                }
            },
            error=> {
                console.log(error)
            }
        )
    }
}

note:Output 注:输出

I am Getting response as 我正在得到回应

{
   success:true,
   message:"success",
   token
}

at Mentioned response.So I am trying to redirect user to /dashboard . 在提及的响应。所以我试图将用户重定向到/dashboard For that request I am getting unAuthorized from my API side. 对于该请求,我正在从API端获得未经授权的信息。

I won't get sos from following questions. 我不会因以下问题而感到困惑。 Angular returning 401 unauthorized Angular.js 401 Unauthorized Status Code Angular returning 401 unauthorized Angular返回401未经授权 Angular.js 401未经授权的状态码 Angular返回401未经授权

Thanks in advance...!! 提前致谢...!!

Missed header in Angular /dashboard http call. Angular /dashboard http调用中缺少标题。 so Code look like 所以代码看起来像

           if(response['success']) {
              this.token=response['token'];
              let value='JWT '+this.token;
              const header=new HttpHeaders({Authorization:value});
              this.http.get('http://localhost:8000/dashboard',{headers:header})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Angular-Node应用程序中调用CORS后,Express会话不会持久化 - Express session not persisting after CORS calls in Angular-Node application 来自调用 Web Api 2 应用程序的 Angular 节点应用程序的 401 未经授权的 ajax 调用 - 401 Unauthorized ajax call from angular node application calling Web Api 2 application 使用REST API未经授权解析注销401 - Parse Logout 401 unauthorized using REST api 在新标签Angular-Node中从应用程序启动新应用程序 - Launch new app from app in new tab Angular-Node 成功登录后要返回角度页面吗? - Going back to angular page after successful login? 从JavaScript前端调用API:未经授权使用Angularfire 401 - Calling API from JavaScript Frontend: 401 unauthorized using angularfire 角+ PHP登录API CORS问题 - angular + php login api cors issue 使用Angular成功登录后使用$ location重定向 - Redirecting using $location upon successful login with Angular Twitter登录Cordova和Angularjs:401未经授权的错误 - Twitter Login with Cordova and Angularjs: 401 unauthorized error 在angular js中成功登录后将用户重定向到仪表板 - Redirect user to dashboard after successful login in angular js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM