Authenticate using Apple
You can let your users authenticate with Portkey using their Apple ID by using the Portkey SDK for sign-in flow.
Access the PortkeySDK.AuthService
The AuthService
class is the entry point for all authentication API calls. It is accessable through PortkeySDK.AuthService
.
IAuthService auth = PortkeySDK.AuthService;
Authenticate with PortkeySDK
- User is to get a Apple Credential by signing in to their Apple ID through the AppleCredentialProvider.
// mySuccessCallback will be called when a Credential is successfully returned by AppleCredentialProvider.
PortkeySDK.AuthService.AppleCredentialProvider.Get(mySuccessCallback);
- After getting Apple Credential, we will check if the user has already signed up beforehand with the corresponding Apple Credential. A user who already has an account with Portkey should have one or more Guardians returned from the callback.
StartCoroutine(PortkeySDK.AuthService.GetGuardians(appleCredential, guardians =>
{
if(guardians.Count == 0)
{
// No guardians, have never signed up before...
}
else
{
// Has a Portkey account...
}
}));
Depending if the user has a Portkey account, we can either sign up or login.
Sign up flow
- Sign in for the user through the
SignUp
function. ADIDAccountInfo
will be returned after a successful sign up that contains the signing key and account information for the user.StartCoroutine(PortkeySDK.AuthService.SignUp(appleCredential, didAccountInfo =>
{
// user has successfully signed up and his info can be found in DIDAccountInfo
}));
- Sign in for the user through the
Login flow
Get the amount of approved Guardians that is required to login the user.
int requiredApprovedGuardians = PortkeySDK.AuthService.GetRequiredApprovedGuardiansCount(guardians.Count);
Verify the Guardians through
AuthService
to getApprovedGuardians
for the login.List<ApprovedGuardian> approvedGuardians = new List<ApprovedGuardian>();
for (int i = 0; i < requiredApprovedGuardians; ++i)
{
StartCoroutine(PortkeySDK.AuthService.Verify(guardians[i], approvedGuardian =>
{
approvedGuardians.Add(approvedGuardian);
}));
}Use the
ApprovedGuardians
to login the user.StartCoroutine(PortkeySDK.AuthService.Login(guardians[0], approvedGuardians, didAccountInfo =>
{
// user is logged in and his info and signing key can be found in DIDAccountInfo
}));
Example
The following is a sample code to authenticate a user through the whole sign up/login flow with their Apple ID.
using Portkey.DID;
public class AppleLoginExample : MonoBehaviour
{
[SerializeField] private PortkeySDK PortkeySDK;
public void SignInWithApple()
{
PortkeySDK.AuthService.AppleCredentialProvider.Get(credential =>
{
StartCoroutine(PortkeySDK.AuthService.GetGuardians(credential, guardians =>
{
if(guardians.Count == 0)
{
// No guardians, proceed to sign up
StartCoroutine(PortkeySDK.AuthService.SignUp(credential, didAccountInfo =>
{
// user has successfully signed up and his info can be found in DIDAccountInfo
}));
}
else
{
int requiredApprovedGuardians = PortkeySDK.AuthService.GetRequiredApprovedGuardiansCount(guardians.Count);
List<ApprovedGuardian> approvedGuardians = new List<ApprovedGuardian>();
// Guardians exist, proceed to sign in
for (int i = 0; i < requiredApprovedGuardians; ++i)
{
StartCoroutine(PortkeySDK.AuthService.Verify(guardians[i], approvedGuardian =>
{
approvedGuardians.Add(approvedGuardian);
}));
}
while(approvedGuardians.Count < requiredApprovedGuardians)
{
// wait for guardians to be approved
}
// first element of the guardians array is the login guardian
StartCoroutine(PortkeySDK.AuthService.Login(guardians[0], approvedGuardians, didAccountInfo =>
{
// user is logged in and his info can be found in DIDAccountInfo
}));
}
}));
});
}
}
Next Steps
After the user has signed up or logged in, a DIDAccountInfo
will be returned that corresponds to the Apple ID used to login. DIDAccountInfo
contains signing key and account information that can be used to get a user's account balance and sign transactions.
Sign out
To sign out a user, call Logout
.
StartCoroutine(PortkeySDK.AuthService.Logout());