Skip to main content

Using of PnP Unity SDK

Once you've installed and successfully initialized Web3Auth, you can use it to authenticate your users.

Logging in a User

web3Auth.login(LoginParams)

This function helps your users to trigger the login process. The login flow is triggered based on the selected provider. You can view the list of selected providers in the provider section.

tip

Additionally, you can associate a function to be triggered on successful login. This function receives the user's profile as a parameter.

void Start()
{
web3Auth = GetComponent<Web3Auth>();
web3Auth.setOptions(new Web3AuthOptions()
{

});
web3Auth.onLogin += onLogin;
}
private void onLogin(Web3AuthResponse response)
{
// Functions to be called after logging in your user
}

Arguments

web3Auth.login() requires LoginParams as a required input.

LoginParams

ParameterDescription
loginProviderIt sets the OAuth login method to be used. You can use any of the supported values are GOOGLE, FACEBOOK, REDDIT, DISCORD, TWITCH, APPLE, LINE, GITHUB, KAKAO, LINKEDIN, TWITTER, WEIBO, WECHAT, EMAIL_PASSWORDLESS.
extraLoginOptions?It can be used to set the OAuth login options for corresponding loginProvider. For instance, you'll need to pass the user's email address as. The Default value for the field is null, and it accepts ExtraLoginOptions as a value.
redirectUrl?Url where user will be redirected after successfull login. By default user will be redirected to same page where login will be initiated. Default value for the field is null, and accepts URI as a value.
appState?It can be used to keep track of the app state when user will be redirected to app after login. Default is null, and accepts string as a value.
mfaLevel?Customize the MFA screen shown to the user during OAuth authentication. Default value for field is MFALevel.DEFAULT, which shows MFA screen every 3rd login. It accepts MFALevel as a value.
dappShare?Custom verifier logins can get a dapp share returned to them after a successful login. This is useful if the dapps want to use this share to allow users to login seamlessly. It accepts string as a value.
curve?It is used to determine the public key encoded in the jwt token which returned in getUserInfo function after user login. This parameter won't change the format of private key returned by Web3Auth. Private key returned by getPrivKey is always secp256k1. The default value is Curve.SECP256K1.
sessionTime?It allows developers to configure the session management time. Session Time is in seconds, and takes an int as a value.

Provider

public enum Provider
{
[EnumMember(Value = "google")]
GOOGLE,
[EnumMember(Value = "facebook")]
FACEBOOK,
[EnumMember(Value = "reddit")]
REDDIT,
[EnumMember(Value = "discord")]
DISCORD,
[EnumMember(Value = "twitch")]
TWITCH,
[EnumMember(Value = "apple")]
APPLE,
[EnumMember(Value = "line")]
LINE,
[EnumMember(Value = "github")]
GITHUB,
[EnumMember(Value = "kakao")]
KAKAO,
[EnumMember(Value = "linkedin")]
LINKEDIN,
[EnumMember(Value = "twitter")]
TWITTER,
[EnumMember(Value = "weibo")]
WEIBO,
[EnumMember(Value = "wechat")]
WECHAT,
[EnumMember(Value = "email_passwordless")]
EMAIL_PASSWORDLESS,
[EnumMember(Value = "email_password")]
EMAIL_PASSWORD,
[EnumMember(Value = "jwt")]
JWT,
[EnumMember(Value = "CUSTOM_VERIFIER")]
CUSTOM_VERIFIER
}

ExtraLoginOptions

The LoginParams class accepts the ExtraLoginOptions as an optional input, containing advanced options for custom authentication, email passwordless login, etc. This parameter can be considered as advanced login options needed for specific cases.

LoginParams() { selectedLoginProvider, extraLoginOptions = ExtraLoginOptions() }
ParameterDescription
additionalParams?Additional params in Dictionary<string, string> format for OAuth login, use id_token(JWT) to authenticate with web3auth.
domain?Your custom authentication domain in string format. For example, if you are using Auth0, this could be example.au.auth0.com.
client_id?Client id in string format, provided by your login provider used for custom verifier.
leeway?The value used to account for clock skew in JWT expirations. The value is in the seconds, and ideally should be no more than 60 seconds, with a maxium of 120 seconds. It takes string as a value.
verifierIdField?The field in the JWT token that maps to verifier id. Please make sure you have selected the correct JWT verifier id in the Developer Dashboard. It takes string as a value.
isVerifierIdCaseSensitive?bool to confirm whether the verifier id field is case sensitive or not.
display?Allows developers to configure the display of UI. It takes Display as a value.
prompt?Prompt shown to the user during the authentication process. It takes Prompt as a value.
max_age?Maxium time allowed without reauthentication. If the last time the user authenticated is greater than this value, then the user must reauthenticate. It takes string as a value.
ui_locales?The space separated list of language tags, ordered by preference. For example fr-CA fr en.
id_token_hint?It specify the previously issued ID token. It takes string as a value.
id_token?JWT (ID Token) to be passed for login.
login_hint?It is used to send the user's email address during Email Passwordless login. It takes string as a value.
acr_values?acr_values
scope?The default scope to be used on authentication requests. The defaultScope defined in the Auth0Client is included along with this scope. It takes string as a value.
audience?The audience, presented as the aud claim in the access token, defines the intended consumer of the token. It takes string as a value.
connection?The name of the connection configured for your application. If null, it will redirect to the Auth0 Login Page and show the Login Widget. It takes string as a value.
state?state
response_type?Defines which grant to be execute for the authorization server. It takes string as a value.
nonce?nonce value
redirect_uri?It can be used to specify the default URL, where your custom JWT verifier can redirect your browser to with the result. If you are using Auth0, it must be whitelisted in the Allowed Callback URLs in your Auth0's application.

Curve

The LoginParams class accepts a curve parameter. This parameter can be used to select the elliptic curve to use for the signature.

LoginParams() { selectedLoginProvider, curve = Curve.SECP256K1 }
public void login()
{
var selectedProvider = Provider.GOOGLE;
var options = new LoginParams()
{
loginProvider = selectedProvider,
curve = Curve.SECP256K1
};
web3Auth.login(options);
}

Sample Response

{
"ed25519PrivKey": "666523652352635....",
"privKey": "0ajjsdsd....",
"coreKitKey": "0ajjsdsd....",
"coreKitEd25519PrivKey": "666523652352635....",
"sessionId": "....",
"error": "....",
"userInfo": {
"aggregateVerifier": "w3a-google",
"email": "john@gmail.com",
"name": "John Dash",
"profileImage": "https://lh3.googleusercontent.com/a/Ajjjsdsmdjmnm...",
"typeOfLogin": "google",
"verifier": "torus",
"verifierId": "john@gmail.com",
"dappShare": "<24 words seed phrase>", // will be sent only incase of custom verifiers
"idToken": "<jwtToken issued by Web3Auth>",
"oAuthIdToken": "<jwtToken issued by OAuth Provider>", // will be sent only incase of custom verifiers
"oAuthAccessToken": "<accessToken issued by OAuth Provider>", // will be sent only incase of custom verifiers
"isMfaEnabled": true // returns true if user has enabled mfa
}
}

Example

public void login()
{
var selectedProvider = Provider.GOOGLE;
var options = new LoginParams()
{
loginProvider = selectedProvider
};
web3Auth.login(options);
}

Logging out a user

web3Auth.logout()

Trigger logout flow. This function doesn't take parameters. A completable future containing a void object will be returned on successfull logout otherwise an error response is returned.

tip

Additionally you can associate a function to be triggered on successful logout.

void Start()
{
web3Auth = GetComponent<Web3Auth>();
web3Auth.setOptions(new Web3AuthOptions()
{

});
web3Auth.onLogout += onLogout;
}
private void onLogout()
{
// Functions to be called after user logs out
}

Example

public void logout()
{
web3Auth.logout();
Debug.Log("Logged out!");
}

Working Example

/Assets/Web3Auth.cs
using System;
using System.Linq;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Newtonsoft.Json;

public class Web3custom : MonoBehaviour
{
Web3Auth web3Auth;

// Start is called before the first frame update
void Start()
{
web3Auth = GetComponent<Web3Auth>();
web3Auth.setOptions(new Web3AuthOptions()
{
redirectUrl = new Uri("torusapp://com.torus.Web3AuthUnity/auth"),
clientId = "BAwFgL-r7wzQKmtcdiz2uHJKNZdK7gzEf2q-m55xfzSZOw8jLOyIi4AVvvzaEQO5nv2dFLEmf9LBkF8kaq3aErg",
network = Web3Auth.Network.TESTNET,
});
web3Auth.onLogin += onLogin;
web3Auth.onLogout += onLogout;
}

public void login()
{
var selectedProvider = Provider.GOOGLE;
var options = new LoginParams()
{
loginProvider = selectedProvider
};
web3Auth.login(options);
}

private void onLogin(Web3AuthResponse response)
{
var userInfo = JsonConvert.SerializeObject(response.userInfo, Formatting.Indented);
Debug.Log(userInfo);
}

public void logout()
{
web3Auth.logout();
}

private void onLogout()
{
Debug.Log("Logged out!");
}
}