-
Notifications
You must be signed in to change notification settings - Fork 497
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
feat(nuxt): Add Nuxt SDK docs #1728
Merged
wobsoriano
merged 26 commits into
rob/eco-224-vue-sdk-references
from
rob/eco-224-nuxt-quickstart
Dec 19, 2024
Merged
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
e7459ad
feat(nuxt): Add initial quickstart and reference docs
wobsoriano a68f129
chore: More nuxt SDK docs updates
wobsoriano 04a8b09
docs(nuxt): Add clerkMiddleware reference
wobsoriano e679860
docs(nuxt): Add clerkMiddleware link to quickstart
wobsoriano b78489d
docs(nuxt): Add route middleware example
wobsoriano 7bc65ed
docs(nuxt): Add clerkMiddleware reference to overview
wobsoriano 026108a
docs(nuxt): Add quickstart overview and homepage entry
wobsoriano a81cc4a
add placeholder icon
wobsoriano 317f0a2
add nuxt svgs
wobsoriano b180c9b
docs(nuxt): Add backend references for clerkClient
wobsoriano d189d22
docs(nuxt): Add missing async keyword
wobsoriano feb1463
make nuxt icon a bit bigger
bradlc 47e4bc6
docs(nuxt): Fix example for protecting pages
wobsoriano 0fbe641
docs(nuxt): Add link to creating a Nuxt app
wobsoriano 162d9f2
Update protecting-pages.mdx
wobsoriano 6cc2acd
fix conflicts
wobsoriano 07f5bd9
docs(nuxt): Replace express with nuxt
wobsoriano 9b1ea2c
fix links
wobsoriano ab341dc
Merge branch 'rob/eco-224-vue-sdk-references' into rob/eco-224-nuxt-q…
wobsoriano 028b4be
Merge branch 'rob/eco-224-vue-sdk-references' into rob/eco-224-nuxt-q…
wobsoriano ad7a401
Merge branch 'rob/eco-224-vue-sdk-references' into rob/eco-224-nuxt-q…
victoriaxyz f70089d
Merge branch 'rob/eco-224-vue-sdk-references' into rob/eco-224-nuxt-q…
victoriaxyz 4fb15b5
update
victoriaxyz ff4bded
Merge branch 'rob/eco-224-vue-sdk-references' into rob/eco-224-nuxt-q…
victoriaxyz 50e5073
docs review
alexisintech c66fd2d
update authz examples
alexisintech File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
The `useAuth()` composable provides access to the current authentication state and methods to manage the active session. You can use this composable to protect [pages](/docs/references/nuxt/protect-pages). | ||
|
||
In the following example, the `isLoaded` property checks if Clerk has finished initializing and the `userId` property checks if the user is signed in. | ||
|
||
```vue {{ filename: 'pages/protected-page.vue' }} | ||
<script setup> | ||
const { userId, isLoaded } = useAuth() | ||
</script> | ||
|
||
<template> | ||
<div v-if="!isLoaded">Loading...</div> | ||
<div v-else-if="!userId">Sign in to access this page</div> | ||
<div v-else>Hello, {{ userId }}!</div> | ||
</template> | ||
``` |
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,54 @@ | ||
--- | ||
title: Protect pages in your Nuxt app with Clerk | ||
description: Learn how to protect the pages in your Clerk + Nuxt application. | ||
--- | ||
|
||
There are two ways to protect pages in your Nuxt application: | ||
|
||
- [Use the `useAuth()` composable](#use-use-auth) | ||
- [Use `defineNuxtRouteMiddleware()`](#use-define-nuxt-route-middleware) | ||
|
||
> [!NOTE] | ||
> To learn how to protect API routes, see the [dedicated guide](/docs/references/nuxt/clerk-middleware#protect-api-routes). | ||
|
||
## Use `useAuth()` | ||
|
||
<Include src="_partials/nuxt/use-auth" /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TIL 👀 |
||
|
||
## Use `defineNuxtRouteMiddleware()` | ||
|
||
The [`defineNuxtRouteMiddleware()`](https://nuxt.com/docs/api/utils/define-nuxt-route-middleware) utility function helps protect pages in your Nuxt application by validating authentication on the client-side. This middleware integrates seamlessly with Clerk authentication. | ||
|
||
<Steps> | ||
### Configure `defineNuxtRouteMiddleware()` | ||
|
||
In your `middleware/` directory, create a file named `auth.ts` with the following code. This middleware uses the `useAuth()` composable to check if the user is signed in. If they aren't, the middleware redirects them to the sign-in page. | ||
|
||
```ts {{ filename: 'middleware/auth.ts' }} | ||
export default defineNuxtRouteMiddleware(() => { | ||
const { userId } = useAuth() | ||
|
||
// If the user is not signed in, redirect to the sign-in page | ||
if (!userId.value) { | ||
return navigateTo('/sign-in') | ||
} | ||
}) | ||
``` | ||
|
||
### Protect pages with `defineNuxtRouteMiddleware()` | ||
|
||
To protect a page, add the middleware to the `definePageMeta()` function. In the last step, you stored the middleware in the `auth.ts` file, so you would pass `auth` in the `middleware` array. | ||
|
||
```vue {{ filename: 'pages/dashboard.vue' }} | ||
<script setup lang="ts"> | ||
definePageMeta({ | ||
// `auth` is the name of the middleware file | ||
middleware: ['auth'], | ||
}) | ||
</script> | ||
|
||
<template> | ||
<h1>Dashboard page</h1> | ||
</template> | ||
``` | ||
</Steps> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can also use the
role
property here @alexisintech? Something lik,e!has({ role: 'org:admin' })
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
c66fd2d