better oauth handle

This commit is contained in:
Jan Prochazka
2022-11-17 19:55:01 +01:00
parent 576fc2062c
commit 94a91d5fed
5 changed files with 58 additions and 30 deletions

View File

@@ -1,34 +1,44 @@
import { apiCall } from './utility/api';
import { getConfig } from './utility/metadataLoaders';
export async function handleAuthOnStartup(config) {
console.log('********************* handleAuthOnStartup');
if (config.oauth) {
const params = new URLSearchParams(location.search);
const sentCode = params.get('code');
const sentState = params.get('state');
export function handleOauthCallback() {
const params = new URLSearchParams(location.search);
const sentCode = params.get('code');
const sentState = params.get('state');
if (
sentCode &&
sentState &&
sentState.startsWith('dbg-oauth:') &&
sentState == sessionStorage.getItem('oauthState')
) {
const authResp = await apiCall('auth/oauth-token', {
code: sentCode,
redirectUri: location.origin,
});
if (
sentCode &&
sentState &&
sentState.startsWith('dbg-oauth:') &&
sentState == sessionStorage.getItem('oauthState')
) {
sessionStorage.removeItem('oauthState');
apiCall('auth/oauth-token', {
code: sentCode,
redirectUri: location.origin,
}).then(authResp => {
const { accessToken } = authResp;
console.log('Got new access token:', accessToken);
localStorage.setItem('accessToken', accessToken);
location.replace('/');
} else {
if (localStorage.getItem('accessToken')) {
return;
}
});
redirectToLogin(config);
console.log('handleOauthCallback TRUE');
return true;
}
console.log('handleOauthCallback FALSE');
return false;
}
export async function handleAuthOnStartup(config) {
console.log('********************* handleAuthOnStartup');
if (config.oauth) {
if (localStorage.getItem('accessToken')) {
return;
}
redirectToLogin(config);
}
}