I am trying to use cookie based auth in addition to the normal clientside firebase auth for my Nextjs application. I have read around the internet and I have come across a few blogs like this one https://dev.to/ariburaco/authentication-with-firebase-in-nextjs-with-ssr-e76 that suggest I can do something like this to accomplish my goal:
useEffect(() => {
const unsubscribe = onIdTokenChanged(auth, async (user) => {
if (user) {
setAdmin(user);
const token = await user.getIdToken();
nookies.set(undefined, 'token', token, { path: '/' });
} else {
setAdmin(null);
nookies.set(undefined, 'token', '', { path: '/' });
}
});
return unsubscribe;
}, []);
However, when I look at the firebase docs I see a completely different approach that involves sending a request to a server for the cookie. Also, the firebase docs signs the user out after receiving the cookie in order the clear the client side token from storage. Does this mean that you cannot use both cookies and a client side token? or are their disadvantages to doing this?
Finally, which approach is "right"? What are the disadvantages and advantages of each?
Any help is greatly appreciated!