iOS-Swift Custom Login

Sample Project

Download this sample project configured with your Auth0 API Keys.

System Requirements
  • CocoaPods 1.0.0
  • XCode 7.3 (7D175)
  • iPhone 6 - iOS 9.3 (13E230)
Show requirements

Implement the Login

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

import Auth0

Then, add the following code to perform a login:

Auth0
    .authentication()
    .login(
        emailOrUsername: "email@foo.com",
        password: "123456",
        connection: "Username-Password-Authentication"
    )
    .start { result in
            switch result {
            case .Success(let credentials):
                // Logged in successfully
                // You've got a Credentials instance, which you'll use, for example, to retrieve the User Profile
            case .Failure(let error):
                // You've got an error
            }
    }

That's it! You'll get either a credentials object or an error case after performing a login.

Basically, credentials contains token-related information; you will normally store this object for later use. On the other hand, error is an enum containing possible authentication error cases that you might want to keep track of.

For further reference on the credentials and error objects, check the Credentials and Authentication files documentation.

Retrieve the User Profile

Once you've obtained a Credentials object, retrieving a user profile is quite simple. All you have to do is:

guard let idToken = credentials.idToken else { return }
Auth0
    .authentication()
    .tokenInfo(token: idToken)
    .start { result in
        switch result {
        case .Success(let profile):
            // You've got a UserProfile object
        case .Failure(let error):
            // You've got an error
        }
}

A trivial example of how to use some profile info:

welcomeLabel.text = "Welcome, \(profile.name)!"
memberLabel.text = "You are member since \(profile.createdAt)"

For further reference on the profile and error objects, check the UserProfile and Authentication files documentation.

Implement a Sign Up

Including a register process in your app is also a piece of cake.

First, don't forget to import the toolkit module in your register form view controller:

import Auth0

Then, all you have to do is:

Auth0
    .authentication()
    .signUp(
        email: "foo@email.com",
        password: "123456",
        connection: "Username-Password-Authentication",
        userMetadata: ["first_name": "Foo", "last_name": "Bar"] // or any extra user data you need
    )
    .start { result in
            switch result {
            case .Success(let credentials):
              // Registered successfully
              // You've got a Credentials object
            case .Failure(let error):
                // You've got an error
            }
        }
}

Notice that any extra information that you need to add to the user's profile, other than the email and password, goes within the userMetadata dictionary, which is passed as a parameter to this function.

Perform Social Authentication

First, go to your Client Dashboard and make sure that Allowed Callback URLs contains the following:

{YOUR_APP_BUNDLE_IDENTIFIER}:///ios/{YOUR_APP_BUNDLE_IDENTIFIER}/callback

In your application's Info.plist file, register your iOS Bundle Identifier as a custom scheme. To do so, open the Info.plist as source code, and add this chunk of code under the main <dict> entry:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleTypeRole</key>
        <string>None</string>
        <key>CFBundleURLName</key>
        <string>auth0</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>{YOUR_APP_BUNDLE_IDENTIFIER}</string>
        </array>
    </dict>
</array>

Remember to replace all the {YOUR_APP_BUNDLE_IDENTIFIER} appearances with your actual app's bundle identifier, which you can get from your project settings.

The Auth0.swift toolkit will only handle URLs with your Auth0 domain as host, for instance: com.auth0.MyApp://samples.auth0.com/ios/com.auth0.MyApp/callback

Then, add the following function in your application's AppDelegate:

import Auth0
func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
    return Auth0.resumeAuth(url, options: options)
}

Finally, perform webauth social authentication by specifying a social connection, for instance, Facebook:

Auth0
    .webAuth()
    .connection("facebook")
    .scope("openid")
    .start { result in
        switch result {
        case .Success(let credentials):
            // You've got your credentials
        case .Failure(let error):
            // Handle the error
        }
    }

Once you get the credentials object, upon a successful authentication, you deal with them as usual. For more information on that topic, check out the login and session handling tutorials.

Previous Tutorial
2. Login
Use Auth0 for FREECreate free Account