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

5. Authenticate Existing Users with Sign in with Apple

Episode complete

Play next episode

Next
Save for later
About this episode
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 4. Connect Your iOS App to Your Vapor App Next episode: 6. Setting up Sign in with Apple for the Web

This video Authenticate Existing Users with Sign in with Apple was last updated on Nov 15 2022

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.

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