Day#15 Firebase Auth

前言

好的,那就来看看firebase到底是什麽吧。

Firebase

我们这次会用到firebase auth的注册、登入功能;firebase real time database的资料库服务;以及firebase storage的云端硬碟。

Cocoapods

那麽我们先来新增引用的函式库。

在Podfile新增以下的指令,然後下载。

pod 'Firebase/Core'
pod 'Firebase/Auth'
pod 'Firebase/Database'
pod 'Firebase/Analytics'

LoginViewController

首先将auth函式库引进来

import FirebaseAuth

然後将昨天留下登入的部分补上逻辑

FirebaseAuth.Auth.auth().signIn(withEmail: email, password: password, completion: { authResult, error in
    guard let result = authResult, error == nil else {
        print("failed to login with email: \(email)")
        return
    }

    let user = result.user
    print("logged in user: \(user)")
})

firebase提供的登入服务非常方便,可以直接呼叫signIn方法。
其中我们选用提供withEmail参数的方法。

使用guard确保登入成功,在此我们先印出成功登入的讯息,先不处理跳转等的东西。

RegisterViewController

同样的,我们也在注册这边加上firebase auth的服务逻辑。

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("Create user: \(user)")
})

AppDelegate

别忘了在appDelegate加上firebase auth的configuration。
如下第9行,如此一来,就大功告成了!

appDelegate.swift

import UIKit
import Firebase

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        
        FirebaseApp.configure()
        return true
    }
}

结语

今天写的比较简短,且都是着墨在程序上,解释得较少(其实就是因为存稿用完了,开始每天赶稿的日子、呜呜呜)
不过写的短好过缺少文章麻~未来还是会继续努力产出的~

参考资料

若上述内容有误或可以改进的部分,欢迎留言以及提出任何指教~
谢谢 (´・∀・`)


<<:  html 下拉式选单内的群组

>>:  Day14【Web】网路攻击:域名劫持

用爬虫计算自己 IT 邦帮忙所有文章的浏览/Like/留言总数

我想应该有许多 IT 邦帮忙的作者都很关心自己文章的浏览数,像笔者就是一个喜欢三不五时去看看自己文章...

Vaadin Pro Components 图表初探 - day26

目标 使用 Pro Components 制作图表 萤幕截图 汇出png图 Vaadin Chart...

iOS APP 开发 OC 第十四天,打包签名,你真的懂吗? 阅读笔记

iOS APP 开发 OC 第十四天,签证 tags: OC 30 day 资料来源:iOS 打包签...

Golang 学习笔记-- 快速上手/重点整理 - 3 - Array, Slice

宣告方式 var array_name = [length]datatype{values} var...

漏洞管理(Vulnerability Management)

由於管理是实现一个或多个目标的系统方法,我根据维基百科和NIST CSRC 术语表中的定义定义漏洞...