1
1

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 1 year has passed since last update.

FirebaseUI Authを使って最低限のログイン機能を実装する

Posted at

FirebaseUI Authを使って最低限のログイン機能を実装します。
今回はGoogleログインとAppleログインの2つを実装します。
Firebaseのプロジェクトは作成できている前提です。

1. XcodeにFirebase Authenticationを追加する

今回はcocoapodsでFirebaseを導入します。
podfileに以下を追加し、

  pod 'Firebase/Analytics'
  pod 'FirebaseUI'

pod installします。

2. Firebaseのプロジェクト上でAuthenationを有効にする

左側のタブからAuthenationを探し、始めるを押します
image.png
このような画面になるはずです
今回はGoogleとAppleの認証を行うので、
image.png

2-1. Googleを有効にする

Googleを押し、
image.png
有効にするのチェックをオンにします
公開名サポートメールを入力し、保存を押します
image.png

次に、GoogleSercvice-Info.plistを開き、REVERSED_CLIENT_IDの内容をコピーし、
20230122-093504.png
Info内のURL Types内のURL Schemesにペーストします
image.png

2-2. Appleを有効にする

新しいプロバイダを追加を押し、
image.png
Appleを選択します
image.png
有効にするのチェックをオンにし、保存を押します
image.png
Signing & Capabilities+ Capabilityを選択し、
image.png
Sign in with Appleをドラッグ&ドロップして追加します
image.png

(リリースするときに必要)
Appleの開発者サイトのCertificates, Identifiers & Profilesページを開き、アプリのSign In with Appleを有効にします。

3. Xcodeで設定する

AppDelegate.swift
import UIKit
+ import FirebaseCore
+ import FirebaseAuthUI

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
+         FirebaseApp.configure()
        return true
    }
ログイン実装する画面 (ViewController.swift)
import UIKit
+ import FirebaseAuthUI
+ import FirebaseOAuthUI
+ import FirebaseGoogleAuthUI

+ class ViewController: UIViewController, FUIAuthDelegate {
    
+     let authUI = FUIAuth.defaultAuthUI()!

    override func viewDidLoad() {
        super.viewDidLoad()
        
+         let providers: [FUIAuthProvider] = [
+             FUIGoogleAuth(authUI: authUI),
+             FUIOAuth.appleAuthProvider()
+         ]
+         self.authUI.delegate = self
+         self.authUI.providers = providers
    }

+     @IBAction func LoginButtonTapped(){
+         let authViewController = self.authUI.authViewController()
+         self.present(authViewController, animated: true, completion: nil)
+     }
    
+     public func authUI(_ authUI: FUIAuth, didSignInWith user: User?, error: Error?){
+         if error == nil {
+             print("成功")
+         } else {
+             print(error!)
+         }
+     }

}

これでできます!お疲れ!

参考

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?