Thinkster
Angular
Simple AngularJS Authentication with JWT

Working with JWTs

Make authentication requests to retrieve a JWT from a backend

Before we can really examine the JWT, we'll need to get one from the server. This involves registering or signing into an account. Then we'll build out our auth service for working with the token.

Getting a Token

There are two different actions a user can perform to identify themselves to the server (and thus receive a token): when a user first signs up and when the user logs in. We'll use a user service to handle these two interactions:

Create the user.register() method
self.register = function(username, password) {
  return $http.post(API + '/auth/register', {
      username: username,
      password: password
    })
}
Create the user.login() method
self.login = function(username, password) {
  return $http.post(API + '/auth/login', {
      username: username,
      password: password
    })
};

You can now create an account or authenticate against a previous account. If either action is successful, you should see a JWT printed out to the JavaScript console.

The test server is liable to clear account information at anytime so if a previously registered account no longer works, just create another one.

Now that we can authenticate against the server and receive a token, we need to build out some functionality to parse, save, and retrieve the token information. To do this, we're going to use a separate service called auth.