Server-Side Sign in with Apple

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

Part 2: Add Sign in with Apple to a Website

9. Authenticate Sign in with Apple Users on the Web

Episode complete

Save for later
About this episode
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 8. Handle the Sign in with Apple Callback

This video Authenticate Sign in with Apple Users on the Web 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 Professional subscription.

The last piece of the puzzle is implementing the logic for completing the Sign in with Apple flow.

func appleAuthRedirectHandler(_ req: Request) async throws -> Response {

let data = try req.content.decode(SIWARedirectData.self)
guard let appIdentifier = Environment.get("WEBSITE_APPLICATION_IDENTIFIER") else {
    throw Abort(.internalServerError)
let siwaToken = try await, applicationIdentifier: appIdentifier)
let user: User
if let userFound = try await User.query(on: req.db).filter(\.$siwaIdentifier == siwaToken.subject.value).first() {
  user = userFound
} else {

guard let email =, let firstName = data.firstName, let lastName = data.lastName else {
  throw Abort(.badRequest)
if let existingUser = try await User.query(on: req.db).filter(\.$username == email).first() {

} else {

user = existingUser
user.siwaIdentifier = siwaToken.subject.value
try await req.db)
let newUser = User(name: "\(firstName) \(lastName)", username: email, password: UUID().uuidString, siwaIdentifier: siwaToken.subject.value)
try await req.db)
user = newUser
return req.redirect(to: "/")"login", "siwa", "handle", use: appleAuthRedirectHandler)