There are a lot of examples on how to use useCurrentUser
with Nuxt3 and vuefire, however, the only example I found for getCurrentUser
was in a middleware, not on a page.
So, I have a middleware that protects a page.
// middleware/auth.ts
export default defineNuxtRouteMiddleware(async (to, from) => {
const user = await getCurrentUser();
// redirect the user to the login page
if (!user) {
return await navigateTo({
path: "/",
query: {
redirect: to.fullPath,
},
});
}
});
And pages/my-page.vue
<script setup lang="ts">
definePageMeta({
showHeader: true,
middleware: ["auth"]
});
const _auth = useFirebaseAuth();
const db = useFirestore();
const user = await getCurrentUser();
if (!user) {
throw new Error("No user?");
}
const idToken = await user.getIdToken();
// Use the token for API calls etc etc etc
</script>
Since I can reach this page, I obviously have a user
. But this page would return No user?
error.
I can’t figure it out.
P.S. Replacing the call with const user = useCurrentUsers
works, but I want to await the user before continuing with the process…