-
Notifications
You must be signed in to change notification settings - Fork 625
Open
Description
I'm using Firebase Auth and NextJS with Firebase hosting.
Goal is to redirect / rewrite based on whether user has signed in or not.
I set listen to IdTokenChange and set __session cookie.
// AuthUserContext.tsx
// listen to auth state change
useEffect(() => {
const onNext = async (user: User | null) => {
if (user) {
setUser(user);
const idToken = await user.getIdToken();
nookies.set(undefined, "__session", idToken, { path: "/" });
} else {
setUser(null);
nookies.set(undefined, "__session", "", { path: "/" });
}
};
const unsubscribe = onIdTokenChanged(auth, onNext);
return unsubscribe;
}, []);
I then try to get __session cookie from middleware
// middleware.ts
import { parseCookies } from "nookies";
export function middleware(request: NextRequest) {
const cookies = parseCookies();
console.log(cookies, "cookies"); // empty
const cookie = request.cookies.get("__session")?.value;
console.log(cookie, "cookie"); // undefined
// more code
}
Am I missing something? Or does middleware have no access to cookies?
Metadata
Metadata
Assignees
Labels
No labels