Following the next steps you will be able to build an App that will Sign-in, Sign-up and Logout at Back4App Database.
1 - Set up and Create your Sign Up and Login UI
Go to Xcode, access the main folder from the Project and then open the ViewController.swift file to edit it.
In ViewController.swift make sure to include the Parse module by including it at the top of the file.
Swift
1 import Parse
3. Go to Main.storyboard, drag four UITextFields onto the ViewController in the main storyboard. Center the textfield and put two at the top and two at the bottom of the view controller. Drag two more UIButtons on to the view and place them below the textfields. Drag one more Loader Indicators on each Button.
Set the top button text to say Sign In. Set the bottom bottom to say Sign Up. Set the text fields to say username and password.
4. Next we are going to connect your UITextFields in your storyboard to properties in your view controller. Add the following properties to the top of ViewController.swift. Next go to your storyboard and right click on each UITextField and click on the reference outlet then drag a line back to the ViewController icon and set it to the appropriate field. signInUsernameField connects to the sign In Username Field, etc…
It should look like this:
Swift
1 import UIKit
2 import Parse
34 class ViewController: UIViewController {56 @IBOutlet weak var txtUsernameSignin: UITextField!7 @IBOutlet weak var txtPasswordSignin: UITextField!8 @IBOutlet weak var indicatorLogin: UIActivityIndicatorView!910 @IBOutlet weak var txtUsernameSignup: UITextField!11 @IBOutlet weak var txtPasswordSignup: UITextField!12 @IBOutlet weak var indicatorSignup: UIActivityIndicatorView!1314 override func viewDidLoad(){15 super.viewDidLoad()16// Do any additional setup after loading the view.17}1819 @IBAction func signin(_ sender: Any){20//todo21}2223 @IBAction func signup(_ sender: Any){24//todo25}2627}
2 - Create a Sign-in function
Add the following code inside the sign-in function:
Add the following code inside the sign-up function:
Swift
1 @IBAction func signup(_ sender: Any){2 let user =PFUser()3 user.username = self.txtUsernameSignup.text
4 user.password = self.txtPasswordSignup.text
56 self.indicatorSignup.startAnimating()7 user.signUpInBackground {(succeeded: Bool, error: Error?)-> Void in8 self.indicatorSignup.stopAnimating()9if let error = error {10 self.displayAlert(withTitle:"Error", message: error.localizedDescription)11}else{12 self.displayAlert(withTitle:"Success", message:"Account has been successfully created")13}14}15}
4 - Log Out
When logging, it creates a Session object, which points to the User logged in. If login is successful, ParseUser.CurrentUser() returns a User object, and a Session object is created in the Dashboard. Otherwise, if the target username does not exist, or the password is wrong, it returns null.
To do Logout follow the steps below:
Go to Main.storyboard, drag one UIButton called “Logout”, and add a action between this UIbutton and the ViewController.swift.
Find your app and click on Dashboard>Core>Browser>User.
Try logging in and out with the same user and signing back in.
At this point, you should see your users as displayed below:
Note: Using the codes displayed above, every time you log in with a user, a Session is opened in your Dashboard, but when the user logs out that particular Session ends. Also, whenever an unsuccessful login or sign up attempt occurs, the Session opened in Parse Server Dashboard is deleted.
It’s done!
At this stage, you can log in, register or log out of your app using Parse Server core features through Back4App!