You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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') {
Copy file name to clipboardExpand all lines: docs/nuxt/getting-started.md
+31-5Lines changed: 31 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,16 +1,16 @@
1
1
# Nuxt.js
2
2
3
3
::: 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.
5
5
:::
6
6
7
7
## Installation
8
8
9
9
```bash
10
-
npm install nuxt-vuefire
10
+
npm install vuefire nuxt-vuefire
11
11
```
12
12
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:
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:
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:
This service account file contains sensitive information and should **not be committed to your repository**.
57
57
:::
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 ✨.
0 commit comments