DEV Community

Robertino
Robertino

Posted on • Originally published at auth0.com

Get Started with iOS Authentication using SwiftUI

If you’re an iOS developer who’s new to SwiftUI and authentication with Auth0, you’re in for a treat. In this tutorial, you’ll build an app in SwiftUI that implements basic login and logout using Auth0.


If you’re an iOS developer who’s new to SwiftUI and authentication with Auth0, you’re in for a treat. In this tutorial, you’ll build an app in SwiftUI that implements basic login and logout using Auth0!

This tutorial covers using version 2.0.0 of the Auth0.swift SDK to implement login/logout in an iOS app written using the Swift programming language and the SwiftUI framework. In the process, you’ll get a quick introduction to the basics of our newly revised Swift SDK and how to use it with Apple’s SwiftUI framework. You’ll also become familiar with the Auth0 dashboard and learn how to use it to register applications and manage users.

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

Apple’s SwiftUI Framework

SwiftUI logoSwiftUI is a user interface toolkit and “the new way” for building apps that Apple introduced in 2019. With SwiftUI, you build user interfaces using Swift code with a declarative approach, which means that you specify how the UI should look and behave under different states. You don’t have to specify how the UI moves between those states — it takes care of that for you. If you know React programming, you’ll find SwiftUI familiar.

The SwiftUI approach is quite different from the one used by UIKit, iOS’ original UI toolkit, which dates back to 2007. With UIKit, you build user interfaces using Interface Builder, a drag-and-drop layout tool to build the app’s views graphically. You connect UI elements to variables and methods in the app’s view controllers using outlets and actions. UIKit uses an imperative approach, which means that you define how the program moves between states and how the UI should look and behave in those states.

Auth0 and the Auth0.swift SDK

Auth0 logoAdding login and logout to an app may seem like a simple task — until you try it. You have to handle the many ways to log in, confirm email addresses and passwords, manage users, and handle security and scalability. Each of these issues 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 your app’s main functionality.

Auth0.swift is the Auth0 SDK for all Apple platforms: not just iOS, but macOS, iPadOS, watchOS, and tvOS. It’s our third most-used SDK, accounting for 11% of all API requests made to our systems. If you’re building an Apple device application that needs to authenticate or authorize its users, you need this SDK!

The newest version of Auth0.swift, version 2.0.0, incorporates what we’ve learned from securing applications on Apple devices over the past five years. If you’ve used earlier versions, you’ll appreciate its updated error handling, support for async/await and Combine, improved default configuration, new documentation, and that we removed deprecated methods and legacy features. If you’re new to Auth0, you’ll be pleasantly surprised at how little code you’ll need to write to add authentication to your apps.

What You’ll Build

In this tutorial, 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 screen will have a different appearance and show the user their name, email address, and photo from their profile.

Instead of building the app from a starter project, you’ll build it “from scratch,” starting with selecting FileNewProject… from Xcode’s menu bar.

Read more...

Top comments (0)