Shortcut Authentication provides a convenient way to authenticate user with AppleID using AuthenticationServices and Combine.
Before you start using this package, you'll need to add Sign in with Apple capability to your project, otherwise this will not work.
Once these steps are complete, you can start adding the support for AppleAuthentication.
NOTE: If you would like to listen to the account revoke notification from users iCloud account you'll need to keep the instance alive.
import AppleAuthentication
private let appleAuthentication = AppleAuthentication()
Use authenticate()
method to authenticate. It returns token as a String
as well as ASAuthorizationAppleIDCredential
. You can also pass requestedScopes
as a parameter, default requested scopes are fullName and email.
appleAuthentication.authenticate()
.receive(on: RunLoop.main)
.sink { completion in
switch completion {
case .finished:
break
case .failure(let error):
self.error = error.localizedDescription
}
} receiveValue: { [weak self] token in
self.authToken = token
}
.store(in: &cancellables)
Use getCredentialState to get AppleIdCredentialSate
. AppleIdCredentialSate
is an enum which represents the states of AppleID credentials. For example: .authorize
or .revoked
.
appleAuthentication.getCredentialState(for: userId)
.receive(on: RunLoop.main)
.sink { completion in
switch completion {
case .finished:
break
case .failure(let error):
self.error = error.localizedDescription
}
} receiveValue: { [weak self] state in
self?.credentialState = state
if state == .authorized {
// Go to tab view
}
}
.store(in: &cancellables)
If you'd like to listen to the account revoke notification from user's iCloud account use credentialStatePublisher
. When the state is revoked, it's recommended to log out the user but it's not obligated by Apple.
appleAuthentication.credentialStatePublisher
.receive(on: RunLoop.main)
.sink { _ in
} receiveValue: { [weak self] state in
if state == .revoked {
// Logout user
}
}
.store(in: &cancellables)
To find more see the example project.