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

04. Connect Your iOS App 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: 03. Add Sign in with Apple to Your iOS App Next episode: 05. Authenticate Existing Users with Sign in with Apple

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.

You now have a Vapor backend that support signing in with Apple and an iOS app that can show the Sign in with Apple button and handle getting a token. It’s time to connect the two up!

Sending the Sign in with Apple token to the backend

First, open the TILiOS app in Xcode. Inside handleSIWA(result:) remove the error you added at the end of the last video to make it compile:

throw AuthError.notLoggedIn
let requestData = SignInWithAppleToken(token: tokenString, name: name, username: credential.email)
let path = "\(apiHostname)/api/users/siwa"
guard let url = URL(string: path) else {
    fatalError("Failed to convert URL")
}
do {

} catch {
    self.showingLoginErrorAlert = true
    throw error
}
var loginRequest = URLRequest(url: url)
loginRequest.httpMethod = "POST"
loginRequest.httpBody = try JSONEncoder().encode(requestData)
loginRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
let (data, response) = try await URLSession.shared.data(for: loginRequest)
guard let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 else {
    self.showingLoginErrorAlert = true
    throw AuthError.badResponse
}
return try JSONDecoder().decode(Token.self, from: data)

Running the Vapor app

In Terminal, navigate to your Vapor app. Execute the script to create a Postgres database in Docker:

swift Scripts/dockerDB.swift start
IOS_APPLICATION_IDENTIFIER=dev.timc.siwa-demo.TILiOS
swift run