Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- class struct
- ReferceCycle
- @escaping
- 테이블뷰 나누기
- Reference Cycle
- 면접을 위한 CS전공 지식 노트
- TableView
- tableview section별 다른 cell적용
- Array vs Linked List
- UserDefaults
- 면접을 위한 CS 전공 지식 노트 Tree
- TableView Section
- til
- SWIFT
- coremotion
- Value Type Reference Type
- retain cycle
- wil
- CoreData
- 강한 참조 순환
- Carousel CollectionView
- 양궁대회
- 자료구조
- UIKit
- Input Output
- 롤케이크 자르기
- firebase
- 프로그래머스
- CarouselCollectionview
- NavigationSearchBar
Archives
- Today
- Total
개발하는 동글 :]
[TIL],[UIKit],[Firebase 로그인 기능 다양하게 구현하기!] 본문
1. 함수명으로 정확히 기능을 구별하여 구현하기
먼저 로그인 및 회원가입에 대한 정보를 전달해 주는 LoginResult 구조체를 만들어 주자.
struct LoginResult {
var isSuccess: Bool
let email: String
var errorMessage: String?
}
그 후 Firebase에서 제공하는 메서드를 통해 각각 로그인과 회원가입에 대한 메서드를 만들어 준다.
import Foundation
import FirebaseCore
import FirebaseAuth
struct LoginManager {
func trySignUp(email:String, password: String, completion: @escaping (LoginResult) -> Void) {
Auth.auth().createUser(withEmail: email, password: password) { authResult, error in
var result = LoginResult(isSuccess: true, email: email)
if error == nil {
completion(result)
} else {
result.isSuccess = false
result.errorMessage = String(describing: error)
completion(result)
}
}
}
func trySignIn(email:String, password: String, completion: @escaping (LoginResult) -> Void) {
Auth.auth().signIn(withEmail: email, password: password) {authResult, error in
var result = LoginResult(isSuccess: true, email: email)
if error == nil {
completion(result)
} else {
result.isSuccess = false
result.errorMessage = String(describing: error)
completion(result)
}
}
}
}
2. 한 함수로 타입을 지정하여 기능을 구별하여 구현하기
struct LoginManager {
enum SignType {
case signUp
case signIn
}
func trySign(type: SignType, email: String, password: String, completion: @escaping (LoginResult) -> Void) {
switch type {
case .signIn:
Auth.auth().signIn(withEmail: email, password: password) { authResult, error in
checkError(email: email, authResult: authResult, error: error, completion: completion)
}
case .signUp:
Auth.auth().createUser(withEmail: email, password: password) { authResult, error in
checkError(email: email, authResult: authResult, error: error, completion: completion)
}
}
}
func checkError(email: String, authResult: AuthDataResult?, error: Error?, completion: @escaping (LoginResult) -> Void) {
var result = LoginResult(isSuccess: true, email: email)
if error == nil {
completion(result)
} else {
result.isSuccess = false
result.errorMessage = String(describing: error)
completion(result)
}
}
}
이번에는 trySign이라는 메서드로 묶어 타입을 받아 분기처리를 하고 error를 체크하는 부분을 메서드로 분리하여 구현해 보았다.
3. 두 코드를 비교하여 어떤 코드를 사용할지 정하기
각각의 코드의 장단점?을 조금은 생각해 보았다. 먼저 각각의 기능을 분리하여 만든 메서드는 일단 사용하는 측면에서 더 편리한 것 같다. 메서드의 이름만으로 그 기능을 명확히 알 수 있고, 추후 코드가 조금씩 달라지게 될 경우 각각의 수정이 편리할 것 같다. 단점을 생각해 보면 반복되는 코드를 줄이지 못한다는 느낌을 받았다. 두번째로 타입을 받아 처리하는 방식은 코드를 재사용하여 메서드를 만들 때에는 좀 더 쉽게 만들 수 있었다. 하지만 단점으로는 메서드의 이름만으로는 기능을 명확히 알 수 없었고, 각각의 수정이 불편하다는 단점이 있는 것 같다. 그렇기에 1번 방식을 사용하여 Login기능을 구현할 것 같다. 추후 수정이 힘들다는 점과 명확한 이름을 가지는 것이 1번의 방법으로 구현하게 되는 이유가 될 것 같다.