Im working on a WebApplication running on the MEAN stack (Following the TraversyMedia Tutorial). However, I have ran into an issue and none of the solutions offered over here seem to work for me. Whenever I try to access my /profile page (which is protected unless you are authorized) I get the error returned. However, I am logged in and have a proper access token.
I have looked at the passport-jwt git. But what I have seems to match up.
I have tried to use:
ExtractJwt.fromAuthHeaderWithScheme("bearer");
ExtractJwt.fromAuthHeaderWithScheme("bearer");
ExtractJwt.fromAuthHeaderAsBearerToken();
Currently going with: ExtractJwt.fromAuthHeaderWithScheme("bearer") since none showed a difference.
I have looked into the HttpClientModule, but my call seems to be OK.
passport.js
module.exports = function(passport) {
let opts = {};
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderWithScheme("bearer");
opts.secretOrKey = config.secret;
passport.use(
new JwtStrategy(opts, (jwt_payload, done) => {
User.getUserById(jwt_payload.data._id, (err, user) => {
if (err) {
return done(err, false);
}
if (user) {
return done(null, user);
} else {
return done(null, false);
}
});
})
);
};
users.js
const express = require("express");
const router = express.Router();
const passport = require("passport");
const jwt = require("jsonwebtoken");
const config = require("../config/database");
const User = require("../models/user");
...
router.get(
"/profile",
passport.authenticate("jwt", { session: false }),
(req, res, next) => {
res.json({ user: req.user });
}
);
module.exports = router;
auth.service.ts
import { Injectable } from "@angular/core";
import { HttpClient, HttpHeaders } from "@angular/common/http";
import { map } from "rxjs/operators";
// Left some unrelated stuff out here
export class AuthService {
authToken: any;
user: any;
constructor(private http: HttpClient) {}
...
getProfile() {
let headers = new HttpHeaders();
this.loadToken();
headers.append("authorization", this.authToken);
headers.append("Content-Type", "application/json");
return this.http
.get("http://localhost:3000/users/profile", {
headers: headers
})
.pipe(map(res => res));
}
...
loadToken() {
const token = localStorage.getItem("id_token");
this.authToken = token;
}
profile.component.ts
export class ProfileComponent implements OnInit {
user: Object;
constructor(private authService: AuthService, private router: Router) {}
ngOnInit() {
this.authService.getProfile().subscribe(
profile => {
this.user = profile.user;
},
err => {
console.log(err);
return false;
}
);
}
}
Expected to be directed to the profile page, but instead I am getting:
GET http://localhost:3000/users/profile 401 (Unauthorized)
HttpErrorResponse {headers: HttpHeaders, status: 401, statusText: "Unauthorized", url: "http://localhost:3000/users/profile", ok: false, …}
error: "Unauthorized"
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, headers: Map(0)}
message: "Http failure response for http://localhost:3000/users/profile: 401 Unauthorized"
name: "HttpErrorResponse"
ok: false
status: 401
statusText: "Unauthorized"
url: "http://localhost:3000/users/profile"