Get Started with iOS Authentication using Swift and UIKit
Tags: mobile, ios, security
Published: 2021-12-10 13:51:40

#### A step-by-step tutorial on using Auth0 and Swift to implement login/logout and use user metadata in iOS apps.

### <br>

If you’re planning to build a native iOS app that requires users to log in, you *could* implement authentication yourself — or you can use Auth0 and focus your time and effort on your app’s *actual* functionality.

Building a user login/logout system looks simple. It often turns into its own project. Just handling the many ways users want to log in can easily take a lot of time. You’d also have to deal with issues such as user management, scaling, and security, each of which has dozens of considerations, risks, issues, and edge cases.

Auth0 solves this problem. With Auth0 and a few lines of code, your app can have a full-featured system that supports logging in with a username/password combination, single sign-on and social accounts, passwordless login, biometrics, and more. You won’t have to handle the “behind the scenes” issues, either! Instead, you can focus on what your app does.

**This tutorial covers the basics of using Auth0 to implement login/logout in an iOS app written using Swift and the UIKit framework.** It also provides an introduction to reading and writing information into a user account’s metadata so that your app can “remember” each user’s status, configurations, and preferences. Along the way, you’ll become familiar with the Auth0 dashboard and learn how to use it to register applications and manage users and their metadata.

> **Look for the 🛠 emoji if you’d like to skim through the content while focusing on the build and execution steps.** 


## What You’ll Build

You’ll build a simple, single-screen iOS app that will allow the user to log in and log out using Auth0. While logged in, the user will be able to see the following information from their user profile, namely:

* The following basic information from the user account:
	* The user’s name
	* The user’s email address
* User metadata, specifically the user’s favorite color, which they will be able to edit in the app
* An announcement if a flag in the user’s app metadata has been set

### A quick tour of the app

When you launch the completed app, you’ll see a greeting, a **Log In** button, and a disabled **Log Out** button:

![The app’s “Welcome” screen. The “Log In” button is enabled, and the “Log In” button is disabled.](https://images.ctfassets.net/23aumh6u8s0i/1yFKSaXbb0jLpvSP37ZSD1/e5dd0b31e231450d81f1f7d1b22e06d3/finished_app_-_welcome_screen.png)

Tapping the **Log In** button takes the user to the [Auth0 Universal Login screen](https://auth0.com/docs/login/universal-login), which appears in a custom browser window:

![The default Auth0 Universal Login web page, with Auth0 logo and “email address” and “password” fields.](https://images.ctfassets.net/23aumh6u8s0i/29unD3DL7wIRlacIbgXw8J/4f730e99dae87de64273ea0f3219695a/universal_login.png)

When you use Auth0 to add login/logout capability to your apps, you delegate authentication to a centralized login page in the same way that Google properties such as Gmail and YouTube redirect you to **accounts.google.com** when you log in. In this exercise, you’ll use the default “look and feel” for the login page, but [you can customize it to match your app or organization’s branding](https://auth0.com/docs/universal-login/new-experience/universal-login-page-templates).

If the user logs in successfully, the Universal Login screen and its browser window disappear, and the user returns to the app. 

Immediately after login, the app checks the app metadata associated with the user’s account for a “make an announcement” flag. If this flag is set, the app shows this alert box:

![The app displays an alert that tells the user if the alert is visible, then the “make_announcement” flag in “app_metadata” is set to “true”.](https://images.ctfassets.net/23aumh6u8s0i/47pe3bEupD0XBA6KLtw9id/2e5ad517195ff269238373bf35258bee/finished_app_-_logged_in_-_announcement.png)

This alert box doesn’t appear if the flag *isn’t* set.

Here’s what the app looks like once the user is logged in and has dismissed the alert box (if it appeared):

![The app in its “logged in” state. The “Log In” button is disabled, “Log Out” button is enabled, and the app’s other controls are visible.](https://images.ctfassets.net/23aumh6u8s0i/yNc58XQjBvMrt18aYk0FY/b9efe02e3e738e3b7e9c283c9f2ee0c8/finished_app_-_logged_in.png)

Here’s what changed after login:

* The greeting will have changed to a notification telling you that you’re now logged into the app.
* The **Log In** button will now be disabled, and the **Log Out** button will now be enabled.
* The name and email address for the user account that you used to log in will be displayed.
* There will now be a section for metadata, complete with an editable text field and **Get** and **Set** buttons that will allow you to read and write to the `favorite_color` field of the metadata for the user account.

As you might expect, logging out is done by tapping the **Log Out** button. Once logged out, the user will see this:

![The app in its “logged out” state. The “Log In” button is enabled, “Log Out” button is disabled, and the app’s other controls are hidden.](https://images.ctfassets.net/23aumh6u8s0i/61JF1QFqXo4Spfl2OEadPi/0a0f5e1bdd7a6ef1a70461aed1a17160/logged_out.png)

Note that:

* The notification at the top of the screen will now inform you that you’re logged out of the app.
* Logging out enables the **Log in** button and disables the **Log out** button.
* The other controls that were visible when you were logged in will no longer be visible.

[Read more...](https://auth0.com/blog/get-started-ios-authentication-swift-uikit/?utm_source=content_synd&utm_medium=sc&utm_campaign=mobile)