forked from posva/nuxt--vuefire-example-spark-plan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.vue
135 lines (118 loc) · 3.03 KB
/
app.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
<script lang="ts" setup>
import {
doc,
getDoc,
serverTimestamp,
setDoc,
updateDoc,
} from 'firebase/firestore'
import { updateProfile } from 'firebase/auth'
const db = useFirestore()
const user = useCurrentUser()
const {
public: { vuefireVersion, nuxtVuefireVersion },
} = useRuntimeConfig()
const router = useRouter()
const route = useRoute()
watch(user, async (currentUser, previousUser) => {
// redirect to login if they logout and the current route is only for authenticated users
if (
!currentUser &&
previousUser &&
(Array.isArray(route.meta.middleware)
? route.meta.middleware.includes('authenticated')
: route.meta.middleware === 'authenticated')
) {
return router.push({ name: 'login' })
}
// redirect the user if they are logged in but were rejected because the user wasn't ready yet
if (currentUser && typeof route.query.redirect === 'string') {
return router.push(route.query.redirect)
}
// update user info
if (currentUser) {
console.log('Updating user info...')
const userDoc = doc(db, 'users', currentUser.uid)
const userData = {
displayName: currentUser.displayName || 'Anonymous',
photoURL: currentUser.photoURL,
lastLogin: serverTimestamp(),
}
// fallback photo for dev with emulators or anonymous users
if (currentUser.isAnonymous || process.dev) {
userData.displayName ??= 'Anonymous'
userData.photoURL ??= `https://i.pravatar.cc/150?u=${currentUser.uid}`
updateProfile(currentUser, {
displayName: userData.displayName,
photoURL: userData.photoURL,
})
}
// only create entries for real users
if (!currentUser.isAnonymous) {
const existingUser = await getDoc(userDoc)
if (existingUser.exists()) {
await updateDoc(userDoc, userData)
} else {
await setDoc(userDoc, {
...userData,
joinedAt: serverTimestamp(),
})
}
console.log('User updated')
}
}
})
</script>
<template>
<div>
<a href="https://nuxt.com" target="_blank">
<img src="@/assets/nuxt.svg" class="logo" alt="Nuxt logo" />
</a>
<a href="https://vuefire.vuejs.org" target="_blank">
<img src="/vuefire.svg" class="logo vuefire" alt="VueFire logo" />
</a>
</div>
<h1>Nuxt + VueFire</h1>
<NavigationLinks />
<NuxtPage />
<footer>
<a href="https://github.com/posva/nuxt--vuefire-example-spark-plan">
<img
src="@/assets/github-mark.svg"
alt="GitHub logo"
class="logo github"
/>
Source Code
</a>
</footer>
</template>
<style scoped>
.logo {
height: 6em;
padding: 1.5em;
will-change: filter;
transition: filter ease-out 0.3s;
}
.logo.vuefire {
height: 7.5em;
}
.logo:hover {
filter: drop-shadow(0 0 2em #00dc82aa);
}
.logo.vuefire:hover {
filter: drop-shadow(0 0 2em #f78200aa);
}
@media screen and (max-width: 425px) {
.logo {
height: 4em;
padding: 0.5em;
}
.logo.vuefire {
height: 5em;
}
}
.logo.github {
height: 1em;
padding: 0 0.15em;
}
</style>