#tech
Nuxt3
Firebase
- https://www.memory-lovers.blog/entry/2022/10/14/103000
// ログイン後に元のページへ戻る const to = useRoute().redirectedFrom?.fullPath || '/' navigateTo(to, { redirectCode: 302 })
import { onAuthStateChanged, getAuth } from "firebase/auth";
export default defineNuxtRouteMiddleware((to, from) => {
console.log({ to }, { from });
onAuthStateChanged(getAuth(), (user) => {
console.log({ user });
if (!user) {
// ログインしていない場合はログインページにリダイレクトする
navigateTo("/login");
} else {
const to = from.redirectedFrom?.fullPath || "/home";
console.log({ to });
console.log(from.fullPath);
navigateTo(from.fullPath, { redirectCode: 302 });
}
})
});