-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: migrate from lucia to nuxt-auth-utils (#2546)
<!-- ☝️ PR title should follow conventional commits (https://conventionalcommits.org). In particular, the title should start with one of the following types: - docs: 📖 Documentation (updates to the documentation or readme) - fix: 🐞 Bug fix (a non-breaking change that fixes an issue) - feat: ✨ New feature/enhancement (a non-breaking change that adds functionality or improves existing one) - feat!/fix!:⚠️ Breaking change (fix or feature that would cause existing functionality to change) - chore: 🧹 Chore (updates to the build process or auxiliary tools and libraries) --> ### 🔗 Linked issue <!-- If it resolves an open issue, please link the issue here. For example "Resolves #123" --> ### 📚 Description Reasons: - Lucia mainly handles session management, but for this nuxt has h3 - nuxt-auth provides better integration with nuxt - nuxt-auth provides passkey etc - Lucia has some conventions that make it hard to work around it (especially the whole "prisma adapter" thing, that is not quite working well) - doesn't get really better with v4 see #2335 <!-- Describe your changes in detail --> <!-- Why is this change required? What problem does it solve? -->
- Loading branch information
1 parent
9644914
commit bd2e82b
Showing
28 changed files
with
874 additions
and
485 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
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
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
declare module '#auth-utils' { | ||
/** | ||
* The public information about a user stored in the session | ||
*/ | ||
interface User { | ||
id: string | ||
} | ||
|
||
/** | ||
* The public information about the current session | ||
*/ | ||
interface UserSession { | ||
/** | ||
* The secure data associated with the session, only accessible on the server | ||
*/ | ||
server: ServerSessionData | ||
} | ||
|
||
/** | ||
* Private information about the current session, only accessible on the server | ||
* (exposed in an encrypted form to the client) | ||
*/ | ||
interface SecureSessionData {} | ||
|
||
/** | ||
* The data stored for the session on the server | ||
*/ | ||
interface ServerSessionData { | ||
lastActive: Date | ||
} | ||
} | ||
|
||
export {} |
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,38 +1,16 @@ | ||
import { gql } from '@apollo/client/core' | ||
|
||
/** | ||
* Plugin that adds checks if the user is logged in, and redirects her to the login page if not. | ||
*/ | ||
export default defineNuxtRouteMiddleware(async (to, _from) => { | ||
if (to.matched.some((record) => record.meta.requiresAuth)) { | ||
const { $apolloClient } = useNuxtApp() | ||
try { | ||
// TODO: Only call this if we have a session cookie? | ||
const response = await $apolloClient.query({ | ||
query: gql(/* GraphQL */ ` | ||
query CheckLoggedIn { | ||
me { | ||
id | ||
} | ||
} | ||
`), | ||
}) | ||
|
||
// If the user is not authenticated, then redirect | ||
if ( | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access | ||
response.data?.me?.id === undefined || | ||
response.errors !== undefined | ||
) { | ||
return redirectToLogin() | ||
} | ||
} catch { | ||
const { loggedIn } = useUserSession() | ||
if (!loggedIn.value) { | ||
return redirectToLogin() | ||
} | ||
} | ||
}) | ||
|
||
async function redirectToLogin() { | ||
// TODO: Remember the intended url by appending something like ?redirect=context.route.fullPath | ||
return await navigateTo({ path: '/user/login' }) | ||
return await navigateTo('/user/login') | ||
} |
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
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
Oops, something went wrong.