Server-Side Sign in with Apple

Nov 15 2022 · Swift 5.6, macOS 12, iOS 15, Xcode 13.3

Part 1: Add Sign in with Apple to an iOS Project

05. Authenticate Existing Users with Sign in with Apple

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 04. Connect Your iOS App to Your Vapor App Next episode: 06. Setting up Sign in with Apple for the Web

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

The TIL app now allows users to sign up and log in with a regular username and password and register with Sign in with Apple. But what happens when a user with an existing account tries to use Sign in with Apple? At the moment they’ll get an error because their user already exists. It’s time to fix that!

Handling existing users

If the Vapor app is still running from the previous video, stop it with CRTL+C and open Package.swift to open the project in Xcode. Navigate to UsersController.swift and find the signInWithApple(_:) route handler. Here you want to see if you can find the user using their email before creating a new one:

if let existingUser = try await User.query(on: req.db).filter(\.$username == email).first() {
    
} else {

}
let newUser = User(name: name, username: email, password: UUID().uuidString, siwaIdentifier: siwaToken.subject.value)
try await newUser.save(on: req.db)
user = newUser
user = existingUser
user.siwaIdentifier = siwaToken.subject.value
try await user.save(on: req.db)

Trying it out

To try this out, first you need to reset the database so you can register via the normal means:

swift Scripts/dockerDB.swift reset
swift run