-
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved code examples for nuxt + vue3 + kratos.
- Loading branch information
Showing
4 changed files
with
76 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,50 @@ | ||
import { serverCheckAuth } from "~/server/utils/checkAuth"; | ||
import { useAuth } from "~/composables/useAuth"; | ||
import type { RouteLocationNormalizedGeneric } from "vue-router"; | ||
|
||
export default defineNuxtRouteMiddleware(async (to) => { | ||
if (import.meta.server) { | ||
if (!to.path.startsWith("/api")) { | ||
return; | ||
} | ||
|
||
const event = useRequestEvent(); | ||
if (!event) { | ||
console.error('Request event is undefined'); | ||
return; | ||
} | ||
const session = await serverCheckAuth(event); | ||
|
||
try { | ||
const session = await serverCheckAuth(event); | ||
if (!session && to.meta.requiresAuth !== false && to.meta.guestOnly !== true) { | ||
return navigateTo("/login"); | ||
} | ||
if (session && to.meta.guestOnly) { | ||
return navigateTo("/"); | ||
} | ||
} catch (error) { | ||
console.error('Server-side auth check failed:', error); | ||
return navigateTo("/login"); | ||
} | ||
} else { | ||
return checkRoute(session, to); | ||
} | ||
|
||
if (import.meta.client) { | ||
const { checkAuth, isAuthenticated, isLoading } = useAuth(); | ||
|
||
await checkAuth(); | ||
|
||
if (isLoading.value) { | ||
return; | ||
} | ||
|
||
if (to.meta.requiresAuth !== false && to.meta.guestOnly !== true && !isAuthenticated.value) { | ||
return navigateTo("/login"); | ||
} | ||
|
||
if (to.meta.guestOnly && isAuthenticated.value) { | ||
return navigateTo("/"); | ||
} | ||
return checkRoute(isAuthenticated.value, to); | ||
} | ||
}); | ||
|
||
const checkRoute = ( | ||
session: boolean, | ||
route: RouteLocationNormalizedGeneric | ||
) => { | ||
if ( | ||
!session && | ||
route.meta.requiresAuth !== false && | ||
route.meta.guestOnly !== true | ||
) { | ||
return navigateTo("/login"); | ||
} | ||
|
||
if (session && route.meta.guestOnly) { | ||
return navigateTo("/"); | ||
} | ||
|
||
return; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,33 @@ | ||
import { getCookie } from "h3"; | ||
import { useRuntimeConfig } from "#imports"; | ||
import type { H3Event } from "h3"; | ||
import { Configuration, FrontendApi } from "@ory/kratos-client"; | ||
|
||
const createKratosClient = (basePath: string) => | ||
new FrontendApi( | ||
new Configuration({ | ||
basePath, | ||
}) | ||
); | ||
|
||
export async function serverCheckAuth(event: H3Event) { | ||
const config = useRuntimeConfig(); | ||
const cookie = getCookie(event, "ory_kratos_session"); | ||
const client = createKratosClient(config.ORY_SDK_URL); | ||
|
||
if (!cookie) { | ||
console.warn("No session cookie found"); | ||
return null; | ||
} | ||
|
||
try { | ||
const response = await fetch(`${config.ORY_SDK_URL}/sessions/whoami`, { | ||
method: "GET", | ||
headers: { | ||
Cookie: `ory_kratos_session=${cookie}`, | ||
Accept: "application/json", | ||
}, | ||
return client | ||
.toSession({ | ||
xSessionToken: cookie, | ||
}) | ||
.then(({ data, status }) => { | ||
if (status !== 200) { | ||
console.error(`HTTP error! status: ${status}`); | ||
return false; | ||
} | ||
return !!data.active && !!data.id && !!data.identity; | ||
}) | ||
.catch((error) => { | ||
console.error("Error checking authentication:", error); | ||
return false; | ||
}); | ||
|
||
if (!response.ok) { | ||
console.error(`HTTP error! status: ${response.status}`); | ||
return null; | ||
} | ||
|
||
return await response.json(); | ||
} catch (error) { | ||
console.error("Error checking authentication:", error); | ||
return null; | ||
} | ||
} |