-
-
Notifications
You must be signed in to change notification settings - Fork 322
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
191 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<script setup lang="ts"> | ||
import { doc, getDoc } from 'firebase/firestore' | ||
import { useCurrentUser, useDocument, useFirestore, usePendingPromises } from 'vuefire' | ||
import { ref } from 'vue' | ||
const db = useFirestore() | ||
const user = useCurrentUser() | ||
console.log(user.value?.uid) | ||
const secretRef = computed(() => user.value ? doc(db, 'secrets', user.value.uid) : null) | ||
const secret = useDocument(secretRef) | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<p v-if="!user"> | ||
Log in in the authentication page to test this. | ||
</p> | ||
<template v-else> | ||
<p>Secret Data for user {{ user.displayName }} ({{ user.uid }})</p> | ||
<pre>{{ secret }}</pre> | ||
</template> | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { FirebaseApp } from 'firebase/app' | ||
import { User } from 'firebase/auth' | ||
import { UserRecord } from 'firebase-admin/auth' | ||
import { App, ref } from 'vue' | ||
import { authUserMap } from '../auth/user' | ||
import { getGlobalScope } from '../globals' | ||
import { _Nullable } from '../shared' | ||
|
||
export function VueFireAuthServer( | ||
firebaseApp: FirebaseApp, | ||
app: App, | ||
userRecord: _Nullable<UserRecord> | ||
) { | ||
const user = getGlobalScope(firebaseApp, app).run(() => | ||
ref<_Nullable<User>>(createServerUser(userRecord)) | ||
)! | ||
authUserMap.set(firebaseApp, user) | ||
} | ||
|
||
/** | ||
* Creates a user object that is compatible with the client but will throw errors when its functions are used as they | ||
* shouldn't be called within in the server. | ||
* | ||
* @param userRecord - user data from firebase-admin | ||
*/ | ||
function createServerUser(userRecord: _Nullable<UserRecord>): _Nullable<User> { | ||
if (!userRecord) return null | ||
const user = userRecord.toJSON() as UserRecord | ||
|
||
return { | ||
...user, | ||
// these seem to be type mismatches within firebase source code | ||
tenantId: user.tenantId || null, | ||
displayName: user.displayName || null, | ||
photoURL: user.photoURL || null, | ||
email: user.email || null, | ||
phoneNumber: user.phoneNumber || null, | ||
|
||
delete: InvalidServerFunction('delete'), | ||
getIdToken: InvalidServerFunction('getIdToken'), | ||
getIdTokenResult: InvalidServerFunction('getIdTokenResult'), | ||
reload: InvalidServerFunction('reload'), | ||
toJSON: InvalidServerFunction('toJSON'), | ||
get isAnonymous() { | ||
return warnInvalidServerGetter('isAnonymous', false) | ||
}, | ||
get refreshToken() { | ||
return warnInvalidServerGetter('refreshToken', '') | ||
}, | ||
get providerId() { | ||
return warnInvalidServerGetter('providerId', '') | ||
}, | ||
} | ||
} | ||
|
||
// function helpers to warn on wrong usage on server | ||
|
||
/** | ||
* Creates a function that throws an error when called. | ||
* | ||
* @param name - name of the function | ||
*/ | ||
function InvalidServerFunction(name: string) { | ||
return () => { | ||
throw new Error( | ||
`The function User.${name}() is not available on the server.` | ||
) | ||
} | ||
} | ||
|
||
/** | ||
* Creates a getter that warns when called and return a fallback value. | ||
* | ||
* @param name - name of the getter | ||
* @param value - value to return | ||
*/ | ||
|
||
function warnInvalidServerGetter<T>(name: string, value: T) { | ||
console.warn( | ||
`The getter User.${name} is not available on the server. It will return ${String( | ||
value | ||
)}.` | ||
) | ||
return value | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { VueFireAppCheckServer } from './app-check' | ||
export { VueFireAuthServer } from './auth' |