0

I am working in Angular,node-express API web app. I am getting Unauthorized even using cors. I am very new to asynchronous world. I show My code snippet below.

node-login 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

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. For that request I am getting unAuthorized from my API side.

I won't get sos from following questions. Angular returning 401 unauthorized Angular.js 401 Unauthorized Status Code Angular returning 401 unauthorized

Thanks in advance...!!

2
  • It looks like you are not adding any header containing the token in your get request. You should pass it in the second parameter of your $get and it might look like: this.http.get('http://localhost:8000/dashboard', { headers: { Authorization: `Bearer ${this.token}`}}) Commented Aug 3, 2017 at 20:21
  • Thankyou @Dario .... found it. Commented Aug 3, 2017 at 20:23

1 Answer 1

0

Missed header in Angular /dashboard http call. 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})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.