How to use SSR Firebase Authentication in Nuxt-js with Middleware?

Viewed 43

In my project , user can login in system . If users do not choose "Remember me " option in beginning, their session will end after page closed entirely.

My firebase works Client Side

in firebase.js

export default ({ store, $storage }) => {
  onAuthStateChanged(auth, async user => { 

    if (user && !user.isAnonymous) {

      store.commit('firebase-auth/setUser', user);
 
      if (user.emailVerified) {
        $storage.setCookie('firebase_token', user.accessToken);
      }
    } else {
      enableGoogleOneTapSignIn(user);
    }
  });
};

in MiddleWare auth.js

export default ({ $storage, req, redirect }) => {
  if (process.server) {
    const token = !!req.headers.cookie.firebase_token;

    if (!token) return redirect('/');
  } else if (process.client) {
    const token = !!$storage.getCookie('firebase_token');

    if (!token) return redirect('/');
  }
};

This code controls if user signed in or not . This works in server and client side.

The problem is ; When user chose "remember me" option at the beginning , session will not finish even if user closes entire browser. And if user opens the browser again , his session will continue and he will be logged-in already but in this scenario I can not reach cookie because it was deleted when browser was closed .

If user comes with a direct link like abc.com/user/my-profile when browser was closed, user will not reach his profile beacuse cookie was cleaned . I need to find a good way for this problem .

0 Answers
Related