티스토리 뷰

728x90
반응형

회원가입은 Auth의 createUser라는 함수만 불러주면 쉽게 할 수 있다 

 

FirebaseManager에 signup 메소드를 추가해준다 

extension FirebaseManager {

    class func signup(email: String, password: String, completion: @escaping (Result<AuthDataResult,Error>) -> Void) {
        Auth.auth().createUser(withEmail: email, password: password) { (result, error) in
            if let result = result {
                 completion(.success(result))
            } else if let error = error {
                completion(.failure(error))
            }
        }
    }

 

SignupViewModel에서 뷰컨으로부터 email과 password를 받아서 completion핸들러를 채워주고 signup메소드를 호출해준다 :-) 

struct SignupViewModel {
    struct State {
        
    }
    
    struct Action {
        let signup = PublishSubject<(String, String)>()
    }
    
    let state = State()
    let action = Action()
    private let bag = DisposeBag()
    
    init() {
        action.signup.subscribe(onNext: { email, password in
            FirebaseManager.signup(email: email, password: password, completion: { result in
                switch result {
                case .success:
                    Navigator.presentAlert(with: "회원가입 완료")
                case .failure(let error):
                    Navigator.presentAlert(with: error.localizedDescription)
                }
            })
        }).disposed(by: bag)
    }
}

 

SignUpViewController 도 이렇게 코딩해준다 

class SignUpViewController: UIViewController {

    @IBOutlet weak var emailTextField: UITextField!
    @IBOutlet weak var passwordTextField: UITextField!
    @IBOutlet weak var signupButton: UIButton!
    
    private var viewModel: SignupViewModel!
    private let bag = DisposeBag()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        viewModel = SignupViewModel()
        
        Observable.combineLatest(emailTextField.rx.text.orEmpty, passwordTextField.rx.text.orEmpty) { email, password -> Bool in
            return LoginManager.isValidEmail(email) && LoginManager.isValidPassword(password)
            }
            .subscribe(onNext: { [weak self] isValid in
                isValid ? (self?.signupButton.isEnabled = true) : (self?.signupButton.isEnabled = false)
            }).disposed(by: bag)
        
        
        signupButton.rx.tap.map { [weak self] _ in
            return (self?.emailTextField.text ?? "", self?.passwordTextField.text ?? "")
            }
            .bind(to: viewModel.action.signup)
            .disposed(by: bag)
    }
}

LoginManager도 매우 간단하게 일단 구현해준다 

 

class LoginManager {
    class func isValidEmail(_ email: String) -> Bool {
        return email.count >= 5
    }
    
    class func isValidPassword(_ password: String) -> Bool {
        return password.count >= 5
    }
}

 

 

반응형
댓글