2

I have just shifted from Marionette.js to Angular. Right now I am creating one application where there is a login screen and home screen, I have two rest apis one for authentication and another one to check if user has active session or not(using tokens). If user has active session I want to redirect him to home screen.

How can I implement this in Angular in best way?

Thanks, MSK

Note: I am using yeoman/generator-angular and ui-router

1 Answer 1

1
  1. Authentication service

'use strict';

app.factory('Auth', function Auth($http, $cookies, $q) {
    this.login = //...
    this.isLoggedIn = //... point to your REST services
});

  1. Controlling through ui-router when needs to be authenticated

      .state('settings', {
        url: '/settings',
        authenticate: true //add this to state where user needs to be authenticated
      });

  1. Storing and retrieving tokens

app.config(function($httpProvider) {
    //add interceptor to add authentication header to be verified on the server
    $httpProvider.interceptors.push('authInterceptor');
  })

app.factory('authInterceptor', function($cookies) {
    var state;
    return {
      // Add authorization token to headers
      request: function(config) {
        config.headers = config.headers || {};
        if ($cookies.get('token')) {
          config.headers.Authorization = 'Bearer ' + $cookies.get('token');
        }
        return config;
      }
    };
});

  1. Checking certain states and redirecting if needed

  app.run(function($rootScope, $state, Auth) {
    // Redirect to login if route requires authentication
    $rootScope.$on('$stateChangeStart', function(event, next) {
      if (next.authenticate) {
        Auth.isLoggedIn(function(loggedIn) {
          if (!loggedIn) {
            event.preventDefault();
            $state.go('login');
          }
        });
      }
    });

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @Yerken but I also want to validate the token (using rest api) when application loads so that i can navigate the user to home screen. So if user is already logged in and hits the application URL rest api will validate the token and then navigate him to login or home screen based on result.
@MSK you mean you want to validate that user is logged in on any URL (state) ? Or you want to validate only when the application is launched?
This is the use case User logs is and application saves the token in cookie, User closes the browser or opens a new tab hits the application url. Application gets the token from cookie but in this case I need to make sure that token is active and for that I want to make a api call which will tell me whether the token is valid or not. If the token is valid I will redirect the user to home page else he will redirected to login page.

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.