Quickstart

A quick dive into getting started with Lore

Step 5: Add Authorization Header

In this step we'll add an Authorization header to all API calls.

You can view the finished code for this step by checking out the server.5 branch of the completed project.

Add the Authorization Header

Now that we can clearly see when the user is unauthorized, let's add the user's token to the Authorization header to authenticate them and learn who they are.

Remember the auth utility in utils/auth.js that we used to save the user's token to localStorage? We're going to be using that once again to retrieve the user's token and add it to the header of all API requests.

Open config/connections.js and find the commented out method below the apiRoot called headers. It looks like this:

// config/connections.js
export default {

  default: {

    apiRoot: 'http://localhost:1337',

    // headers: function() {
    //   return {};
    // },

    ...

  }

};

Import the auth module into the config and set the Authorization like this:

// config/connections.js
import auth from '../src/utils/auth';

export default {

  default: {

    apiRoot: 'http://localhost:1337',

    headers: function() {
      return {
        Authorization: `Bearer ${auth.getToken()}`
      };
    },

    ...
  }

};

With that change in place, refresh the browser and application should display correctly again.

Login as Different Characters

At this point, not only will the application display correctly again, but because we're now backed by a real API, you'll also be able to login as different characters and have the profile reflect that. Try it out!

As a reminder, you can login as any of the characters below:

  • ayla
  • crono
  • frog
  • lucca
  • magus
  • marle
  • robo

The email format is {name}@example.com, and the password for all of them is password. So if you wanted to login as marle, you would enter marle@example.com for the email and password for the password.

Visual Check-in

If everything went well, your application should look like this.

Code Changes

Below is a list of files modified during this step.

config/connections.js

import auth from '../src/utils/auth';

export default {

  default: {

    apiRoot: 'http://localhost:1337',

    headers: function() {
      return {
        Authorization: `Bearer ${auth.getToken()}`
      };
    },

    collections: {
      properties: {
        parse: function(response) {
          return response.data;
        }
      }
    }

  }
};

Next Steps

In the next section we'll add support for pagination.