1
1
<script lang="ts" setup>
2
+ import { googleAuthProvider } from ' @/firebase'
2
3
import {
3
4
createUserWithEmailAndPassword ,
4
5
EmailAuthProvider ,
@@ -8,20 +9,31 @@ import {
8
9
signInWithPopup ,
9
10
signInWithRedirect ,
10
11
signOut ,
12
+ GoogleAuthProvider ,
13
+ updateCurrentUser ,
14
+ updateProfile ,
15
+ AuthCredential ,
16
+ getRedirectResult ,
11
17
} from ' firebase/auth'
12
18
import { ref } from ' vue'
13
- import { useCurrentUser , useFirebaseAuth } from ' vuefire'
19
+ import {
20
+ updateCurrentUserProfile ,
21
+ useCurrentUser ,
22
+ useFirebaseAuth ,
23
+ } from ' vuefire'
14
24
15
25
const auth = useFirebaseAuth ()
16
26
const user = useCurrentUser ()
27
+ let credential: AuthCredential | null = null
17
28
18
29
// new user
19
30
const email = ref (' ' )
20
31
const password = ref (' ' )
21
32
function signUp() {
22
33
// link to an existing anonymous account
23
34
if (user .value ?.isAnonymous ) {
24
- const credential = EmailAuthProvider .credential (email .value , password .value )
35
+ credential = EmailAuthProvider .credential (email .value , password .value )
36
+
25
37
return linkWithCredential (user .value , credential ).then (() => {
26
38
return signInWithEmailAndPassword (auth , email .value , password .value )
27
39
})
@@ -30,13 +42,48 @@ function signUp() {
30
42
// create a regular account
31
43
return createUserWithEmailAndPassword (auth , email .value , password .value )
32
44
}
45
+
46
+ function signinPopup() {
47
+ return signInWithPopup (auth , googleAuthProvider ).then ((result ) => {
48
+ const googleCredential = GoogleAuthProvider .credentialFromResult (result )
49
+ credential = googleCredential
50
+ const token = googleCredential ?.accessToken
51
+ console .log (' Got Google token' , token )
52
+ console .log (' Got googleCredential' , googleCredential )
53
+ })
54
+ }
55
+
56
+ async function changeUserImage() {
57
+ if (user .value ) {
58
+ await updateCurrentUserProfile ({
59
+ photoURL: ' https://i.pravatar.cc/150?u=' + Date .now (),
60
+ })
61
+
62
+ // updateCurrentUserEmail('[email protected] ')
63
+ }
64
+ }
65
+
66
+ function signinRedirect() {
67
+ signInWithRedirect (auth , googleAuthProvider )
68
+ }
69
+
70
+ getRedirectResult (auth ).then ((creds ) => {
71
+ console .log (' got creds' , creds )
72
+ if (creds ) {
73
+ // credential = creds.user.
74
+ }
75
+ })
33
76
</script >
34
77
35
78
<template >
36
79
<main >
37
80
<h1 >Auth playground</h1 >
38
81
<button @click =" signOut(auth)" >SignOut</button >
39
82
<button @click =" signInAnonymously(auth)" >Anonymous signIn</button >
83
+ <button @click =" signinPopup()" >Signin Google (popup)</button >
84
+ <button @click =" signinRedirect()" >Signin Google (redirect)</button >
85
+ <button @click =" changeUserImage" >Change User picture</button >
86
+
40
87
<form @submit.prevent =" signUp()" >
41
88
<fieldset >
42
89
<legend >New User</legend >
@@ -64,6 +111,14 @@ function signUp() {
64
111
<button >Signin</button >
65
112
</fieldset >
66
113
</form >
114
+
115
+ <p v-if =" user" >
116
+ Name: {{ user.displayName }} <br />
117
+ <img v-if =" user.photoURL" :src =" user.photoURL" />
118
+ </p >
119
+
120
+ <hr />
121
+
67
122
<p >Current User:</p >
68
123
<pre >{{ user }}</pre >
69
124
</main >
0 commit comments