iOS-Swift Login

Sample Project

Download this sample project configured with your Auth0 API Keys.

Implement the Login

First, import the Lock module in the file where you want to present the login dialog:

import Lock

Then, configure and present the login screen, like this:

let controller = A0Lock.sharedLock().newLockViewController()
controller.closable = true
controller.onAuthenticationBlock = { profile, token in
    // Do something with token  profile. e.g.: save them.
    // Lock will not save these objects for you.

    // Don't forget to dismiss the Lock controller
    controller.dismissViewControllerAnimated(true, completion: nil)
}
A0Lock.sharedLock().presentLockController(controller, fromController: self)

Lock.png

There are multiple ways of implementing the login dialog. What you see above is the default widget; however, if you want, you can use your own UI. You can also try our passwordless Login Widgets: SMS or TouchID.

As you can see, upon successful authentication, the onAuthenticationBlock callback will yield the user's profile and token.

A basic example of how to use this information:

self.usernameLabel.text = profile.name
self.emailLabel.text = profile.email
MyApplication.sharedInstance.accessToken = token.accessToken

For further reference on the profile and token objects, check the A0UserProfile and A0Token classes' documentation.

To learn how to save and manage the tokens and profile in detail, please read this guide. Notice that Lock on its own will not save the tokens and profile for you.

Optional: Log In with Social Connections

In order to have a simple login mechanism through social connections, all you have to do is enable them in your account's connections dashboard. Every social connection you switch on there, will appear in the Login screen of your app. That's pretty much it!

Previous Tutorial
1. Introduction
Next Tutorial
3. Custom Login
Use Auth0 for FREECreate free Account