forked from NdoleStudio/httpsms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.ts
More file actions
29 lines (27 loc) · 817 Bytes
/
user.ts
File metadata and controls
29 lines (27 loc) · 817 Bytes
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
import { Context } from '@nuxt/types'
import { User, Auth } from 'firebase/auth'
import { AuthUser as StateUser } from '~/store'
import { setAuthHeader } from '~/plugins/axios'
export default async function (context: Context) {
await setUser(context)
}
const setUser = (context: Context): Promise<User | null> => {
return new Promise((resolve, reject) => {
const unsubscribe = (context.app.$fire.auth as Auth).onAuthStateChanged(
async (user) => {
unsubscribe()
let stateUser: StateUser | null = null
if (user) {
stateUser = {
id: user.uid,
}
setAuthHeader(await user.getIdToken())
}
context.store.dispatch('setAuthUser', stateUser).finally(() => {
resolve(user)
})
},
reject
)
})
}