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
Copy file name to clipboardExpand all lines: docs/guide/auth.md
+98-11
Original file line number
Diff line number
Diff line change
@@ -30,6 +30,83 @@ const auth = useFirebaseAuth()
30
30
</script>
31
31
```
32
32
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
<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'
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
78
155
79
156
Once the user is loaded, `getCurrentUser()` will immediately resolve the current user.
80
157
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:
82
159
83
160
```ts
84
-
// within the Page component displayed for the `/login` route
0 commit comments