Closed8

Next.js / iOS 16 で Firebase Auth の signInWithRedirect() がうまく動作しない問題

ピン留めされたアイテム
椎名椎名

prodで問題なく動作して解決したので他の人向けメモ:

  1. next.config.js にrewritesの設定を追記
/** @type {import('next').NextConfig} */
module.exports = {
  async rewrites() {
    return [
      {
        source: "/__/auth/:path*",
        destination: `https://${process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID}.firebaseapp.com/__/auth/:path*`,
      },
    ];
  },
};
  1. Firebase Configのauth domainをサービスのドメインに変更
    (環境変数のNEXT_PUBLIC_FIREBASE_AUTH_DOMAIN など)

auth domainに localhost:3000 を設定するとうまく動かず、stagingにデプロイしないと試せなかったのだけ気になる、なにか方法があるんだろうか

椎名椎名

Hosting the helper code has the following steps:

Download the files to host from the <project>.firebaseapp.com location by
executing the following commands:

mkdir signin_helpers/ && cd signin_helpers
wget https://<project>.firebaseapp.com/__/auth/handler
wget https://<project>.firebaseapp.com/__/auth/handler.js
wget https://<project>.firebaseapp.com/__/auth/experiments.js
wget https://<project>.firebaseapp.com/__/auth/iframe
wget https://<project>.firebaseapp.com/__/auth/iframe.js

Host the above files under your app domain. Ensure that your web server can respond to https://<app domain>/__/auth/<filename>.

https://github.com/firebase/firebase-js-sdk/issues/6716#issuecomment-1302667920

椎名椎名

Next.jsのRewritesで動かせるかも?
https://nextjs.org/docs/api-reference/next.config.js/rewrites

椎名椎名
module.exports = {
  async rewrites() {
    return [
      {
        source: "/__/auth/:path*",
        destination: `https://${process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID}.firebaseapp.com/__/auth/:path*`,
      },
    ];
  },
};
nabettunabettu

検証&まとめありがとうございます!

https://firebase.google.com/docs/auth/web/redirect-best-practices

軽減策 #1の2にもあるんですが、"OAuth プロバイダーの承認済みリダイレクト URI"へ自分のアプリのドメインを追加する必要がありますので、コメントしておきます。

Googleログインの場合にはGoogleCloudコンソールの、API→認証情報→OAuth 2.0 クライアント IDのクライアント詳細設定で、Firebaseが自動で生成しているOAuthクライアントで以下のような設定が必要なようです。

椎名椎名

補足ありがとうございます~!
たしかに各プロバイダーでリダイレクトの設定変えなきゃですね

このスクラップは2022/12/06にクローズされました