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

02. Add Sign in with Apple to Your Vapor App

Episode complete

Play next episode

About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 01. Learn About Sign in with Apple Next episode: 03. Add Sign in with Apple to Your iOS App

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've reached locked video content where the transcript will be shown as obfuscated text.

In this video we’re going to set up a Vapor app to handle Sign in with Apple from an iOS device. Let’s get started!

Adding JWT

First open TILApp in the starter project for this course in Xcode and open Package.swift. In the packages dependencies array, add a new dependency for Vapor’s JWT library:

.package(url: "", from: "4.0.0"),
.product(name: "JWT", package: "jwt"),
struct SignInWithAppleToken: Content {
  let token: String
  let name: String?
import JWT
func signInWithApple(_ req: Request) async throws -> Token {

let data = try req.content.decode(SignInWithAppleToken.self)
guard let appIdentifier = Environment.get("IOS_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 name = else {
    throw Abort(.badRequest)
let newUser = User(name: name, username: email, password: UUID().uuidString, siwaIdentifier: siwaToken.subject.value)
try await req.db)
user = newUser
let token = try Token.generate(for: user)
try await req.db)
return token"siwa", use: signInWithApple)