-
-
Notifications
You must be signed in to change notification settings - Fork 342
/
Copy pathauthentication.vue
163 lines (136 loc) · 4.68 KB
/
authentication.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<script lang="ts" setup>
import {
createUserWithEmailAndPassword,
EmailAuthProvider,
linkWithCredential,
signInAnonymously,
signInWithEmailAndPassword,
signInWithPopup,
signInWithRedirect,
signOut,
GoogleAuthProvider,
updateCurrentUser,
updateProfile,
AuthCredential,
getRedirectResult,
} from 'firebase/auth'
import { ref } from 'vue'
import {
updateCurrentUserProfile,
useCurrentUser,
useFirebaseAuth,
} from 'vuefire'
import { googleAuthProvider } from '~/helpers/auth'
// auth is null on the server but it's fine as long as we don't use it. So we force the type to be non-null here because
// auth is only used within methods that are only called on the client
const auth = useFirebaseAuth()!
const user = useCurrentUser()
let credential: AuthCredential | null = null
const route = useRoute()
const router = useRouter()
// automatically redirect the user if they are logged in but was rejected on the server because of an outdated cookie
watch(user, (user) => {
if (
user &&
route.query.redirect &&
typeof route.query.redirect === 'string'
) {
router.push(route.query.redirect)
}
})
// new user
const email = ref('')
const password = ref('')
const tenant = ref<string | null>(null)
async function signUp() {
// link to an existing anonymous account
if (user.value?.isAnonymous) {
credential = EmailAuthProvider.credential(email.value, password.value)
return linkWithCredential(user.value, credential).then(() => {
return signInWithEmailAndPassword(auth, email.value, password.value)
})
}
// create a regular account
return createUserWithEmailAndPassword(auth, email.value, password.value)
}
async function signinPopup() {
return signInWithPopup(auth, googleAuthProvider).then((result) => {
const googleCredential = GoogleAuthProvider.credentialFromResult(result)
credential = googleCredential
const token = googleCredential?.accessToken
console.log('Got Google token', token)
console.log('Got googleCredential', googleCredential)
})
}
async function changeUserImage() {
if (user.value) {
await updateCurrentUserProfile({
photoURL: 'https://i.pravatar.cc/150?u=' + Date.now(),
})
// updateCurrentUserEmail('[email protected]')
}
}
function signinRedirect() {
signInWithRedirect(auth, googleAuthProvider)
}
onMounted(() => {
getRedirectResult(auth).then((creds) => {
console.log('got creds', creds)
if (creds) {
// credential = creds.user.
}
})
})
</script>
<template>
<main>
<h1>Auth playground</h1>
<button data-testid="sign-out" @click="auth.tenantId = null; signOut(auth)">SignOut</button>
<button data-testid="anonymous-sign-in" @click="auth.tenantId = null; signInAnonymously(auth)">Anonymous signIn</button>
<button data-testid="google-popup-sign-in" @click="auth.tenantId = null; signinPopup()">Signin Google (popup)</button>
<button data-testid="google-redirect-sign-in" @click="auth.tenantId = null; signinRedirect()">Signin Google (redirect)</button>
<button data-testid="change-user-picture" @click="changeUserImage">Change User picture</button>
<p>
Tenant: <input v-model="tenant" data-testid="tenant" type="text" placeholder="leave empty for default tenant"/>
</p>
<form @submit.prevent="auth.tenantId = tenant || null; signUp()">
<fieldset>
<legend>New User</legend>
<label> Email: <input v-model="email" data-testid="email-signup" type="email" required /> </label>
<label>
Password: <input v-model="password" data-testid="password-signup" type="password" required />
</label>
<button data-testid="submit-signup">Create</button>
</fieldset>
</form>
<form @submit.prevent="auth.tenantId = tenant || null; signInWithEmailAndPassword(auth, email, password)">
<fieldset>
<legend>Sign in</legend>
<label> Email: <input v-model="email" data-testid="email-signin" type="email" required /> </label>
<label>
Password: <input v-model="password" data-testid="password-signin" type="password" required />
</label>
<button data-testid="submit-signin">Signin</button>
</fieldset>
</form>
<p v-if="user">
Name: {{ user.displayName }} <br />
<img
v-if="user.photoURL"
:src="user.photoURL"
referrerpolicy="no-referrer"
/>
</p>
<hr />
<!-- this is for debug purposes only, displaying it on the server would create a hydration mismatch -->
<ClientOnly>
<p>Current User:</p>
<pre data-testid="user-data-client">{{ user }}</pre>
</ClientOnly>
<ServerOnlyPre
v-if="user"
:data="user"
testid="user-data-server"
/>
</main>
</template>