-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 91938: Signs out when click on Close (X) (#68)
* Aligned Zapp.ie Authentication process with Alice * updated authConfig redirect setting
- Loading branch information
1 parent
65eba66
commit d1082d8
Showing
11 changed files
with
371 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import React, { useEffect } from 'react'; | ||
import { PublicClientApplication } from '@azure/msal-browser'; | ||
import { msalConfig } from './services/authConfig'; | ||
|
||
const AuthEnd: React.FC = () => { | ||
useEffect(() => { | ||
const msalInstance = new PublicClientApplication(msalConfig); | ||
const urlParams = new URLSearchParams(window.location.search); | ||
const redirectUrl = urlParams.get('redirectUrl') || window.location.origin; | ||
msalInstance.initialize().then(() => { | ||
msalInstance | ||
.handleRedirectPromise() | ||
.then(response => { | ||
window.close(); | ||
if (response) { | ||
// Authentication was successful | ||
window.opener.postMessage( | ||
{ | ||
type: 'auth-success', | ||
data: response, | ||
}, | ||
window.location.origin, | ||
); | ||
if (window.opener) { | ||
window.opener.focus(); | ||
window.close(); // Close the authentication window | ||
if (!window.closed) { | ||
window.location.href = redirectUrl; // Fallback to redirect if window.close() fails | ||
} | ||
} else { | ||
window.location.href = redirectUrl; // Redirect to the original page | ||
} | ||
} | ||
}) | ||
.catch(error => { | ||
console.error('Error handling redirect:', error); | ||
window.opener.postMessage( | ||
{ | ||
type: 'auth-error', | ||
data: error, | ||
}, | ||
window.location.origin, | ||
); | ||
window.opener.focus(); | ||
window.close(); // Close the authentication window | ||
if (!window.closed) { | ||
window.location.href = redirectUrl; // Fallback to redirect if window.close() fails | ||
} | ||
}); | ||
}).catch(error => { | ||
console.error('Error initializing MSAL:', error); | ||
}); | ||
}, []); | ||
|
||
return ( | ||
<div> | ||
<h1>Completing Authentication...</h1> | ||
</div> | ||
); | ||
}; | ||
|
||
export default AuthEnd; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import React, { useEffect } from 'react'; | ||
import { PublicClientApplication, InteractionStatus } from '@azure/msal-browser'; | ||
import { useMsal } from '@azure/msal-react'; | ||
import { msalConfig } from './services/authConfig'; | ||
|
||
const AuthStart: React.FC = () => { | ||
const { inProgress } = useMsal(); | ||
|
||
useEffect(() => { | ||
const msalInstance = new PublicClientApplication(msalConfig); | ||
|
||
msalInstance.initialize().then(() => { | ||
const accounts = msalInstance.getAllAccounts(); | ||
if (accounts.length > 0) { | ||
// User is already signed in | ||
if (inProgress !== InteractionStatus.Startup && inProgress !== InteractionStatus.HandleRedirect) { | ||
window.location.href = window.location.origin + '/auth-end'; // Redirect to the auth-end page | ||
} | ||
} else { | ||
// Check if an interaction is already in progress | ||
if (inProgress !== InteractionStatus.None) { | ||
// Initiate login | ||
msalInstance.loginRedirect({ | ||
scopes: ['User.Read'], | ||
redirectUri: window.location.origin + '/auth-end', // Redirect back to auth-end page | ||
}).catch(error => { | ||
console.error('Error during loginRedirect:', error); | ||
}); | ||
} else { | ||
console.log('Interaction is already in progress.'); | ||
} | ||
} | ||
}).catch(error => { | ||
console.error('Error initializing MSAL:', error); | ||
}); | ||
}, [inProgress]); | ||
|
||
return ( | ||
<div> | ||
<h1>Starting Authentication...</h1> | ||
</div> | ||
); | ||
}; | ||
|
||
export default AuthStart; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,21 @@ | ||
import { useEffect } from 'react'; | ||
import { useMsal } from '@azure/msal-react'; | ||
import { loginRequest } from '../services/authConfig'; | ||
import { useNavigate } from 'react-router-dom'; | ||
|
||
const RequireAuth = ({ children }: { children: JSX.Element }) => { | ||
const { instance, accounts } = useMsal(); | ||
const { instance, accounts, inProgress } = useMsal(); | ||
const navigate = useNavigate(); | ||
|
||
useEffect(() => { | ||
if (accounts.length === 0) { | ||
instance.loginPopup(loginRequest).catch(error => { | ||
console.error(error); | ||
if (accounts.length === 0 && inProgress === "none") { | ||
instance.acquireTokenSilent(loginRequest).catch(() => { | ||
navigate('/login', { replace: true }); | ||
}); | ||
} | ||
}, [accounts, instance]); | ||
}, [accounts, instance, inProgress, navigate]); | ||
|
||
return accounts.length > 0 ? children : null; | ||
}; | ||
|
||
export default RequireAuth; | ||
export default RequireAuth; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.