포스트

Final (1)

이틀간의 회의, 대략적인 와이어프레임 구성이 끝났다.

이번에 우선적으로 내가 해야하는것은 Sign In, Sign Up, Sign out이 되겠다.

Firebase Auth를 사용할 계획.

Sign up 기능 구현

우선 디자인은 얼추 했고, 물론 view로 세부적으로 다시 따는건 주말에 하고 가입 기능 부터 먼저 하나 해보려 한다

이번엔 MVVM이므로 좀 더 생각을 해서 구현을 해야한다.

우선 SignUpVM, SignManager 두개를 만들어준다

SignManager를 통해 Firebase와 통신을 할 예정

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
31
class SignManager {
    
    func signUp(nickName: String, email: String, password: String, onError: @escaping((Error) -> Void)) {
        Auth.auth().createUser(withEmail: email, password: password) { authDataResult, error in
            
            // error를 escaping closure를 통해 전달.
            if error != nil {
                onError(error!)
                return
            }
            
            // error가 없을 경우
            if let authData = authDataResult {
                var dict: Dictionary<String, Any> = [
                    "uid": authData.user.uid,
                    "email": authData.user.email,
                    "username": nickName,
                    "profileImageUrl": ""
                ]
   
                Database.database().reference().child("users").child(authData.user.uid).updateChildValues(dict) { error, ref in
                    if error != nil {
                        onError(error!)
                    }
                }
            }
        }
        
    }
}

우선은 이렇게 구현

그리고 버튼에 대한 메서드는 다음과 같이 했다.

이렇게 하게되면 Auth 뿐만아니라 DB에도 유져에 대한 데이터가 저장이 된다.

1
2
3
4
5
6
7
8
9
10
11
@objc func signUpButtonDidTapped() {
        
        signUpVM.signUp(nickName: nicknameTextField.text!, email: emailTextField.text!, password: passwordTextField.text!) { [weak self] error in
            let alert = UIAlertController(title: "에러 발생", message: "\(error.localizedDescription)이 발생했습니다", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "확인", style: .default))
            self?.present(alert, animated: true)
        }
        let alert = UIAlertController(title: "가입 완료", message: "회원 가입 되었습니다.\n환영합니다.", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "확인", style: .default))
        self.present(alert, animated: true)
    }

CleanShot 2024-05-31 at 05 30 28@2x

완료.

Sign in 구현

1
2
3
4
5
6
7
8
9
10
func signIn(email: String, password: String, onError: @escaping((Error) -> Void)) {
        Auth.auth().signIn(withEmail: email, password: password) { authDataResult, error in
            // error를 escaping closure를 통해 전달.
            if error != nil {
                onError(error!)
                return
            }
             print(authDataResult?.user.email) // check
        }
    }

로그인은 더 간단하다

로그인 확인을 위해 print로 확인을 해보려고 적는다.

1
2
3
4
5
6
7
@objc func signInButtonDidTapped() {
        signVM.signIn(email: emailTextField.text!, password: passwordTextField.text!) { [weak self] error in
            let alert = UIAlertController(title: "에러 발생", message: "\(error.localizedDescription)이 발생했습니다", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "확인", style: .default))
            self?.present(alert, animated: true)
        }
    }
1
Optional("test1@gmail.com")

출력 확인 완료.

즉 로그인이 된다는것.

오늘은 심플하게 이걸로 끝

이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.