From the course: Angular: API Communication and Authentication

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Sending JWTs to API requests

Sending JWTs to API requests - Angular Tutorial

From the course: Angular: API Communication and Authentication

Start my 1-month free trial

Sending JWTs to API requests

- Now that we have a way of obtaining a JWT from the API by giving the right credentials, we need to make sure that we are sending this token on every request made to our API, so that we have access to our data. Let's go ahead and open up the API servers. The first thing we'll need to do is import the Authservice. And, we're already in the shared folder, so we can already go ahead and import auth.service. Next step is to inject it into the constructor here. Then, in the request method, we will add another header. This header will be the authorization header. And the format of this header is Bearer, space, then the token. And, we can use the auth service to retrieve our token. Let's open up the terminal here and run npm start to test things out. Once things are ready, let's head over to our browser at port 3000/login and let's login with the right credentials. The username is admin and then we're going to head over and just grab the password from the users.json file. It's in the server…

Contents