11<script lang="ts" setup>
2+ import { googleAuthProvider } from ' @/firebase'
23import {
34 createUserWithEmailAndPassword ,
45 EmailAuthProvider ,
@@ -8,20 +9,31 @@ import {
89 signInWithPopup ,
910 signInWithRedirect ,
1011 signOut ,
12+ GoogleAuthProvider ,
13+ updateCurrentUser ,
14+ updateProfile ,
15+ AuthCredential ,
16+ getRedirectResult ,
1117} from ' firebase/auth'
1218import { ref } from ' vue'
13- import { useCurrentUser , useFirebaseAuth } from ' vuefire'
19+ import {
20+ updateCurrentUserProfile ,
21+ useCurrentUser ,
22+ useFirebaseAuth ,
23+ } from ' vuefire'
1424
1525const auth = useFirebaseAuth ()
1626const user = useCurrentUser ()
27+ let credential: AuthCredential | null = null
1728
1829// new user
1930const email = ref (' ' )
2031const password = ref (' ' )
2132function signUp() {
2233 // link to an existing anonymous account
2334 if (user .value ?.isAnonymous ) {
24- const credential = EmailAuthProvider .credential (email .value , password .value )
35+ credential = EmailAuthProvider .credential (email .value , password .value )
36+
2537 return linkWithCredential (user .value , credential ).then (() => {
2638 return signInWithEmailAndPassword (auth , email .value , password .value )
2739 })
@@ -30,13 +42,48 @@ function signUp() {
3042 // create a regular account
3143 return createUserWithEmailAndPassword (auth , email .value , password .value )
3244}
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('hello@esm.dev')
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+ })
3376 </script >
3477
3578<template >
3679 <main >
3780 <h1 >Auth playground</h1 >
3881 <button @click =" signOut(auth)" >SignOut</button >
3982 <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+
4087 <form @submit.prevent =" signUp()" >
4188 <fieldset >
4289 <legend >New User</legend >
@@ -64,6 +111,14 @@ function signUp() {
64111 <button >Signin</button >
65112 </fieldset >
66113 </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+
67122 <p >Current User:</p >
68123 <pre >{{ user }}</pre >
69124 </main >
0 commit comments