Appearance
Login with custom auth
INFO
Before following the instructions below, you must first configure your app in the Privy Developer Dashboard to use custom auth, per these instructions.
Privy's embedded wallets are fully-compatible with any authentication provider that supports JWT-based, stateless authentication. If you're looking to add embedded wallets to your app, you can use a custom authentication provider.
If your app uses a custom provider to authenticate your user, you will need to initialize a session with Privy in order to provision embedded wallets for that user.
1. Initialize Privy using a callback to get the user's access token
First, initialize the Privy SDK with a tokenProvider
callback to get the user's access token.
The function should return the user's current access token from your custom authentication provider as a String?
. If the user is not authenticated, this lambda should return null.
dart
Future<String?> _retrieveCustomAuthAccessToken() async {
// Implement logic to fetch the access token from your auth provider
// Return the token if the user is authenticated, or null if not
return "access_token";
}
final privyConfig = PrivyConfig(
appId: "YOUR_APP_ID",
appClientId: "YOUR_APP_CLIENT_ID",
logLevel: PrivyLogLevel.NONE,
customAuthConfig: LoginWithCustomAuthConfig(
tokenProvider: _retrieveCustomAuthAccessToken,
),
);
final privy = Privy(config: privyConfig);
INFO
When the Privy SDK is first initialized, Privy will attempt to restore a prior session, if it exists. If it does, Privy automatically attempts to reauthenticate the user using the tokenProvider
you provide. This is why it's important to await ready before triggering any other Privy flows.
You may also call loginWithCustomAccessToken
, as shown below to manually trigger the login flow.
2. Authenticate your user
Once you have initialized Privy with a tokenProvider
callback, to authenticate your user with Privy at any time, use the Privy SDK's privy.customAuth.loginWithCustomAccessToken
method.
Internally, this triggers the tokenProvider
callback to retrieve a custom access token and authenticate the user.
Example:
dart
final result = await privy.customAuth.loginWithCustomAccessToken();
result.fold(
onSuccess: (user) {
print("Privy login success! User: ${user}");
},
onFailure: (error) {
print("Privy login failure! $error.message");
},
);
If the provided access or identity token is invalid, loginWithCustomAccessToken
will return Result.failure
. If the token is valid, Privy will successfully authenticate your user and loginWithCustomAccessToken
will return Result.success
with an encapsulated PrivyUser
.
Once your user is authenticated, you have access to the PrivyUser.
Privy identifies your user based on the unique ID that your auth provider has assigned the user, which is stored as the sub
claim of their access token. You can view all users in the Users section of the Privy Developer Dashboard.