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

4. Connect Your iOS App to Your Vapor App

Episode complete

Play next episode

Next
Save for later
About this episode
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 3. Add Sign in with Apple to Your iOS App Next episode: 5. Authenticate Existing Users with Sign in with Apple

This video Connect Your iOS App to Your Vapor App 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.

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