LoginSignup
6
8

More than 3 years have passed since last update.

[Nuxt.js + Firebase] ログアウト中にログイン必須ページにアクセスした時に一瞬表示される

Posted at

はじめに

Firebase Authenticationsのログインチェック処理を入れたけど、ログアウト中にアクセスすると一瞬だけログイン必須ページが表示されてしまった。

middleware/authenticated.js
export default (context) => {
  return context.app.$firebase.auth().onAuthStateChanged((user) => {
    if (user) {
      return user;
    }
    return context.redirect('/signin');
  });
};
test.vue
<template>
  <div>
    ログイン必須ページ
  </div>
</template>

<script>
export default {
  middleware: 'authenticated',
};
</script>

await 使って色々試してもダメ。。
onAuthStateChangedには効かないっぽい?

単純にPromiseで囲んだらうまく動いた

middleware/authenticated.js
const checkSigninStatus = (context) => {
  return new Promise((resolve) => {
    context.app.$firebase.auth().onAuthStateChanged((user) => {
      if (user) {
        return resolve(user);
      }
      return context.redirect('/signin');
    });
  });
};

export default (context) => {
  return checkSigninStatus(context);
};

よかったよかった。

6
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
8