Skip to content

Commit

Permalink
fix: login redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinKolarik committed Aug 27, 2024
1 parent ef95d7e commit cec6cef
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
4 changes: 2 additions & 2 deletions middleware/auth.global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ export default defineNuxtRouteMiddleware(async (to) => {

if (auth.isLoggedIn && to?.name === 'login') {
abortNavigation();
return navigateTo('/');
return navigateTo(auth.getRedirectUrl(), { external: true });
}

if (!auth.isLoggedIn && to?.name !== 'login') {
abortNavigation();
return navigateTo('/login');
return navigateTo(`/login?redirect=${encodeURIComponent(location.href)}`);
}
});
13 changes: 12 additions & 1 deletion store/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,20 @@ export const useAuth = defineStore('auth', {
},

actions: {
getRedirectUrl () {
const url = new URL(location.href);
const redirect = url.searchParams.get('redirect') || '/';
const redirectUrl = new URL(redirect, url.origin);

if (redirectUrl.origin !== url.origin) {
return url.origin;
}

return redirectUrl.toString();
},
async login () {
const config = useRuntimeConfig();
const redirect = `${window.location.origin}${'/'}`;
const redirect = this.getRedirectUrl();
await navigateTo(`${config.public.directusUrl}/auth/login/github?redirect=${encodeURIComponent(redirect)}`, { external: true });
},
async logout () {
Expand Down

0 comments on commit cec6cef

Please sign in to comment.