Skip to content

[DX-2505] feat: login functions #138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
359 changes: 338 additions & 21 deletions sample/Assets/Scenes/AuthenticatedScene.unity

Large diffs are not rendered by default.

992 changes: 712 additions & 280 deletions sample/Assets/Scenes/UnauthenticatedScene.unity

Large diffs are not rendered by default.

49 changes: 47 additions & 2 deletions sample/Assets/Scripts/AuthenticatedScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ public class AuthenticatedScript : MonoBehaviour
[SerializeField] private Canvas AuthenticatedCanvas;
[SerializeField] private Button AccessTokenButton;
[SerializeField] private Button IdTokenButton;
[SerializeField] private Button LogoutButton;

// IMX
[SerializeField] private Button ConnectButton;
[SerializeField] private Button IsRegisteredOffchainButton;
[SerializeField] private Button RegisterOffchainButton;
[SerializeField] private Button GetAddressButton;
[SerializeField] private Button LogoutButton;
[SerializeField] private Button ShowTransferButton;

[SerializeField] private Canvas TransferCanvas;
Expand Down Expand Up @@ -62,13 +65,47 @@ void Start()
if (Passport.Instance != null)
{
passport = Passport.Instance;
ConnectButton.gameObject.SetActive(!SampleAppManager.IsConnected);
IsRegisteredOffchainButton.gameObject.SetActive(SampleAppManager.IsConnected);
RegisterOffchainButton.gameObject.SetActive(SampleAppManager.IsConnected);
GetAddressButton.gameObject.SetActive(SampleAppManager.IsConnected);
ShowTransferButton.gameObject.SetActive(SampleAppManager.IsConnected);
}
else
{
ShowOutput("Passport Instance is null");
}
}

public async void Connect()
{
try
{
// Use existing credentials to connect to Passport
ShowOutput("Connecting into Passport using saved credentials...");
ConnectButton.gameObject.SetActive(false);
bool connected = await passport.ConnectImx(useCachedSession: true);
if (connected)
{
IsRegisteredOffchainButton.gameObject.SetActive(true);
RegisterOffchainButton.gameObject.SetActive(true);
GetAddressButton.gameObject.SetActive(true);
ShowTransferButton.gameObject.SetActive(true);
ShowOutput($"Connected");
}
else
{
ShowOutput($"Could not connect using saved credentials");
ConnectButton.gameObject.SetActive(true);
}
}
catch (Exception ex)
{
ShowOutput($"Connect() error: {ex.Message}");
ConnectButton.gameObject.SetActive(true);
}
}

public async void IsRegisteredOffchain()
{
ShowOutput($"Called IsRegisteredOffchain()...");
Expand All @@ -93,7 +130,14 @@ public async void RegisterOffchain()
try
{
RegisterUserResponse response = await passport.RegisterOffchain();
ShowOutput($"Registered {response.tx_hash}");
if (response != null)
{
ShowOutput($"Registered {response.tx_hash}");
}
else
{
ShowOutput($"Not registered");
}
}
catch (PassportException e)
{
Expand Down Expand Up @@ -130,6 +174,7 @@ public async void Logout()
#else
await passport.Logout();
#endif
SampleAppManager.IsConnected = false;
SceneManager.LoadScene(sceneName: "UnauthenticatedScene");
}

Expand Down
4 changes: 4 additions & 0 deletions sample/Assets/Scripts/SampleAppManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public static class SampleAppManager
{
public static bool IsConnected { get; set; }
}
11 changes: 11 additions & 0 deletions sample/Assets/Scripts/SampleAppManager.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

137 changes: 107 additions & 30 deletions sample/Assets/Scripts/UnauthenticatedScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ public class UnauthenticatedScript : MonoBehaviour
{
#pragma warning disable CS8618
[SerializeField] private Text Output;

[SerializeField] private Button LoginButton;
[SerializeField] private Button ConnectButton;
[SerializeField] private Button TryAgainButton;
[SerializeField] private Button ReloginButton;
[SerializeField] private Button ReconnectButton;

private Passport passport;
#pragma warning restore CS8618
Expand All @@ -22,8 +23,10 @@ async void Start()
try
{
ShowOutput("Starting...");
LoginButton.gameObject.SetActive(false);
ConnectButton.gameObject.SetActive(false);
TryAgainButton.gameObject.SetActive(false);
ReloginButton.gameObject.SetActive(false);
ReconnectButton.gameObject.SetActive(false);

string clientId = "ZJL7JvetcDFBNDlgRs5oJoxuAUUl6uQj";
string environment = Immutable.Passport.Model.Environment.SANDBOX;
Expand All @@ -46,42 +49,88 @@ async void Start()

// Check if user's logged in before
bool hasCredsSaved = await passport.HasCredentialsSaved();
if (hasCredsSaved)
Debug.Log(hasCredsSaved ? "Has credentials saved" : "Does not have credentials saved");
ReloginButton.gameObject.SetActive(hasCredsSaved);
ReconnectButton.gameObject.SetActive(hasCredsSaved);
LoginButton.gameObject.SetActive(!hasCredsSaved);
ConnectButton.gameObject.SetActive(!hasCredsSaved);

ShowOutput("Ready");
}
catch (Exception ex)
{
ShowOutput($"Start() error: {ex.Message}");
}
}

public async void Login()
{
try
{
ShowOutput("Called Login()...");
LoginButton.gameObject.SetActive(false);

// macOS editor (play scene) does not support deeplinking
#if UNITY_ANDROID || UNITY_IPHONE || (UNITY_STANDALONE_OSX && !UNITY_EDITOR_OSX)
await passport.LoginPKCE();
#else
await passport.Login();
#endif

SampleAppManager.IsConnected = false;
NavigateToAuthenticatedScene();
}
catch (Exception ex)
{
string error;
if (ex is PassportException passportException && passportException.IsNetworkError())
{
error = $"Login() error: Check your internet connection and try again";
}
else if (ex is OperationCanceledException)
{
// Use existing credentials to connect to Passport
ShowOutput("Connecting to Passport using saved credentials...");

bool connected = await passport.ConnectImxSilent();
if (connected)
{
// Successfully connected to Passport
NavigateToAuthenticatedScene();
}
else
{
// Could not connect to Passport, enable connect button
ConnectButton.gameObject.SetActive(true);
ShowOutput("Failed to connect using saved credentials");
}
error = "Login() cancelled";
}
else
{
// No existing credentials to use to connect
ShowOutput("Ready");
// Enable connect button
ConnectButton.gameObject.SetActive(true);
error = $"Login() error: {ex.Message}";
// Restart everything
await passport.Logout();
}
}
catch (Exception ex)
{
ShowOutput($"Start() error: {ex.Message}");
TryAgainButton.gameObject.SetActive(true);

Debug.Log(error);
ShowOutput(error);
#if UNITY_ANDROID || UNITY_IPHONE || UNITY_STANDALONE_OSX
LoginButton.gameObject.SetActive(true);
#endif
}
}

public void OnTryAgain()
public async void Relogin()
{
Start();
try
{
// Use existing credentials to log in to Passport
ShowOutput("Logging into Passport using saved credentials...");
ReloginButton.gameObject.SetActive(false);
bool loggedIn = await passport.Login(useCachedSession: true);
if (loggedIn)
{
SampleAppManager.IsConnected = false;
NavigateToAuthenticatedScene();
}
else
{
ShowOutput($"Could not login using saved credentials");
ClearStorageAndCache();

}
}
catch (Exception ex)
{
ShowOutput($"Relogin() error: {ex.Message}");
ClearStorageAndCache();
}
}

public async void Connect()
Expand All @@ -98,6 +147,7 @@ public async void Connect()
await passport.ConnectImx();
#endif

SampleAppManager.IsConnected = true;
NavigateToAuthenticatedScene();
}
catch (Exception ex)
Expand Down Expand Up @@ -126,6 +176,33 @@ public async void Connect()
}
}

public async void Reconnect()
{
try
{
// Use existing credentials to connect to Passport
ShowOutput("Reconnecting into Passport using saved credentials...");
ReconnectButton.gameObject.SetActive(false);
bool connected = await passport.ConnectImx(useCachedSession: true);
if (connected)
{
SampleAppManager.IsConnected = true;
NavigateToAuthenticatedScene();
}
else
{
ShowOutput($"Could not connect using saved credentials");
ClearStorageAndCache();

}
}
catch (Exception ex)
{
ShowOutput($"Reconnect() error: {ex.Message}");
ClearStorageAndCache();
}
}

public void ClearStorageAndCache()
{
#if (UNITY_IPHONE && !UNITY_EDITOR) || (UNITY_ANDROID && !UNITY_EDITOR)
Expand Down
4 changes: 2 additions & 2 deletions src/Packages/Passport/Editor/PassportPostprocess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,6 @@ private void CopyFilesTo(string destinationPath)
}
}
}
}
}

#endif
14 changes: 7 additions & 7 deletions src/Packages/Passport/Runtime/Resources/index.html

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ namespace Immutable.Passport
public static class PassportFunction
{
public const string INIT = "init";
public const string INIT_DEVICE_FLOW = "initDeviceFlow";
public const string RELOGIN = "relogin";
public const string RECONNECT = "reconnect";
public const string CONNECT = "connect";
public const string LOGIN_PKCE = "loginPKCE";
public const string CONNECT_PKCE = "connectPKCE";
public const string GET_PKCE_AUTH_URL = "getPKCEAuthUrl";
public const string CONFIRM_CODE = "confirmCode";
public const string RECONNECT = "reconnect";
public const string LOGIN_CONFIRM_CODE = "loginConfirmCode";
public const string CONNECT_CONFIRM_CODE = "connectConfirmCode";
public const string GET_ACCESS_TOKEN = "getAccessToken";
public const string GET_ID_TOKEN = "getIdToken";
public const string GET_ADDRESS = "getAddress";
Expand Down
Loading