Skip to content

Commit 019b7aa

Browse files
committed
docs: add auth user docs
1 parent 45cf4a2 commit 019b7aa

File tree

1 file changed

+47
-13
lines changed

1 file changed

+47
-13
lines changed

docs/guide/auth.md

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
# Firebase Authentication
22

3-
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:
45

56
## Installation
67

78
Start by adding the `VueFireAuth` module to the `VueFire` plugin:
89

910
```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+
})
1819
```
1920

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
2124

2225
You can get the current user as a reactive variable with the `useCurrentUser()` composable:
2326

@@ -33,13 +36,27 @@ const user = useCurrentUser()
3336
</template>
3437
```
3538

36-
## Wait for the user to be loaded
39+
### Wait for the user to be loaded
3740

3841
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:
3942

4043
```ts
41-
router.beforeEach(async () => {
42-
await getCurrentUser()
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 = await getCurrentUser()
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+
}
4360
})
4461
```
4562

@@ -48,3 +65,20 @@ If you are using `getCurrentUser()` in a navigation guard, make sure to add it b
4865
:::
4966

5067
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
73+
onMounted(async () => {
74+
const currentUser = await getCurrentUser()
75+
if (currentUser) {
76+
const to =
77+
route.query.redirectTo && typeof route.query.redirectTo === 'string'
78+
? route.query.redirectTo
79+
: '/'
80+
81+
router.push(to)
82+
}
83+
})
84+
```

0 commit comments

Comments
 (0)