Swift 4 ile Firebase Authentication İşlemleri

Firebase Kütüphanesinin sunmuş olduğu bir çok özellikten biri olan Firebase Authentication(Kimlik Doğrulama) uygulamamıza bir çok şekilde(Facebook,Eposta,Twitter) kimlik doğrulama olanağı sağlar.Bu makalemizde de Eposta ile kimlik doğrulama işlemini anlatmaya çalıştım. Öncelikle projemize Firebase Kütüphanesini tanıtmamız gerekiyor. Buraya tıklayarak daha önce anlattığım makaleye göre ayarlamalarımızı yapalım. Son olarak bu siteye giderek aşağıdaki gibi Authentication işlemini aktif hale getirelim.
Ayarlamalarımızı yaptığımıza göre proje dosyamıza gelelim ve dosya_adı.xcworkspace dosyamızı açalım.
Aşağıdaki resime göre Main.storyboard klasörümüzü ayarlıyoruz.
signInVC.swift
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
// // signUpIn.swift // FirebaseAuthentication // // Created by Esat Gözcü on 23.01.2018. // Copyright © 2018 Esat Gözcü. All rights reserved. // import UIKit import Firebase import FirebaseAuth class signUpIn: UIViewController { @IBOutlet weak var passwordLabel: UITextField! @IBOutlet weak var emailLabel: UITextField! override func viewDidLoad() { super.viewDidLoad() } @IBAction func signInButton(_ sender: Any) { if emailLabel.text != "" && passwordLabel.text != ""{ //Kullanıcı ve şifre boş değil ise.. //signIn() ile kullanıcıyı kayıt ediyoruz Auth.auth().signIn(withEmail: emailLabel.text!, password: passwordLabel.text!, completion: { (user, error) in if error != nil { let alert = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: UIAlertControllerStyle.alert) let okButton = UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: nil) alert.addAction(okButton) self.present(alert, animated: true, completion: nil) } else{ //Herhangi bir hata olmazsa kayıt yapıldı demektir //Segue çalışacak ve diğer sayfaya geçiş yapılacak self.performSegue(withIdentifier: "gecisSegue", sender: nil) } }) } else{ //Kullanıcı ve Şifre Boş Bırakılırsa //AlertDialog oluşturuyoruz let alert = UIAlertController(title: "Dikkat!", message: "Kullanıcı Adı ve Şifre Gerekli", preferredStyle: UIAlertControllerStyle.alert) let okButton = UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: nil) alert.addAction(okButton) self.present(alert, animated: true, completion: nil) } } @IBAction func signUpButton(_ sender: Any) { if emailLabel.text != "" && passwordLabel.text != ""{ //Kullanıcı ve şifre boş değil ise.. //createUser() ile kullanıcıyı kayıt ediyoruz Auth.auth().createUser(withEmail: emailLabel.text!, password: passwordLabel.text!, completion: { (user, error) in if error != nil{ //Sistemsel bir hata olursa AlertDialog oluşturuyoruz //Örneğin şifre 6 haneden azsa Firebase otomatik hatayı bastıracaktır let alert = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: UIAlertControllerStyle.alert) let okButton = UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: nil) alert.addAction(okButton) self.present(alert, animated: true, completion: nil) } else{ //Eğer kayıt başarılı olursa.. let alert = UIAlertController(title: "Success", message: "", preferredStyle: UIAlertControllerStyle.alert) let okButton = UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: nil) alert.addAction(okButton) self.present(alert, animated: true, completion: nil) } self.emailLabel.text="" self.passwordLabel.text="" }) } else{ //Kullanıcı ve Şifre Boş Bırakılırsa //AlertDialog oluşturuyoruz let alert = UIAlertController(title: "Dikkat!!", message: "Kullanıcı Adı ve Şifre Gerekli", preferredStyle: UIAlertControllerStyle.alert) let okButton = UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: nil) alert.addAction(okButton) self.present(alert, animated: true, completion: nil) } } } |
ViewController.swift
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// ViewController.swift // FirebaseKurulum // // Created by Esat Gözcü on 23.01.2018. // Copyright © 2017 Esat Gözcü. All rights reserved. // import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() } } |
Projenin kodlarını buraya tıklayarak indirebilirsiniz.