mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-24 15:25:46 +00:00
feat: create auth-helper file and redirectToSSO helper function
This commit is contained in:
@@ -0,0 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* Redirects the user to the SSO login page with loop protection.
|
||||||
|
*
|
||||||
|
* This function checks a session storage timestamp to ensure that redirects
|
||||||
|
* do not happen too frequently (blocking infinite redirect loops).
|
||||||
|
*/
|
||||||
|
export const redirectToSSO = () => {
|
||||||
|
if (typeof window === 'undefined') return;
|
||||||
|
|
||||||
|
const lastRedirect = sessionStorage.getItem('auth_redirect_timestamp');
|
||||||
|
const now = Date.now();
|
||||||
|
|
||||||
|
// Loop protection: allow redirect only if last one was > 2 seconds ago
|
||||||
|
// or if no redirect has happened yet.
|
||||||
|
if (!lastRedirect || now - parseInt(lastRedirect, 10) > 2000) {
|
||||||
|
sessionStorage.setItem('auth_redirect_timestamp', now.toString());
|
||||||
|
// const ssoLoginUrl = `${process.env.NEXT_PUBLIC_SSO_LOGIN_URL as string}?redirect_url=${window.location.href}`;
|
||||||
|
|
||||||
|
const ltiSsoStart = `${process.env.NEXT_PUBLIC_API_BASE_URL as string}/sso/start?client_id=${process.env.NEXT_PUBLIC_CLIENT_ID as string}&redirect_url=${window.location.href}`;
|
||||||
|
const ssoLoginUrl = `${process.env.NEXT_PUBLIC_SSO_LOGIN_URL as string}?redirect_url=${ltiSsoStart}`;
|
||||||
|
window.location.href = ssoLoginUrl;
|
||||||
|
} else {
|
||||||
|
console.error('Redirect loop detected. Aborting redirect.');
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user