Snippets

Mythics JIRA - Redirect anonymous users to login page

Created by Jonathan Hult last modified
var RedirectLogin = RedirectLogin || {}; 

RedirectLogin.init = function() {
  // example: /secure/Dashboard.jspa
  var pathname = window.location.pathname;
  
  // login page URL
  var loginUrl = "/login.jsp";
  
  // is user already on JIRA login page?
  var isOnLoginUrl = loginUrl.match(pathname) !== null;
  
  // Is a full JIRA user already logged in?
  var isJIRAUserLoggedIn = JIRA.Users.LoggedInUser.userName().length > 0;
  
  // JIRA user is already logged in and yet on the login page; so, redirect to Dashboard
  if (isJIRAUserLoggedIn && isOnLoginUrl) window.location = "/secure/Dashboard.jspa";
  
  if (!isJIRAUserLoggedIn && !RedirectLogin.isURLToRedirect(pathname) && !isOnLoginUrl) {
    // do redirect

    // the querystring part of a URL
    var queryStr = window.location.search;    
    window.location = loginUrl + queryStr;
  }
};

// is this a URL to redirect to the login page?
RedirectLogin.isURLToRedirect = function(pathname) {
  var shouldRedirect = false;
  
  var forgotPassUrl = /^\/secure\/ForgotLoginDetails.jspa$/;
  var resetPassUrl = /^\/secure\/ResetPassword!default.jspa/;
  
  if (forgotPassUrl.test(pathname) || resetPassUrl.test(pathname)) {
    shouldRedirect = true;
  }
  return shouldRedirect;
};

RedirectLogin.init();

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.