Skip to content

Instantly share code, notes, and snippets.

import UIKit
import Firebase
import FBSDKCoreKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
func googleSignInButtonTapped(with sender: LoginView) {
GIDSignIn.sharedInstance().signIn()
}
extension LoginViewController: GIDSignInUIDelegate{
func sign(_ signIn: GIDSignIn!, dismiss viewController: UIViewController!) {
viewController.dismiss(animated: true, completion: nil)
NotificationCenter.default.post(name: .closeLoginVC, object: nil)
}
func sign(_ signIn: GIDSignIn!, present viewController: UIViewController!) {
extension FirebaseManager: GIDSignInDelegate {
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
if let error = error {
print(error.localizedDescription)
return
}
if let user = user {
let idToken = user.authentication.idToken ?? ""
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FIRApp.configure()
GIDSignIn.sharedInstance().clientID = FIRApp.defaultApp()?.options.clientID
GIDSignIn.sharedInstance().delegate = firebaseManager
return true
}
func logout() {
do{
try FIRAuth.auth()?.signOut()
} catch let signOutError as NSError {
print ("Error signing out: %@", signOutError)
}
NotificationCenter.default.post(name: .closeMainVC, object: nil)
func forgotPasswordTapped(with sender: LoginView) {
let alertController = UIAlertController(title: "Forgot My Password", message: "To reset your password, please enter your email address.", preferredStyle: .alert) let sendAction = UIAlertAction(title: "Send", style: .default) { (action) in
let emailField = alertController.textFields![0]
if let email = emailField.text {
FIRAuth.auth()?.sendPasswordReset(withEmail: email, completion: { (error) in
if let error = error {
let alertController = UIAlertController(title: "Error", message: "\(error.localizedDescription)", preferredStyle: .alert)
let okAction = UIAlertAction(title: "Ok", style: .default, handler: nil)
alertController.addAction(okAction)
func signUpTapped(with sender: CreateAccountView) {
if checkIfAllFieldsValid() {
if let email = self.createView.emailTextField.text, let password = self.createView.passwordTextField.text {
FirebaseManager.createAccount(with: email, and: password, completion: { (success) in
if !success {
let alertController = UIAlertController(title: "Error", message: "Creating an account was failed. Please Try Again", preferredStyle: .alert)
let okAction = UIAlertAction(title: "Ok", style: .default, handler: nil)
alertController.addAction(okAction)
func signInButtonTapped(with sender: LoginView) {
if let email = self.loginView.emailTextField.text, let password = self.loginView.passwordTextField.text {
FirebaseManager.signIn(with: email, and: password, completion: { (success) in
if !success{
let alertController = UIAlertController(title: "Error", message: "Email or Password is incorrect", preferredStyle: .alert)
let action = UIAlertAction(title: "Ok", style: .default, handler: nil)
alertController.addAction(action)
import Foundation
import Firebase
import FirebaseAuth
import UIKit
class FirebaseManager {
class func createAccount(with email: String, and password: String, completion:@escaping (Bool)->Void) {
FIRAuth.auth()!.createUser(withEmail: email, password: password, completion: { (user, error) in