비전공자 개발일기

Firebase Auth(Email / Password Login) 본문

SWIFT

Firebase Auth(Email / Password Login)

HiroDaegu 2023. 1. 15. 21:15
728x90
SMALL

// AppDelegate

import UIKit
import Firebase

@main
class AppDelegate: UIResponder, UIApplicationDelegate {



    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        FirebaseApp.configure()
        return true
    }

    // MARK: UISceneSession Lifecycle

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }


}
// 계정등록, 로그인 ViewController
...

import FirebaseAuth

...

// 계정 등록 버튼 클릭 관련 함수에 아래 코드 사용

FirebaseAuth.Auth.auth().createUser(withEmail: email, password: password, completion: { authResult, error in
    guard let result = authResult, error == nil else {
        print("Error creating user")
        return
    }
    let user = result.user
    print("Created User: \(user)")
 })

// 로그인 버튼 클릭 관련 함수에서 아래 코드 사용

FirebaseAuth.Auth.auth().signIn(withEmail: email, password: password, completion: { authResult, error in
    guard let result = authResult, error == nil else {
        print("Failed to log in user with email: \(email)")
        return
    }
    let user = result.user
    print("Logged in User: \(user)")
})
728x90
LIST

'SWIFT' 카테고리의 다른 글

Memo App made by SwiftUI  (0) 2023.01.17
Log Out  (0) 2023.01.16
Choose a picture in iPhone's photo library(UIImagePickerController)  (0) 2023.01.14
MVC Pattern  (0) 2023.01.13
UIView Border by storyboard  (0) 2023.01.10