4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

auth0でGoogleログインした場合、リロードでログイン状態が消える問題

Last updated at Posted at 2020-04-22

nextjs + auth0 + google (reload でもログインキープ)サンプル

repo
https://github.com/github0013/nextjs-auth0-google

auth0 はこのパッケージ( https://github.com/Swizec/useAuth )でだいぶ楽だが、google などの ID プロバイダーなどでのログインとなるとやっかいで、ログイン後にリロードするとログアウト状態になる。
これはセキュリティの観点上、重要な情報はメモリ内にあるだけなのでリロードすると消えてしまう。
このパッケージにもあるように、Google でリロード後もログイン状態を保つには以下の通りにする。

Set Up Social Connections
https://auth0.com/docs/dashboard/guides/connections/set-up-connections-social

参考:
React Tutorial: Building and Securing Your First App
(Keeping Users Signed In after a Refresh から読む)
https://auth0.com/blog/react-tutorial-building-and-securing-your-first-app/

これを読んでいってもちょっと分かりにくかったので、このサンプル環境と手順で簡単に試せます。

事前準備

  1. gmail のアカウントがある(あとで google API console にログインする必要がある)
  2. ngrok にアカウントがある(すぐ作れる - 外から繋がる https が必要)
  3. ngrok の authtoken がある(ログイン後、通常表示される)
  4. auth0 にアカウントがある
  5. auth0 に app がある
  6. auth0 に Google の connection がある
  7. docker-compose.yml のYOUR_NGROK_AUTHに ngrok の authtoken を入れる
  8. ./frontend に .env を作る(.env.EXAMPLE 参考)
  9. docker-compose up (down すると ngrok のアドレスが変わり、auth0 の設定をやり直さないといけないので注意)
  10. http://YOUR_LOCAL_IP:4040/ を開いて ngrok の https アドレスをクリック表示
  11. frontend が表示される
  12. url をコピー
  13. auth0 を開いて app のAllowed Callback URLsAllowed Logout URLsAllowed Web Originsを設定

.env の設定

AUTH0_DOMAIN AUTH0_CLIENT_ID
auth0 の app の Domain auth0 の app の Client ID

auth0 の設定

Allowed Callback URLs Allowed Logout URLs Allowed Web Origins
https://...ngrok.io/auth0/callback https://...ngrok.io https://...ngrok.io

メールアドレス登録でのログイン

  1. frontend の login ボタンで auth0 のダイアログが表示されれば一旦は OK
  2. sign up でとりあえずメールアドレス登録でログインしてみる
  3. /users が開けば OK
  4. リロードしてみる
  5. 引き続きログインが出来ていれば OK

Google の設定

ここがややこしい。Google でガチャガチャ設定して Google のClient IDClient Secretを作り、Admin SDKを有効にしておいて、auth0 の Google connection に Google のClient IDClient Secretを設定する流れ。

  1. auth0 へログインする
  2. https://auth0.com/docs/connections/social/google を開く
  3. 手順が書いてあるが、一応説明
  4. https://console.developers.google.com にログインする
  5. プロジェクトがないならまず作る
  6. OAuth 同意画面へ行く
  7. 承認済みドメインへauth0.comを入れて保存
  8. 認証情報で OAuth クライアント ID 新規作成
  9. ウェブ アプリケーションを選択
  10. 承認済みの JavaScript 生成元、承認済みのリダイレクト URI を https://auth0.com/docs/connections/social/google に表示されているとおりに入力
  11. ID とシークレットが表示されるのでコピー
  12. !!!念のために作成されたクライアント ID をクリックして内容確認(意外と承認済みの JavaScript 生成元 | 承認済みのリダイレクト URI どっちかが無かったりする
  13. ライブラリに行って、adminと検索してAdmin SDKを有効にする
  14. auth0 に行く
  15. connections で google をクリックして表示
  16. Client IDClient Secretが入力出来るので OAuth クライアント ID 作成時に出来たものを入れる
  17. TRY ボタンで試してみて、google のログインが出たら OK
  18. frontend に行って、google アカウント(誰のでも)でログインしてみる
  19. ログイン後、リロードしてもログイン状態なら成功

https://auth0.com/docs/connections/social/google

承認済みの JavaScript 生成元 承認済みのリダイレクト URI
Authorized JavaScript origins Authorized redirect URIs

OAuth 同意画面(autho0.com 追加)

ウェブアプリケーションで ID の作成

出来たクライアント ID を開いた所

Admin SDK の検索

4
6
1

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
4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?