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
Create an instance of SignInWithAppleToken with the data collected. This is the same type as your Vapor app!
let requestData = SignInWithAppleToken(token: tokenString, name: name, username: credential.email)
Then, create the URL to send the data to:
let path = "\(apiHostname)/api/users/siwa"
guard let url = URL(string: path) else {
fatalError("Failed to convert URL")
}
This uses the apiHostname that’s passed in from AppMain. Next create a do/catch block to send the request. In the catch, show the login error and rethrow the error:
do {
} catch {
self.showingLoginErrorAlert = true
throw error
}
Create a URLRequest from the URL, set the method to POST and encode the request data to the body. Also set the content-type header:
var loginRequest = URLRequest(url: url)
loginRequest.httpMethod = "POST"
loginRequest.httpBody = try JSONEncoder().encode(requestData)
loginRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
Send the request and ensure you get a valid response:
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
}
Finally, decode the response to a Token type as you set in the Vapor app:
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
Next, edit the .env file - this stores environment variables the app needs. In a previous video, you read an environment variable called IOS_APPLICATION_IDENTIFIER. So edit the .env file and add the variable. Set it to the application identifier you used in the previous video:
IOS_APPLICATION_IDENTIFIER=dev.timc.siwa-demo.TILiOS
Then, run the Vapor app:
swift run
If the firewall asks you to allow connections, click Allow. When it’s started, you’ll see the “Server started” message in the console. Then, find your IP address. There are a number of ways of doing this, but OPTION clicking on the Wifi symbol in the status bar will show you it.
Open the TILiOS project in Xcode and open AppMain.swift. Change the API hostname to use your machines IP address, using port 8080. Then build and run the app. Sign in with Apple currently doesn’t work on the simulator so you’ll need a real device to do this.
When the app launches, you’ll be taken to the log in screen. Click Sign in with Apple and complete the authentication flow. You’ll end up logged in!
Back in terminal, make a request to /api/users and you’ll see your user account returned.