brunch

Firebase, #iOS Signup

Email, Authentication , create user

by Tilltue

* 이글은 Swift 3 , Firebase 3.12.0, FirebaseAuth 3.1.0 를 기준으로 작성하였습니다.

스크린샷 2017-03-01 오후 5.24.06.png


1. Firebase 유저 생성

email 사용자 생성을위해 firebase console 에서 아래와 같이 이메일을 활성화 하자.

스크린샷 2017-03-01 오후 5.25.20.png



Create user api

open func createUser(withEmail email: String, password: String, completion: FirebaseAuth.FIRAuthResultCallback? = nil)


유저 생성 api 이며 이메일과 비밀번호가 필요하다.

- 비밀번호 규칙등은 클라이언트에서 처리한다.


FIRAuth.auth()?.createUser(withEmail: withEmail, password: password) { (user, error) in

if let error = error {

print(error.localizedDescription)

}else {

}

}


FIRAuth.auth()?.currentUser?.isEmailVerified

(bool)

현재 유저의 이메일 인증 정보는 위를 통해 확인 가능.



Verification email api

open func sendEmailVerification(completion: FirebaseAuth.FIRSendEmailVerificationCallback? = nil)


이메일 인증을 위해 인증 메일을 보내는 api 이다.


FIRAuth.auth()?.currentUser?.sendEmailVerification(completion: { (error) in

if let error = error {

print(error.localizedDescription)

}else {

}

})


SignIn api

open func signIn(withEmail email: String, password: String, completion: FirebaseAuth.FIRAuthResultCallback? = nil)


sign in api 이다.

인증 단계가 끝나지 않았다면 에러를 반환한다.


FIRAuth.auth()?.signIn(withEmail: withEmail, password: password, completion: { (user, error) in

if let error = error {

print(error.localizedDescription)

}else {

}

})



2.Firebase 유저 관리

연결된 유저들의 정보는 console 에서 확인할 수 있다.

스크린샷 2017-03-01 오후 5.42.12.png

user reload api

open func reload(completion: FirebaseAuth.FIRUserProfileChangeCallback? = nil)


유저가 삭제되었을때 등의 처리를 위해 현재 유저를 갱신하기 위한 api


FIRAuth.auth()?.currentUser?.reload(completion: { (error) in


})


user delete api

open func delete(completion: FirebaseAuth.FIRUserProfileChangeCallback? = nil)


유저 삭제 api


FIRAuth.auth()?.currentUser?.delete(completion: { (error) in

})


sign out api

open func signOut() throws


sign out api 이다.


do {

try FIRAuth.auth()?.signOut()

}catch let error as NSError {

}


user profile change api

- (FIRUserProfileChangeRequest *)profileChangeRequest;


유저 프로필 변경 api 이다.


displayname 을 변경하는 예제이다.


guard let user = FIRAuth.auth()?.currentUser else { return }

let changeRequest = FIRAuth.auth()?.currentUser?.profileChangeRequest()

changeRequest?.displayName = username

changeRequest?.commitChanges() { error in

if let error = error {

}else {

let ref = FIRDatabase.database().reference()

ref.child("users").child(user.uid).setValue(["username": username])

}

}



Sociel Login 인지 구분하는 방법

if let providerData = FIRAuth.auth()?.currentUser?.providerData {

for userInfo in providerData {

switch userInfo.providerID {

case "facebook.com":

print("user is signed in with facebook")

default:

print("user is signed in with \(userInfo.providerID)")

}

}

}


keyword
매거진의 이전글UICollectionView prefetch