Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/bugfix/authentication-issues' in…
Browse files Browse the repository at this point in the history
…to lf-staging-main
  • Loading branch information
joanagmaia committed Jan 16, 2024
2 parents 48c47df + b96c31b commit 4a6c6e6
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 14 deletions.
10 changes: 8 additions & 2 deletions frontend/src/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ export default {
async handler(value) {
if (value) {
await TenantService.fetchAndApply();
this.fetchActivityTypes();
this.fetchActivityChannels();
try {
const user = await Auth0Service.getUser();
Expand All @@ -95,6 +93,14 @@ export default {
}
},
},
currentTenant: {
handler(tenant, oldTenant) {
if (tenant?.id && tenant.id !== oldTenant?.id) {
this.fetchActivityTypes();
this.fetchActivityChannels();
}
},
},
},
async created() {
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/modules/auth/pages/callback.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ const { doSigninWithAuth0 } = mapActions('auth');
onMounted(() => {
Auth0Service.handleAuth()
.then(() => {
.then(({ appState }) => {
Auth0Service.authData().then((token) => {
doSigninWithAuth0(token);
doSigninWithAuth0({ token, appState }).then(() => {
});
});
})
.catch(() => {
Expand Down
8 changes: 6 additions & 2 deletions frontend/src/modules/auth/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export default {

doSigninWithAuth0(
{ commit, dispatch },
token,
{ token, appState },
) {
commit('AUTH_START');
return AuthService.ssoGetToken(token)
Expand All @@ -121,7 +121,11 @@ export default {
commit('AUTH_SUCCESS', {
currentUser: currentUser || null,
});
router.push('/');

window.history.replaceState(null, '', appState?.targetUrl ?? '/');
window.history.pushState(null, '', appState?.targetUrl ?? '/');

router.push(appState?.targetUrl ?? '/');
})
.catch((error) => {
AuthService.signout();
Expand Down
7 changes: 4 additions & 3 deletions frontend/src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import authGuards from '@/middleware/auth';
import modules from '@/modules';
import ProgressBar from '@/shared/progress-bar/progress-bar';
import { useLfSegmentsStore } from '@/modules/lf/segments/store';
import AuthCurrentTenant from '@/modules/auth/auth-current-tenant';

/**
* Loads all the routes from src/modules/ folders, and adds the catch-all rule to handle 404s
Expand Down Expand Up @@ -90,8 +91,8 @@ export const createRouter = () => {
store,
};

middlewareArray.forEach((middleware) => {
middleware(context);
await middlewareArray.forEach(async (middleware) => {
await middleware(context);
});

// Redirect to project group landing pages if routes that require a selected project group
Expand All @@ -107,7 +108,7 @@ export const createRouter = () => {
return;
}

if (!selectedProjectGroup.value) {
if (!selectedProjectGroup.value && AuthCurrentTenant.get()) {
try {
await listProjectGroups({
limit: null,
Expand Down
16 changes: 11 additions & 5 deletions frontend/src/shared/services/auth0.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import config from '@/config';
import { Auth0Client } from '@auth0/auth0-spa-js';
import { store } from '@/store';
import { router } from '@/router';

const baseUrl = `${config.frontendUrl.protocol}://${config.frontendUrl.host}`;
const authCallback = `${baseUrl}/auth/callback`;
Expand Down Expand Up @@ -41,12 +42,17 @@ class Auth0ServiceClass {
store.dispatch('auth/doAuthenticate');

return Promise.resolve();
}).catch(() => {
// If getTokenSilently() fails it's because user is not authenticated
Auth0ServiceClass.localLogout();
this.loginWithRedirect();
}).catch((error) => {
if (error.error === 'login_required' || error.error === 'consent_required' || error.error
=== 'missing_refresh_token') {
return this.webAuth.loginWithRedirect({
appState: {
targetUrl: router.currentRoute.value.fullPath,
},
});
}

return Promise.reject();
return Promise.reject(error);
});
}

Expand Down

0 comments on commit 4a6c6e6

Please sign in to comment.