multiauth refactor

This commit is contained in:
Jan Prochazka
2024-08-07 16:28:24 +02:00
parent e179b0f20b
commit 5f14da3844
10 changed files with 98 additions and 63 deletions

View File

@@ -1,4 +1,4 @@
import { apiCall, enableApi } from './utility/api';
import { apiCall, enableApi, getAuthCategory } from './utility/api';
import { getConfig } from './utility/metadataLoaders';
import { isAdminPage } from './utility/pageDefs';
@@ -40,9 +40,12 @@ export function handleOauthCallback() {
const sentCode = params.get('code');
if (isOauthCallback()) {
const [_prefix, strmid, amoid] = sessionStorage.getItem('oauthState').split(':');
sessionStorage.removeItem('oauthState');
apiCall('auth/oauth-token', {
code: sentCode,
amoid,
redirectUri: location.origin + location.pathname,
}).then(authResp => {
const { accessToken, error, errorMessage } = authResp;
@@ -113,7 +116,7 @@ export async function handleAuthOnStartup(config, isAdminPage = false) {
return;
}
if (config.isAdminLoginForm && isAdminPage) {
if (getAuthCategory(config) == 'admin') {
if (localStorage.getItem('adminAccessToken')) {
return;
}
@@ -122,10 +125,10 @@ export async function handleAuthOnStartup(config, isAdminPage = false) {
return;
}
if (config.oauth) {
console.log('OAUTH callback URL:', location.origin + location.pathname);
}
if (config.oauth || config.isLoginForm) {
// if (config.oauth) {
// console.log('OAUTH callback URL:', location.origin + location.pathname);
// }
if (getAuthCategory(config) == 'token') {
if (localStorage.getItem('accessToken')) {
return;
}
@@ -145,7 +148,7 @@ export async function redirectToLogin(config = null, force = false) {
config = await getConfig();
}
if (config.isLoginForm) {
if (getAuthCategory(config) == 'token') {
if (!force) {
const params = new URLSearchParams(location.search);
if (params.get('page') == 'login' || params.get('page') == 'admin-login' || params.get('page') == 'not-logged') {