Skip to content
Rishan Solutions
Rishan Solutions
  • PowerApps
  • SharePoint online
    • Uncategorized
    • Uncategorized
  • PowerAutomate
Rishan Solutions
Latest Posts
  • Agentic AI: The Dawn of Autonomous Intelligence Revolutionizing 2025 June 24, 2025
  • Recursive Queries in T-SQL May 7, 2025
  • Generating Test Data with CROSS JOIN May 7, 2025
  • Working with Hierarchical Data May 7, 2025
  • Using TRY_CAST vs CAST May 7, 2025
  • Dynamic SQL Execution with sp_executesql May 7, 2025

Redirecting users based on authentication

Posted on April 1, 2025April 1, 2025 by Rishan Solutions

Loading

In modern web applications, user authentication is an essential feature. One common use case is to redirect users based on their authentication status. For example, you might want to redirect users who are not logged in to a login page and allow authenticated users to access restricted areas. AngularJS provides various ways to handle such redirections in single-page applications (SPAs).

In this guide, we will discuss how to implement redirection based on authentication status using AngularJS and its built-in features like $routeProvider, $location, $rootScope, and services.


1. Setting Up Authentication in AngularJS

First, let’s outline the basic flow of authentication. Typically, in AngularJS, we use a service to manage user authentication. This service handles the login process, checks whether the user is authenticated, and stores the authentication status.

Authentication Service Example

Create a service that manages user authentication:

app.factory('AuthService', function($http, $q, $rootScope) {
var authStatus = false;

return {
login: function(credentials) {
// Assuming we make an API call to authenticate the user
return $http.post('/api/login', credentials)
.then(function(response) {
authStatus = true;
$rootScope.authenticated = true; // Store authenticated status in $rootScope
return response.data;
})
.catch(function() {
authStatus = false;
$rootScope.authenticated = false;
return $q.reject('Authentication failed');
});
},
logout: function() {
authStatus = false;
$rootScope.authenticated = false;
// Optionally, perform an API call to log out on the server-side
},
isAuthenticated: function() {
return authStatus;
}
};
});

In this example, the AuthService has methods to login, logout, and check the authentication status. The $rootScope.authenticated flag is used to keep track of the user’s authentication state globally across the application.


2. Redirecting Users Based on Authentication Status

You need to ensure that users who are not authenticated cannot access restricted routes or views. This can be achieved by using AngularJS’s routing system and intercepting route changes with $routeChangeStart event to check for authentication.

Using $routeProvider to Define Routes

You can use $routeProvider to define routes and configure access restrictions based on the authentication status.

app.config(function($routeProvider) {
$routeProvider
.when('/home', {
templateUrl: 'home.html',
controller: 'HomeController'
})
.when('/dashboard', {
templateUrl: 'dashboard.html',
controller: 'DashboardController',
resolve: {
auth: function(AuthService, $location) {
// Check if user is authenticated
if (!AuthService.isAuthenticated()) {
// Redirect to login page if not authenticated
$location.path('/login');
}
}
}
})
.when('/login', {
templateUrl: 'login.html',
controller: 'LoginController'
})
.otherwise({
redirectTo: '/home'
});
});

In this configuration:

  • The /dashboard route has a resolve block. This block checks if the user is authenticated before the route is activated. If not authenticated, the user is redirected to the login page.
  • The auth function in resolve checks the authentication status using the AuthService and redirects to the login page if necessary.

3. Intercepting Route Changes Using $rootScope.$on('$routeChangeStart')

You can also use $rootScope.$on('$routeChangeStart') to catch every route change and handle the authentication check globally for the app.

app.run(function($rootScope, $location, AuthService) {
$rootScope.$on('$routeChangeStart', function(event, next, current) {
// If route requires authentication and user is not authenticated
if (next.$$route && next.$$route.resolve && next.$$route.resolve.auth) {
if (!AuthService.isAuthenticated()) {
// Prevent navigation and redirect to login page
event.preventDefault();
$location.path('/login');
}
}
});
});

In this example:

  • The $routeChangeStart event is fired every time a route change happens.
  • The event checks if the route has a resolved auth object (which indicates the route requires authentication).
  • If the user is not authenticated, it prevents the navigation (event.preventDefault()) and redirects the user to the login page ($location.path('/login')).

4. Implementing a Login Page

In your login page, you should handle the authentication process. For example, once the user successfully logs in, you should redirect them to the page they initially tried to access, or to a default page like the dashboard.

app.controller('LoginController', function($scope, $location, AuthService) {
$scope.login = function(credentials) {
AuthService.login(credentials).then(function() {
// Redirect to dashboard or previous page after successful login
$location.path('/dashboard');
}, function() {
// Handle failed login
alert('Login failed');
});
};
});

In this example, once the login is successful, the user is redirected to the /dashboard route. If the login fails, you can show an error message.


5. Handling Session Expiry and Logout

A common requirement is to manage session expiry. If the user’s session expires or if they manually log out, you should ensure that they are redirected to a public page like the login page.

Logout Functionality

To handle logging out, you could use the logout method in your AuthService and clear the authentication status.

app.controller('LogoutController', function($scope, $location, AuthService) {
AuthService.logout();
$location.path('/home');
});

This would clear the authentication status and redirect the user to the home page or any other public page.

Session Expiry Check

To handle session expiry, you might periodically check the user’s session status with a service that manages session expiry. If the session expires, you can redirect the user to the login page.

For example, using an HTTP interceptor to check session expiry:

app.factory('AuthInterceptor', function($q, $location, AuthService) {
return {
responseError: function(response) {
if (response.status === 401) { // Unauthorized error
AuthService.logout();
$location.path('/login');
}
return $q.reject(response);
}
};
});

app.config(function($httpProvider) {
$httpProvider.interceptors.push('AuthInterceptor');
});

This example uses an HTTP interceptor to check for unauthorized responses (like expired sessions) and redirects the user to the login page.

Posted Under AngularJS$http $location $q $rootScope $routeChangeStart $routeProvider $state.go() AngularJS AngularJS Routing AngularJS Services App Security Authentication Authentication Service dynamic routing HTTP Interceptor login page login workflow resolve restricted routes route configuration route guards Route Management route protection Session Expiry session management SPA User Access Control User Authorization user redirection

Post navigation

Using $routeParams to pass data between views
How to implement wildcard routes (*)

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Agentic AI: The Dawn of Autonomous Intelligence Revolutionizing 2025
  • Recursive Queries in T-SQL
  • Generating Test Data with CROSS JOIN
  • Working with Hierarchical Data
  • Using TRY_CAST vs CAST

Recent Comments

  1. Michael Francis on Search , Filter and Lookup in power apps
  2. A WordPress Commenter on Hello world!

Archives

  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • March 2024
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • June 2023
  • May 2023
  • April 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • January 2022

Categories

  • Active Directory
  • AI
  • AngularJS
  • Blockchain
  • Button
  • Buttons
  • Choice Column
  • Cloud
  • Cloud Computing
  • Data Science
  • Distribution List
  • DotNet
  • Dynamics365
  • Excel Desktop
  • Extended Reality (XR) – AR, VR, MR
  • Gallery
  • Icons
  • IoT
  • Java
  • Java Script
  • jQuery
  • Microsoft Teams
  • ML
  • MS Excel
  • MS Office 365
  • MS Word
  • Office 365
  • Outlook
  • PDF File
  • PNP PowerShell
  • Power BI
  • Power Pages
  • Power Platform
  • Power Virtual Agent
  • PowerApps
  • PowerAutomate
  • PowerPoint Desktop
  • PVA
  • Python
  • Quantum Computing
  • Radio button
  • ReactJS
  • Security Groups
  • SharePoint Document library
  • SharePoint online
  • SharePoint onpremise
  • SQL
  • SQL Server
  • Template
  • Uncategorized
  • Variable
  • Visio
  • Visual Studio code
  • Windows
© Rishan Solutions 2025 | Designed by PixaHive.com.
  • Rishan Solutions