Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ViewController.swift
- // student-showcase
- //
- // Created by Chris Augg on 1/18/16.
- // Copyright © 2016 Auggie Doggie iOSware. All rights reserved.
- //
- import UIKit
- import Alamofire
- import Firebase
- import FBSDKCoreKit
- import FBSDKLoginKit
- class LoginVC: UIViewController {
- @IBOutlet weak var emailField: UITextField!
- @IBOutlet weak var passwordField: UITextField!
- var userHandle: UInt!
- var currentUser: User!
- override func viewDidLoad() {
- super.viewDidLoad()
- }
- override func viewDidAppear(animated: Bool) {
- super.viewDidAppear(animated)
- if let key = NSUserDefaults.standardUserDefaults().valueForKey(KEY_UID) {
- self.createCurrentUser(key as! String)
- }
- }
- override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
- if segue.identifier == "loggedIn" {
- let navController = segue.destinationViewController as! UINavigationController
- let vc = navController.viewControllers.first as! FeedVC
- vc.user = self.currentUser
- } else if segue.identifier == "profile" {
- let pvc = segue.destinationViewController as! ProfileVC
- pvc.currentUser = self.currentUser
- }
- }
- @IBAction func fbBtnPressed(sender: UIButton!) {
- let facebookLogin = FBSDKLoginManager()
- facebookLogin.logInWithReadPermissions(["email"], fromViewController: self, handler: { (facebookResult:FBSDKLoginManagerLoginResult!, facebookError: NSError!) -> Void in
- if facebookError != nil {
- print("Facebook login failed. Error \(facebookError)")
- } else {
- let accessToken = FBSDKAccessToken.currentAccessToken().tokenString
- print("Successfully logged in with facebook. \(accessToken)")
- DataService.ds.REF_BASE.authWithOAuthProvider("facebook", token: accessToken, withCompletionBlock: { error, authData in
- if error != nil {
- print("Login Failed. \(error)")
- } else {
- print("Logged In!\(authData)")
- let user = ["provider": authData.provider!]
- DataService.ds.createFirebaseUser(authData.uid, user: user)
- NSUserDefaults.standardUserDefaults().setValue(authData.uid, forKey: KEY_UID)
- self.currentUser = User(userKey: authData.uid)
- self.performSegueWithIdentifier(SEGUE_PROFILE, sender: self.currentUser)
- }
- })
- }
- })
- }
- @IBAction func attemptLogin(sender: UIButton!) {
- if let email = emailField.text where email != "", let pwd = passwordField.text where pwd != "" {
- if ValidationService.vs.isEmailValid(email) && ValidationService.vs.isPasswordValid(pwd) {
- DataService.ds.REF_BASE.authUser(email, password: pwd, withCompletionBlock: {error, authData in
- if error != nil {
- if error.code == STATUS_ACCOUNT_NONEXIST {
- DataService.ds.REF_BASE.createUser(email, password: pwd, withValueCompletionBlock: { error, result in
- if error != nil {
- self.showErrorAlert("Could not create account", msg: "Problem creating account. Try something else")
- } else {
- NSUserDefaults.standardUserDefaults().setValue(result[KEY_UID], forKey: KEY_UID)
- DataService.ds.REF_BASE.authUser(email, password: pwd, withCompletionBlock: { error, authData in
- let aUser = ["provider": authData.provider!, "email": email]
- DataService.ds.createFirebaseUser(authData.uid, user: aUser)
- self.currentUser = User(userKey: authData.uid)
- self.performSegueWithIdentifier(SEGUE_PROFILE, sender: self.currentUser)
- })
- }
- })
- } else {
- self.showErrorAlert("Could not log in", msg: "Please check your user name or password")
- }
- } else {
- self.createCurrentUser(authData.uid)
- }
- })
- } else {
- showErrorAlert("Valid Email and Password Required", msg: INVALID_EMAIL_OR_PASSWORD())
- }
- } else {
- showErrorAlert("Email and Password Required", msg: "You must enter an email and a password")
- }
- }
- func createCurrentUser(uid: String) {
- let userRef = DataService.ds.REF_USERS
- userHandle = userRef.observeEventType(.Value, withBlock: { snapshot in
- if let snapshots = snapshot.children.allObjects as? [FDataSnapshot] {
- for snap in snapshots {
- let userDict = snap.value as! Dictionary<String, AnyObject>
- let key = snap.key
- if uid == key {
- if NSUserDefaults.standardUserDefaults().valueForKey(KEY_UID) != nil {
- self.currentUser = User(userKey: key, userHandle: self.userHandle, userDict: userDict)
- } else {
- NSUserDefaults.standardUserDefaults().setValue(key, forKey: KEY_UID)
- self.currentUser = User(userKey: key, userHandle: self.userHandle, userDict: userDict)
- }
- break
- }
- }
- }
- self.performSegueWithIdentifier(SEGUE_LOGGED_IN, sender: self.currentUser)
- })
- }
- func showErrorAlert(title: String, msg: String) {
- let alert = UIAlertController(title: title, message: msg, preferredStyle: .Alert)
- let action = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)
- alert.addAction(action)
- presentViewController(alert, animated: true, completion: nil)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement