Let's use Swift Firebase Firebase Auth

Firebase Auth introduction

cd /Users/[username]/Desktop/[Project name]/

If you are worried that you can move it, execute the pwd command. After moving, create a pod file.

pod init

When you run the command, a podfile is created. To check, execute the ls command. If it was created properly

open podfile

Open the file with. When you open it, edit it like this.

# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

target 'FirebaseTest' do
  # Comment the next line if you don't want to use dynamic frameworks

  # Pods for FirebaseTest
  //add to
  pod 'Firebase/Auth'

Return to the terminal again. When you get back, type the following command.

pod intall

Now you can use FirebaseAuth.

Let's use new user registration

At the terminal

open [Project name].xcworkspace

Please hit. You can open it from the Finder. The extension is .xcworkspace instead of .xcodeproj, so be careful not to make a mistake. Once opened, let's edit the ViewController.


class ViewController: UIViewController {

    private var emailFiled = UITextField()
    private var passwordField = UITextField()
    private let authButton: UIButton = {
        let button = UIButton()
        button.setTitle("Registration", for: .normal)
        return button
    override func viewDidLoad() {
        emailFiled = initField(placeholder: "email")
        passwordField = initField(placeholder: "password")
        authButton.addTarget(self, action: #selector(didTapAuthButton), for: .touchUpInside)
        emailFiled.frame = .init(x: 50, y: 100, width: view.frame.size.width-100, height: 40)
        passwordField.frame = .init(x: 50, y: 160, width: view.frame.size.width-100, height: 40)
        authButton.frame = .init(x: 50, y: 220, width: view.frame.size.width-100, height: 40)
    @objc func didTapAuthButton() {
    private func initField(placeholder: String) -> UITextField {
        let field = UITextField()
        field.placeholder = placeholder
        field.autocorrectionType = .no
        field.autocapitalizationType = .none
        field.layer.borderWidth = 1
        field.layer.borderColor = UIColor.black.cgColor
        if placeholder == "password" {
            //Hide the characters you type
            field.isSecureTextEntry = true
        return field

Let's set the View like this. I'm sorry to have kept you waiting. We will use FirebaseAuth. Write the process in didTapAuthButton. Write only the changes.

import FirebaseAuth

class ViewController: UIViewController {
    @objc func didTapAuthButton() {
        guard let email = emailFiled.text, let password = passwordField.text else {return}
        Auth.auth().createUser(withEmail: email, password: password) { (result, err) in
            guard let user = result?.user, err == nil else {
                print("error: ", err!)
            let userEmail = user.email
            print("email: ", userEmail ?? "")

This is the only way to create a new user If there is nothing you want to do after createUser, you only needAuth.auth (). createUser (with Email: email, password: password). Well, I don't think it's possible to use the closure with the contents empty.

Let's use Login / Logout

Let's log in and see the code immediately.

@objc func didTapAuthButton() {
        guard let email = emailFiled.text, let password = passwordField.text else {return}
        Auth.auth().signIn(withEmail: email, password: password)

This is all you need to do the login function. It's really easy.

You can also log out like this.

@objc func didTapAuthButton() {
        do {
            try Auth.auth().signOut()
        } catch(let err) {

Exception handling occurs when logging out, so what is exception handling? If you think that, please refer to here. Introduction to Swift 4.0 Error Handling

You can easily implement other functions such as logging in with a phone number, so please check it out. Up to here for this time.

Recommended Posts

Let's use Swift Firebase Firebase Auth
[Swift] Let's use extension
[Swift] Let's use Segmented Control
Let's use jcmd
[Swift] First Firebase
Let's use Swift Package Manager (SwiftPM) ~ Introduction ~
[RSpec] Let's use FactoryBot [Rails]
[spring] Let's use Spring Data JPA
Use Swift Package on Playground
How to use Swift UIScrollView
Use swift Filter and Map
[Swift] How to install Firebase ~ How to use Realtime Database & Cloud Firestore
[Swift UI] Use Map view
Let's use Set more [Collections Framework]
[Swift] Another way to use Result?
Let's use Twilio in Java! (Introduction)
Let's use Amazon Textract with Ruby
Let's use it after understanding sudo!
[Swift] How to use SwiftLint (cocoapods)
[Swift] How to use Unwind segue
Install Xcode and use Hello Swift!