Skip to content

Commit 8976baa

Browse files
committed
docs: nuxt
1 parent 9a37f47 commit 8976baa

File tree

5 files changed

+109
-7
lines changed

5 files changed

+109
-7
lines changed

docs/.vitepress/config.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,16 @@ function sidebarNuxt(): SidebarGroup {
187187
link: '/nuxt/getting-started',
188188
},
189189
{
190-
text: 'Config',
191-
link: '/nuxt/config',
190+
text: 'Authentication',
191+
link: '/nuxt/auth',
192+
},
193+
{
194+
text: 'Server Side Rendering',
195+
link: '/nuxt/ssr',
196+
},
197+
{
198+
text: 'Deployment',
199+
link: '/nuxt/deployment',
192200
},
193201
],
194202
}

docs/nuxt/auth.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Authentication
2+
3+
Nuxt VueFire integrates with [Firebase Authentication](https://firebase.google.com/docs/auth) module to automatically synchronize the current user state on the server and the client.
4+
5+
You can access the current user with the `useCurrentUser()` composable within any component:
6+
7+
```vue{2}
8+
<script setup>
9+
const user = useCurrentUser()
10+
</script>
11+
```
12+
13+
You can also await for the user to be ready in route middleware and other async functions with `getCurrentUser()`. For example, you can create a custom route middleware that only allows authenticated users to access a route:
14+
15+
```ts
16+
// middleware/auth.ts
17+
export default defineNuxtRouteMiddleware(async (to, from) => {
18+
const user = await getCurrentUser()
19+
20+
// redirect the user to the login page
21+
if (!user) {
22+
return navigateTo({
23+
path: '/login',
24+
query: {
25+
redirect: to.fullPath,
26+
},
27+
})
28+
}
29+
})
30+
```
31+
32+
You can then enable this middleware on any `page/` component:
33+
34+
```vue{2-4}
35+
<script setup>
36+
definePageMeta({
37+
middleware: ['auth'],
38+
})
39+
</script>
40+
```
41+
42+
You can even automatically handle the auth state by _watching_ the current user. We recommend you to do this either in a layout or on the `app.vue` component so the watcher is always active:
43+
44+
```vue
45+
<script setup>
46+
const router = useRouter()
47+
const route = useRoute()
48+
const user = useCurrentUser()
49+
50+
// we don't need this watcher on server
51+
onMounted(() => {
52+
watch(user, (user, prevUser) => {
53+
if (prevUser && !user) {
54+
// user logged out
55+
router.push('/login')
56+
} else if (user && typeof route.query.redirect === 'string') {
57+
// user logged in
58+
router.push(route.query.redirect)
59+
}
60+
})
61+
})
62+
</script>
63+
```
File renamed without changes.

docs/nuxt/getting-started.md

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
# Nuxt.js
22

33
::: warning
4-
The Nuxt module and this docs are a WIP. Things might not work yet.
4+
The Nuxt VueFire module is still a work in progress and it might contain breaking changes in the future. If you find any issues, please open an issue on GitHub.
55
:::
66

77
## Installation
88

99
```bash
10-
npm install nuxt-vuefire
10+
npm install vuefire nuxt-vuefire
1111
```
1212

13-
Additionally, if you are using [SSR](https://nuxt.com/docs/api/configuration/nuxt-config/#ssr), you need to install `firebase-admin` and some other peer dependencies:
13+
Additionally, if you are using [SSR](https://nuxt.com/docs/api/configuration/nuxt-config/#ssr), you need to install `firebase-admin` and its peer dependencies:
1414

1515
```bash
1616
npm install firebase-admin @firebase/@app-types
@@ -20,7 +20,7 @@ npm install firebase-admin @firebase/@app-types
2020

2121
Next, add `nuxt-vuefire` to the `modules` section of `nuxt.config.js` and configure it with the credentials created in your app settings in your project overview (`https://console.firebase.google.com/project/YOUR_PROJECT_NAME/overview)`. You can find more details [in Firebase Documentation](https://firebase.google.com/docs/web/setup#create-project). It should look something like this:
2222

23-
```ts
23+
```ts{4,7-15}
2424
export default defineNuxtConfig({
2525
modules: [
2626
// ... other modules
@@ -41,7 +41,7 @@ export default defineNuxtConfig({
4141

4242
If you are using SSR with any auth related feature, you will need to create a [service account](https://firebase.google.com/support/guides/service-accounts) and provide a path to the credentials file in the `serviceAccount` property:
4343

44-
```ts
44+
```ts{5}
4545
export default defineNuxtConfig({
4646
vuefire: {
4747
// ...
@@ -55,3 +55,29 @@ export default defineNuxtConfig({
5555
:::tip
5656
This service account file contains sensitive information and should **not be committed to your repository**.
5757
:::
58+
59+
### Additional configuration
60+
61+
If you are using the [Authentication](https://firebase.google.com/docs/auth) module or [AppCheck](https://firebase.google.com/docs/app-check), make sure to enable them as well:
62+
63+
```ts{3-14}
64+
export default defineNuxtConfig({
65+
// ...
66+
vuefire: {
67+
// ensures the auth module is enabled
68+
auth: true,
69+
appCheck: {
70+
// Allows you to use a debug token in development
71+
debug: process.env.NODE_ENV !== 'production',
72+
isTokenAutoRefreshEnabled: true,
73+
provider: 'ReCaptchaV3',
74+
// Find the instructions in the Firebase documentation, link above
75+
key: '...',
76+
},
77+
},
78+
})
79+
```
80+
81+
## Auto imports
82+
83+
Nuxt VueFire will automatically import the most commonly used functions of `vuefire` so you don't even need to import them in your components ✨.

docs/nuxt/ssr.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Server Side Rendering
2+
3+
Nuxt VueFire works both with and without SSR. You don't need to do anything special to make it work with SSR, everything is handled automatically.
4+
5+
You can read more about SSR in the [VueFire SSR page](../guide/ssr)

0 commit comments

Comments
 (0)