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

Next
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’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

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: "https://github.com/vapor/jwt.git", 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 req.jwt.apple.verify(data.token, 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 = siwaToken.email, let name = data.name else {
    throw Abort(.badRequest)
}
let newUser = User(name: name, username: email, password: UUID().uuidString, siwaIdentifier: siwaToken.subject.value)
try await newUser.save(on: req.db)
user = newUser
let token = try Token.generate(for: user)
try await token.save(on: req.db)
return token
usersRoutes.post("siwa", use: signInWithApple)