Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local auth example improvements #27

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions auth/local/auth/composables/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export const authLogin = async (email: string, password: string) => {
password: password,
},
});
useAuth().redirectTo.value = null;
const { redirectTo } = useRoute().query;
await useAuth().updateSession();
await navigateTo(useAuth().redirectTo.value || "/");
await navigateTo(String(redirectTo) || "/");
};

export const authRegister = async (email: string, password: string) => {
Expand Down
2 changes: 1 addition & 1 deletion auth/local/auth/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default defineNuxtConfig({
},
nitro: {
storage: {
".data:auth": { driver: "fs", base: "./.data/auth" },
"db:auth": { driver: "fs", base: "./.data/auth" },
},
},
});
20 changes: 8 additions & 12 deletions auth/local/auth/plugins/0.auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,21 @@ export default defineNuxtPlugin(async (nuxtApp) => {

const loggedIn: any = computed(() => !!session.value?.email);

// Create a ref to know where to redirect the user when logged in
const redirectTo = useState("authRedirect")

/**
* Add global route middleware to protect pages using:
*
*
* definePageMeta({
* auth: true
* })
*/
//
//

addRouteMiddleware(
"auth",
(to) => {
(to, from) => {
if (to.meta.auth && !loggedIn.value) {
redirectTo.value = to.path
return "/login";
if (process.server && to.path === from.path) return;
return `/login?redirectTo=${to.path}`;
}
},
{ global: true }
Expand All @@ -39,22 +36,21 @@ export default defineNuxtPlugin(async (nuxtApp) => {
if (process.client) {
watch(loggedIn, async (loggedIn) => {
if (!loggedIn && currentRoute.meta.auth) {
redirectTo.value = currentRoute.path
await navigateTo("/login");
await navigateTo(`/login?redirectTo=${currentRoute.path}`);
}
});
}

if (loggedIn.value && currentRoute.path === "/login") {
await navigateTo(redirectTo.value || "/");
const to = String(currentRoute.query.redirectTo) || '/';
await navigateTo(to);
}

return {
provide: {
auth: {
loggedIn,
session,
redirectTo,
updateSession,
},
},
Expand Down
2 changes: 1 addition & 1 deletion auth/local/pages/login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const isValid = computed(() => {
return email.value && password.value;
});

const redirectTo = useAuth().redirectTo.value
const { redirectTo } = useRoute().query;
const alert = ref(
`Please login or register ${redirectTo ? `to access ${redirectTo}` : ""}`
);
Expand Down
2 changes: 1 addition & 1 deletion auth/local/pages/profile.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ definePageMeta({
auth: true,
});

const { data: session } = await useFetch("/api/auth/session", { headers: useRequestHeaders(['cookie'])});
const { data: session } = await useFetch("/api/auth/session");
</script>

<template>
Expand Down