feat(auth): enhance logout and login flow with return URL handling

This commit is contained in:
2025-04-25 14:41:58 -04:00
parent 2c09be4465
commit e394c346ae
5 changed files with 53 additions and 11 deletions

View File

@@ -3,16 +3,30 @@ import {ref} from 'vue';
import {GoogleLogin} from "vue3-google-login";
import {useAuthStore} from '@/stores/authStore.js';
import {useI18n} from 'vue-i18n';
import {useRouter} from 'vue-router';
const {t} = useI18n();
const router = useRouter();
const authStore = useAuthStore();
const errorSnackBar = ref(false);
const props = defineProps({
returnUrl: {
type: String,
default: '/landing'
}
});
async function googleCallback(token) {
const response = await authStore.loginWithGoogle(JSON.stringify(token));
if (response !== true) {
try {
const response = await authStore.loginWithGoogle(JSON.stringify(token));
if (response === true) {
await router.push(props.returnUrl);
} else {
errorSnackBar.value = true;
}
} catch (error) {
console.error('Login failed:', error);
errorSnackBar.value = true;
}
}

View File

@@ -4,9 +4,12 @@ import {useAuthStore} from "@/stores/authStore.js";
import {useCreatorProfileStore} from "@/stores/creatorProfileStore.js";
import {useUserProfileStore} from "@/stores/userProfileStore.js";
import {useLanguageStore} from "@/stores/languageStore.js";
import {useRouter, useRoute} from 'vue-router';
const {locale, t} = useI18n();
const languageStore = useLanguageStore();
const router = useRouter();
const route = useRoute();
const userProfileStore = useUserProfileStore();
const creatorProfileStore = useCreatorProfileStore();
@@ -18,6 +21,14 @@ function toggleLanguage() {
const nextIndex = (currentIndex + 1) % languages.length;
languageStore.setLocale(languages[nextIndex]);
}
function handleLogout() {
// Check if current route requires authentication
const requiresAuth = route.matched.some(record => record.meta.requiresAuth);
// If on a protected page, redirect to landing, otherwise stay on current page
const redirectTo = requiresAuth ? '/landing' : route.fullPath;
authStore.logout(redirectTo);
}
</script>
<template>
@@ -85,7 +96,7 @@ function toggleLanguage() {
</template>
<div v-else>
<button class="menu-item-action"
@click="authStore.logout">
@click="handleLogout">
<i class="mdi mdi-logout"></i>
<span class="label">{{ t('sidebar.signOut') }}</span>
</button>