Skip to content

Commit 46e7716

Browse files
committed
docs: auth signin
1 parent f802558 commit 46e7716

File tree

1 file changed

+98
-11
lines changed

1 file changed

+98
-11
lines changed

Diff for: docs/guide/auth.md

+98-11
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,83 @@ const auth = useFirebaseAuth()
3030
</script>
3131
```
3232

33+
This is necessary if you want to use the [Firebase Auth API](https://firebase.google.com/docs/auth/web/start#sign_in_existing_users) to sign in users, create users, etc:
34+
35+
```vue{7,9,14,22}
36+
<script setup>
37+
import {
38+
getRedirectResult,
39+
signInWithRedirect,
40+
signOut,
41+
} from 'firebase/auth'
42+
import { useCurrentUser, useFirebaseAuth } from 'vuefire'
43+
44+
const auth = useFirebaseAuth()! // only exists on client side
45+
46+
// display errors if any
47+
const error = ref<Error | null>(null)
48+
function signinRedirect() {
49+
signInWithRedirect(auth, someAuthProvider).catch((reason) => {
50+
console.error('Failed signinRedirect', reason)
51+
error.value = reason
52+
})
53+
}
54+
55+
// only on client side
56+
onMounted(() => {
57+
getRedirectResult(auth).catch((reason) => {
58+
console.error('Failed redirect result', reason)
59+
error.value = reason
60+
})
61+
})
62+
</script>
63+
64+
<template>
65+
<main>
66+
<ErrorBox v-if="error" :error="error" />
67+
<button @click="signinRedirect()">SignIn with Google</button>
68+
</main>
69+
</template>
70+
```
71+
72+
::: tip
73+
`useFirebaseAuth()` is `null` on the server side, so if you are using TypeScript, you might need to add a `!` to assert that it's not null:
74+
75+
```ts
76+
const auth = useFirebaseAuth()!
77+
```
78+
79+
:::
80+
81+
### Providers
82+
83+
There are multiple ways to add the auth providers to your app like exporting a `new GoogleAuthProvider()` instance from the file where we initialize Firebase. Another way is to create it directly in the component where you need it. make sure to add it into a regular `<script>` since each `<script setup>` is scoped to a component instance:
84+
85+
```vue{1-4,17}
86+
<script lang="ts">
87+
import { GoogleAuthProvider } from 'firebase/auth'
88+
export const googleAuthProvider = new GoogleAuthProvider()
89+
</script>
90+
91+
<script setup>
92+
import {
93+
signInWithPopup,
94+
signOut,
95+
} from 'firebase/auth'
96+
import { useCurrentUser, useFirebaseAuth } from 'vuefire'
97+
98+
//...
99+
100+
function signinPopup() {
101+
error.value = null
102+
signInWithPopup(auth, googleAuthProvider).catch((reason) => {
103+
console.error('Failed sign', reason)
104+
error.value = reason
105+
})
106+
}
107+
</script>
108+
```
109+
33110
## Current User
34111

35112
You can get the current user as a reactive variable with the `useCurrentUser()` composable:
@@ -78,19 +155,29 @@ If you are using `getCurrentUser()` in a navigation guard, make sure to add it b
78155

79156
Once the user is loaded, `getCurrentUser()` will immediately resolve the current user.
80157

81-
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:
158+
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. You can even redirect them to the login page if they logout:
82159

83160
```ts
84-
// within the Page component displayed for the `/login` route
85-
onMounted(async () => {
86-
const currentUser = await getCurrentUser()
87-
if (currentUser) {
88-
const to =
89-
route.query.redirect && typeof route.query.redirect === 'string'
90-
? route.query.redirect
91-
: '/'
92-
93-
router.push(to)
161+
// App.vue
162+
const user = useCurrentUser()
163+
const router = useRouter()
164+
const route = useRoute()
165+
166+
watch(user, async (currentUser, previousUser) => {
167+
// redirect to login if they logout and the current
168+
// route is only for authenticated users
169+
if (
170+
!currentUser &&
171+
previousUser &&
172+
isCurrentRouteAuthenticated(route)
173+
) {
174+
return router.push({ name: 'login' })
175+
}
176+
// redirect the user if they are logged in but were
177+
// rejected because the user wasn't ready yet, logged in
178+
// then got back to this page
179+
if (currentUser && typeof route.query.redirect === 'string') {
180+
return router.push(route.query.redirect)
94181
}
95182
})
96183
```

0 commit comments

Comments
 (0)