Firebase Auth creating users - ios

I can't seem to get this to work. The database portion works and I'm getting user info as intended in the database, but it is not creating users in Firebase Auth. For the following code, it printed "can't register."
Can someone please tell me what I'm doing wrong?
import UIKit
import Firebase
import FirebaseAuth
class AddUserTableViewController: UITableViewController, UITextFieldDelegate {
#IBOutlet weak var firstNameTextField: UITextField!
#IBOutlet weak var emailTextField: UITextField!
#IBAction func saveUserButton(_ sender: Any) {
let ref: FIRDatabaseReference!
ref = FIRDatabase.database().reference()
FIRAuth.auth()?.createUser(withEmail: emailTextField.text!, password: "pass", completion: { (user, error) in
if error != nil {
print ("Can't Register")
}
else {
print ("I don't know what this means")
}
})
ref?.child("Users").childByAutoId().setValue(["First Name": self.firstNameTextField.text, "Email": self.emailTextField.text])
}

Just include Firebase, you don't need to include FirebaseAuth as well on each page.
Here's my working code for FireBase login, I did this from a Youtube tutorial a few weeks ago.
import UIKit
import Firebase
class LoginController: UIViewController {
#IBOutlet weak var menuButton:UIBarButtonItem!
#IBOutlet weak var signinSelector: UISegmentedControl!
#IBOutlet weak var signinLabel: UILabel!
#IBOutlet weak var emailTextField: UITextField!
#IBOutlet weak var passwordTextField: UITextField!
#IBOutlet weak var signinButton: UIButton!
var isSignIn:Bool = true
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func signinSelectorChanged(_ sender: UISegmentedControl) {
//Flip the boolean true to false
isSignIn = !isSignIn
//Check the boolean and set the buttons and labels
if isSignIn {
signinLabel.text = "Sign In"
signinButton.setTitle("Sign In", for: .normal)
}
else {
signinLabel.text = "Register"
signinButton.setTitle("Register", for: .normal)
}
}
#IBAction func signinButtonTapped(_ sender: UIButton) {
//Do some form validation on email and password
if let email = emailTextField.text, let pass = passwordTextField.text
{
//Check if it's signed or register
if isSignIn {
//Sign in the user with Firebase
Auth.auth().signIn(withEmail: email, password: pass, completion: { (user, error) in
//Check that user isn't nil
if let u = user {
//User is found, goto home screen
self.performSegue(withIdentifier: "goToHome", sender: self)
}
else{
//Error: Check error and show message
}
})
}
else {
//Register the user with Firebase
Auth.auth().createUser(withEmail: email, password: pass, completion: { (user, error) in
//Check that user isn't NIL
if let u = user {
//User is found, goto home screen
self.performSegue(withIdentifier: "goToHome", sender: self)
}
else {
//Check error and show message
}
})
}
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
//Dismiss the keyboard when the view is tapped on
emailTextField.resignFirstResponder()
passwordTextField.resignFirstResponder()
}
}

Related

Value of type 'AuthDataResult' has no member 'uid' problem

I am a newbie in Swift, learning from building a social media app following a tutorial of Kasey Schlaudt on youtube. When I write this line KeychainWrapper.standard.set((user?.uid)!, forKey: "KEY_UID") at minutes 36:11 if this video: https://youtu.be/gBB5tnAzjjo?t=2171 I get this error —> Value of type 'AuthDataResult' has no member 'uid'. Any suggestions on why this might be happening?
This is my code so far:
import UIKit
import Firebase
import SwiftKeychainWrapper
class ViewController: UIViewController {
#IBOutlet weak var UserImageView: UIButton!
#IBOutlet weak var usernameField: UITextField!
#IBOutlet weak var emailField: UITextField!
#IBOutlet weak var passwordField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func signInPress(_ sender: Any) {
if let email = emailField.text, let password = passwordField.text {
Auth.auth().createUser(withEmail: email, password: password) { (user, error) in
if error != nil {
//Create account
} else {
KeychainWrapper.standard.set((user?.uid)!, forKey: "KEY_UID")
}
}
}
}
}
Any help would be much appreciated !
The API documentation for createUser says that it provides an AuthDataResult object as the result. As you can see from link, it doesn't have a uid property. You will want to use its user property to get a User object that does have a uid.
Auth.auth().createUser(withEmail: email, password: password) { (result, error) in
if error != nil {
//Create account
} else {
KeychainWrapper.standard.set((result?.user.uid)!, forKey: "KEY_UID")
}
}

How to add a user with setValue from Firebase Reading and writing data in ios

I am a newbie in Swift and I am learning by building a social media application.
I am struck at trying to implement self.ref.child("users").child(user.uid).setValue(["username": username]) in my code (from https://firebase.google.com/docs/database/ios/read-and-write).
I have been following the instructions of Kasey Schlaudt and at this point of the tutorial https://youtu.be/GrRggN41VF0?t=619 he tried to add a user with setValue as shown in the Firebase documentation I have linked. The errors I get in the line self.ref.child("users").child(user.uid).setValue(["username": username]) are
Use of unresolved identifier 'user' and Use of unresolved identifier 'username'.
My code so far (with some little changes from the original code in the video in my signInPress function) is
import UIKit
import Firebase
import SwiftKeychainWrapper
class ViewController: UIViewController {
#IBOutlet weak var UserImageView: UIButton!
#IBOutlet weak var usernameField: UITextField!
#IBOutlet weak var emailField: UITextField!
#IBOutlet weak var passwordField: UITextField!
override func viewDidLoad()
{
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool)
{
if let _ : Bool = KeychainWrapper.standard.string(forKey: "uid") != nil
{
self.performSegue(withIdentifier: "toFeed", sender: nil)
}
}
func storeUserData(userID: String)
{
//---------------------------problematic line---------------------------
//from https://firebase.google.com/docs/database/ios/read-and-write
//from https://youtu.be/GrRggN41VF0?t=619
self.ref.child("users").child(user.uid).setValue(["username": username])
([
"username": usernameField.text
])
}
#IBAction func signInPress(_ sender: Any)
{
//this way you make sure there is a property inside emailField.text and you have a variable you can easily use
if let email = emailField.text, let password = passwordField.text
{
Auth.auth().signIn(withEmail: email, password: password)
{ (result, error) in
if error != nil && self.usernameField.text!.isEmpty && self.UserImageView.image != nil
{
Auth.auth().createUser(withEmail: email, password: password)
{ (result, error) in
self.storeUserData(userID: (result?.user.uid)!)
KeychainWrapper.standard.set((result?.user.uid)!, forKey: "KEY_UID")
self.performSegue(withIdentifier: "toFeed", sender: nil)
}
} else
{
KeychainWrapper.standard.set((result?.user.uid)!, forKey: "KEY_UID")
self.performSegue(withIdentifier: "toFeed", sender: nil)
}
}
}
}
}
I would very much appreciate any indication as to why the error does not occur for Kasey and what I might need to change to do the same process.
Thank you in advance !
You're actually pretty close, more of a typo issue. See that your storeUserData function is expecting a string called userID? That's what's needed in the line to store that data instead of user.uid
func storeUserData(userID: String) {
let username = self.usernameField.text
self.ref.child("users").child(userID).setValue(["username": username])
here ^^^^^^ userID instead of user.uid
}

iOS firebase login

my firebase login screen crashes when trying to login
Here is the error:
2018-05-02 09:39:25.937258-0400 noteCollab[2418:625945] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'The link provided is not valid for email/link sign-in. Please check the link by calling isSignInWithEmailLink:link: on Auth before attempting to use it for email/link sign-in.'
* First throw call stack:
(0x1837d6d8c 0x1829905ec 0x1837d6c6c 0x1008aa3e0 0x10089c0b8 0x10089cd08 0x10089b808 0x101a29260 0x101a29220 0x101a37e80 0x101a2c730 0x101a38dd8 0x101a3febc 0x1833fbe70 0x1833fbb08)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
Here is my code:
//
// signinViewController.swift
// noteCollab
//
// Created by James Hall on 5/2/18.
// Copyright © 2018 James Hall. All rights reserved.
//
import UIKit
import Firebase
import FirebaseAuth
class signinViewController: UIViewController {
#IBOutlet weak var signInSelector: UISegmentedControl!
#IBOutlet weak var signInLabel: UILabel!
#IBOutlet weak var emailTextField: UITextField!
#IBOutlet weak var passwordTextField: UITextField!
#IBOutlet weak var signInButton: UIButton!
var isSignIn:Bool = true
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func signInSelectorChanged(_ sender: UISegmentedControl) {
isSignIn = !isSignIn
if isSignIn {
signInLabel.text = "Sign In"
signInButton.setTitle("Sign In", for: .normal)
}else{
signInLabel.text = "Register"
signInButton.setTitle("Register", for: .normal)
}
}
#IBAction func signInButtonTapped(_ sender: UIButton) {
if let email = emailTextField.text, let pass = passwordTextField.text{
//check if its sign in or register
if isSignIn{
//sign in the user with Firebase
Auth.auth().signIn(withEmail: email, link: pass) { (user, error) in
// check that user isnt nil
if error != nil{
print("cant sign in user")
}else{
self.performSegue(withIdentifier: "goToHome", sender: self)
}
}
}else{
//register the user with Firebase
Auth.auth().createUser(withEmail: email, password: pass) { (user, error) in
// check that user isnt nil
if let u = user {
//user is found, go to home
self.performSegue(withIdentifier: "goToHome", sender: self)
}else{
//error: check error and show message
}
}
}
}
}
}
Your sign in code is looking for an email link instead of a password. Simply change the word "link:" to "password:" and you should be all set.
Auth.auth().signIn(withEmail: email, password: password)

Swift Firebase says internal error when creating a new user

I am trying to create new users with Firebase but it is giving me the message "An internal error has occurred, print and inspect the error details for more information." I have enabled the email password authentication on Firebase as well.
This is all the code that I have written as a test and it still doesn't work.
import UIKit
import Firebase
class ViewController: UIViewController {
#IBOutlet weak var emailTextField: UITextField!
#IBOutlet weak var pwordTxtField: UITextField!
#IBOutlet weak var continuebutton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func button(_ sender: Any) {
let email = emailTextField.text
let password = pwordTextField.text
Auth.auth().createUser(withEmail: email! , password: password!, completion: { (user, error) in
if let error = error {
print(error.localizedDescription)
}
else {
print("Success")
}
})
}
}
What I am doing wrong.
Thanks!
Include the following pods in your Podfile:
pod 'Firebase/Auth'
open the auth section and enable Email/password sign-in method and save.
make sure you have added GoogleService-Info.plist file in your project.
in Appdelegate :-
import Firebase
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions:
[UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
return true
}
In Viewcontroller:-
import UIKit
import Firebase
class ViewController: UIViewController {
#IBOutlet weak var emailTextField: UITextField!
#IBOutlet weak var pwordTxtField: UITextField!
#IBOutlet weak var continuebutton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func button(_ sender: Any) {
let email = emailTextField.text
let password = pwordTextField.text
Auth.auth().createUser(withEmail: email! , password: password!,
completion: { (user, error) in
if let error = error {
print(error.localizedDescription)
}
else {
print("Success")
}
})
}
}

Custom Twitter Parse Login in Swift

I want to create custom view controller twitter Parse login. I do not want to use default "loginViewController" fields provided by Parse. I also want to extract user's screen name and profile picture from twitter and save it in Parse. Here is my code.
//SignInController for custom Parse SignIn
import UIKit
import Foundation
import Parse
import ParseUI
class SignInController: UIViewController {
#IBOutlet weak var fbLogin: UIButton! //Facebook login button
#IBOutlet weak var TwitterLogin: UIButton! //Twitter login button
#IBOutlet weak var username: UITextField!
#IBOutlet weak var password: UITextField!
#IBOutlet weak var SignIn: UIButton! //Custom SignIn button for Parse
#IBOutlet weak var signUp: UIButton! //Custom SignUp button for Parse
var actInd: UIActivityIndicatorView=UIActivityIndicatorView (frame: CGRectMake(0, 0, 150, 150)) as UIActivityIndicatorView
override func viewDidLoad() {
super.viewDidLoad()
//Do additional setup after loading the view.
self.actInd.center=self.view.center
self.actInd.hidesWhenStopped=true
self.actInd.activityIndicatorViewStyle=UIActivityIndicatorViewStyle.Gray
view.addSubview(self.actInd)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
//Dispose any resourses that can be recreated.
}
#IBAction func FBLoginAction(sender: AnyObject) {
//Facebook login
}
#IBAction func TwitterLoginAction(sender: AnyObject) {
//Here I want to implement twitter login
}
//Custom Parse SignIn
#IBAction func SignInAction(sender: AnyObject) {
var usernamefield=self.username.text
var passwordfield=self.password.text
if (count(usernamefield.utf16)<4 || count(passwordfield.utf16)<5)
{
alert("Invalid", message: "Username must be greater than 4 and password must be greater than 5")
}
else
{
self.actInd.startAnimating()
PFUser.logInWithUsernameInBackground(usernamefield, password: passwordfield, block: { (user, error) -> Void in
self.actInd.stopAnimating()
if((user) != nil)
{
self.alert("Success", message: "Logged In")
}
else
{
self.alert("Error", message: "\(error)")
}
})
}
}
//Create Account or SignUp Controller
class CreatAccount: UIViewController {
#IBOutlet weak var username: UITextField!
#IBOutlet weak var email: UITextField!
#IBOutlet weak var password: UITextField!
#IBOutlet weak var confirmPassword: UITextField!
#IBOutlet weak var signUp: UIButton!
var actInd: UIActivityIndicatorView=UIActivityIndicatorView (frame: CGRectMake(0, 0, 150, 150)) as UIActivityIndicatorView
override func viewDidLoad() {
super.viewDidLoad()
//Do additional setup after loading the view.
self.actInd.center=self.view.center
self.actInd.hidesWhenStopped=true
self.actInd.activityIndicatorViewStyle=UIActivityIndicatorViewStyle.Gray
view.addSubview(self.actInd)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
//Dispose any resourses that can be recreated.
}
//Custom Parse SignUp
#IBAction func signUpAction(sender: AnyObject) {
var usernamefield=self.username.text
var passwordfield=self.password.text
var emailfield=self.email.text
var confirmpasswordfield=self.confirmPassword.text
if (count(usernamefield.utf16)<4 || count(passwordfield.utf16)<5 || count(confirmpasswordfield.utf16)<5 )
{
alert("Invalid", message: "Username must be greater than 4 and password must be greater than 5")
}
else if(count(emailfield.utf16)<8)
{
alert("Invalid", message: "Please enter a valid email")
}
else if(passwordfield != confirmpasswordfield )
{
alert("Invalid", message: "Passwords mismatch")
}
else
{
self.actInd.startAnimating()
var newUser=PFUser()
newUser.username=usernamefield
newUser.password=passwordfield
newUser.email=emailfield
newUser.signUpInBackgroundWithBlock({ (succeed, error) -> Void in
self.actInd.stopAnimating()
if((error) != nil )
{
self.alert("Invalid", message: "\(error)")
}
else
{
self.alert("Success", message: "Signed Up")
}
})
}
}
Here is my answer for my question. Check TwitterLoginAction.
import UIKit
import Foundation
import Parse
import ParseUI
class SignInController: UIViewController
{
#IBOutlet weak var fbLogin: UIButton!
#IBOutlet weak var TwitterLogin: UIButton!
#IBOutlet weak var username: UITextField!
#IBOutlet weak var password: UITextField!
#IBOutlet weak var SignIn: UIButton!
#IBOutlet weak var signUp: UIButton!
var actInd: UIActivityIndicatorView=UIActivityIndicatorView (frame: CGRectMake(0, 0, 150, 150)) as UIActivityIndicatorView
override func viewDidLoad() {
super.viewDidLoad()
//Do additional setup after loading the view.
self.actInd.center=self.view.center
self.actInd.hidesWhenStopped=true
self.actInd.activityIndicatorViewStyle=UIActivityIndicatorViewStyle.Gray
view.addSubview(self.actInd)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
//Dispose any resourses that can be recreated.
}
#IBAction func FBLoginAction(sender: AnyObject) {
}
#IBAction func TwitterLoginAction(sender: AnyObject) {
PFTwitterUtils.logInWithBlock { (user, error) -> Void in
if (user==nil) {
println(user)
println("Uh oh. The user cancelled the Twitter login.")
return;
} else if ((user?.isNew) != nil) {
println("User signed up and logged in with Twitter!")
} else {
println("User logged in with Twitter!")
}
}
}
//Parse SignIn
#IBAction func SignInAction(sender: AnyObject) {
var usernamefield=self.username.text
var passwordfield=self.password.text
if (count(usernamefield.utf16)<4 || count(passwordfield.utf16)<5)
{
alert("Invalid", message: "Username must be greater than 4 and password must be greater than 5")
}
else
{
self.actInd.startAnimating()
PFUser.logInWithUsernameInBackground(usernamefield, password: passwordfield, block: { (user, error) -> Void in
self.actInd.stopAnimating()
if((user) != nil)
{
self.alert("Success", message: "Logged In")
}
else
{
self.alert("Error", message: "\(error)")
}
})
}
}
}
Here is the code for Twitter Login in Swift 3
PFTwitterUtils.logIn { (user, error) in
if (user==nil) {
print("Uh oh. The user cancelled the Twitter login.")
} else if ((user?.isNew) != nil) {
print("User signed up and logged in with Twitter!")
} else {
print("User logged in with Twitter!")
}
}

Resources