23
16

More than 5 years have passed since last update.

Firebase Authによるパスワードリセットとメルアド変更

Posted at

前回の記事の続きです。
https://qiita.com/miutex/items/f64dbcd2694cbc2daaf7

趣旨

  • Firebase Authを使って前回はメールアドレスとパスワードによる認証を行いました。
  • 今回はそれに続いて、パスワードリセット(パスワードリマインダ)とメールアドレス変更を行います。
  • 事前準備が必要です。その辺りは前回の記事に書いてあります。既にやってあれば今回は楽ちん。

パスワードリセット

パスワードを忘れた際に、メールアドレスを入力して送信するとリセット用のメールが来るあれです。

hoge.m
- (void)resetPasswordWithEmail:(void (^)(NSError *error))block
{
    [FIRAuth auth].languageCode = @"ja";
    FIRActionCodeSettings *actionCodeSettings = [[FIRActionCodeSettings alloc] init];
    actionCodeSettings.handleCodeInApp = YES;
    actionCodeSettings.URL = [NSURL URLWithString:@"hogefugapiyo://www.example.com/aaaaa/ccc.html"];
    [actionCodeSettings setIOSBundleID:@"com.fuga.piyo"];

    [[FIRAuth auth] sendPasswordResetWithEmail:_tf_email.text actionCodeSettings:actionCodeSettings completion:^(NSError * _Nullable error) {
        if(block) block(error);
    }];
}

で、これを実行すると、

IMG_0110.PNG

メールが来ました。
メールが英語のままなのは、Firebase Consoleの方で日本語のところにも英文が入っていたためと後で判明。
パスワードリセットに限り、メールの本文もカスタマイズできるようです。

IMG_0111.PNG

パスワードリセット画面。Firebase側で用意してくれています。
SAVEを押すと設定したURLスキームでアプリに戻ります。

メールアドレス変更

こちらはログインしているユーザーに対し、メールアドレスの変更を行いますので
事前にログインしておく必要があります。

hoge.m
- (void)changeEmail:(void (^)(NSError *error))block
{
    [[FIRAuth auth].currentUser updateEmail:_tf_email.text completion:^(NSError * _Nullable error) {
        if (error) {
            if(block) block(error);
        } else {
            [FIRAuth auth].languageCode = @"ja";
            FIRActionCodeSettings *actionCodeSettings = [[FIRActionCodeSettings alloc] init];
            actionCodeSettings.handleCodeInApp = YES;
            actionCodeSettings.URL = [NSURL URLWithString:@"hogefugapiyo://www.example.com/aaaaa/ccc.html"];
            [actionCodeSettings setIOSBundleID:@"com.fuga.piyo"];
            [[FIRAuth auth].currentUser sendEmailVerificationWithActionCodeSettings:actionCodeSettings completion:^(NSError *_Nullable error) {
                if(block) block(error);
            }];
        }
    }];
}

これを実行すると認証時と同様の確認メールが届きます。
違いは、変更完了時に変更前のメールアドレスに変更が完了した旨のメールが届くところですね。

IMG_0112.PNG

以上です

23
16
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
23
16