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
VueFire exposes the current user as a reactive variable while allowing you to use the Firebase Authentication API as you would normally do.
3
+
[Firebase Authentication](https://firebase.google.com/docs/auth/web/start) makes it really easy to add different kind of authentications to your app and integrate with security rules for Firestore and Database.
4
+
Most of the APIs can be used as you would normally do with Firebase, VueFire exposes a few composables to integrate better with Vue:
4
5
5
6
## Installation
6
7
7
8
Start by adding the `VueFireAuth` module to the `VueFire` plugin:
8
9
9
10
```ts
10
-
app
11
-
.use(VueFire, {
12
-
firebaseApp: createFirebaseApp(),
13
-
modules: [
14
-
// ... other modules
15
-
VueFireAuth(),
16
-
],
17
-
})
11
+
import { VueFire, VueFireAuth } from'vuefire'
12
+
app.use(VueFire, {
13
+
firebaseApp: createFirebaseApp(),
14
+
modules: [
15
+
// ... other modules
16
+
VueFireAuth(),
17
+
],
18
+
})
18
19
```
19
20
20
-
## Get the Current User
21
+
This will automatically create and inject the [Auth module](https://firebase.google.com/docs/auth/web/start#add-initialize-sdk) from Firebase so you can access it from within any component with the `useFirebaseAuth()` composable.
22
+
23
+
## Current User
21
24
22
25
You can get the current user as a reactive variable with the `useCurrentUser()` composable:
23
26
@@ -33,13 +36,27 @@ const user = useCurrentUser()
33
36
</template>
34
37
```
35
38
36
-
## Wait for the user to be loaded
39
+
###Wait for the user to be loaded
37
40
38
41
There is also a `getCurrentUser()` function that returns a promise of the current user. This is useful if you want to wait for the user to be loaded before doing anything. You can, for example, await it within a navigation guard:
39
42
40
43
```ts
41
-
router.beforeEach(async () => {
42
-
awaitgetCurrentUser()
44
+
router.beforeEach(async (to) => {
45
+
// routes with `meta: { requiresAuth: true }` will check for the users, others won't
46
+
if (to.meta.requiresAuth) {
47
+
const currentUser =awaitgetCurrentUser()
48
+
// if the user is not logged in, redirect to the login page
49
+
if (!currentUser) {
50
+
return {
51
+
path: '/login',
52
+
query: {
53
+
// we keep the current path in the query so we can redirect to it after login
54
+
// with `router.push(route.query.redirectTo || '/')`
55
+
redirectTo: to.fullPath,
56
+
},
57
+
}
58
+
}
59
+
}
43
60
})
44
61
```
45
62
@@ -48,3 +65,20 @@ If you are using `getCurrentUser()` in a navigation guard, make sure to add it b
48
65
:::
49
66
50
67
Once the user is loaded, `getCurrentUser()` will immediately resolve the current user.
68
+
69
+
Sometimes, the Firebase SDK might be able to automatically log in the user with a hidden cookie or local storage. In that case, you can automatically redirect the user to the page they were trying to access before being automatically logged in:
70
+
71
+
```ts
72
+
// within the Page component displayed for the `/login` route
0 commit comments