Skip to content

Commit e025bdc

Browse files
committed
feat: login functions
1 parent f817191 commit e025bdc

File tree

11 files changed

+1382
-461
lines changed

11 files changed

+1382
-461
lines changed

sample/Assets/Scenes/AuthenticatedScene.unity

Lines changed: 338 additions & 21 deletions
Large diffs are not rendered by default.

sample/Assets/Scenes/UnauthenticatedScene.unity

Lines changed: 712 additions & 280 deletions
Large diffs are not rendered by default.

sample/Assets/Scripts/AuthenticatedScript.cs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@ public class AuthenticatedScript : MonoBehaviour
1515
[SerializeField] private Canvas AuthenticatedCanvas;
1616
[SerializeField] private Button AccessTokenButton;
1717
[SerializeField] private Button IdTokenButton;
18+
[SerializeField] private Button LogoutButton;
19+
20+
// IMX
21+
[SerializeField] private Button ConnectButton;
1822
[SerializeField] private Button IsRegisteredOffchainButton;
1923
[SerializeField] private Button RegisterOffchainButton;
2024
[SerializeField] private Button GetAddressButton;
21-
[SerializeField] private Button LogoutButton;
2225
[SerializeField] private Button ShowTransferButton;
2326

2427
[SerializeField] private Canvas TransferCanvas;
@@ -62,13 +65,47 @@ void Start()
6265
if (Passport.Instance != null)
6366
{
6467
passport = Passport.Instance;
68+
ConnectButton.gameObject.SetActive(!SampleAppManager.IsConnected);
69+
IsRegisteredOffchainButton.gameObject.SetActive(SampleAppManager.IsConnected);
70+
RegisterOffchainButton.gameObject.SetActive(SampleAppManager.IsConnected);
71+
GetAddressButton.gameObject.SetActive(SampleAppManager.IsConnected);
72+
ShowTransferButton.gameObject.SetActive(SampleAppManager.IsConnected);
6573
}
6674
else
6775
{
6876
ShowOutput("Passport Instance is null");
6977
}
7078
}
7179

80+
public async void Connect()
81+
{
82+
try
83+
{
84+
// Use existing credentials to connect to Passport
85+
ShowOutput("Connecting into Passport using saved credentials...");
86+
ConnectButton.gameObject.SetActive(false);
87+
bool connected = await passport.ConnectImx(useCachedSession: true);
88+
if (connected)
89+
{
90+
IsRegisteredOffchainButton.gameObject.SetActive(true);
91+
RegisterOffchainButton.gameObject.SetActive(true);
92+
GetAddressButton.gameObject.SetActive(true);
93+
ShowTransferButton.gameObject.SetActive(true);
94+
ShowOutput($"Connected");
95+
}
96+
else
97+
{
98+
ShowOutput($"Could not connect using saved credentials");
99+
ConnectButton.gameObject.SetActive(true);
100+
}
101+
}
102+
catch (Exception ex)
103+
{
104+
ShowOutput($"Connect() error: {ex.Message}");
105+
ConnectButton.gameObject.SetActive(true);
106+
}
107+
}
108+
72109
public async void IsRegisteredOffchain()
73110
{
74111
ShowOutput($"Called IsRegisteredOffchain()...");
@@ -93,7 +130,14 @@ public async void RegisterOffchain()
93130
try
94131
{
95132
RegisterUserResponse response = await passport.RegisterOffchain();
96-
ShowOutput($"Registered {response.tx_hash}");
133+
if (response != null)
134+
{
135+
ShowOutput($"Registered {response.tx_hash}");
136+
}
137+
else
138+
{
139+
ShowOutput($"Not registered");
140+
}
97141
}
98142
catch (PassportException e)
99143
{
@@ -130,6 +174,7 @@ public async void Logout()
130174
#else
131175
await passport.Logout();
132176
#endif
177+
SampleAppManager.IsConnected = false;
133178
SceneManager.LoadScene(sceneName: "UnauthenticatedScene");
134179
}
135180

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public static class SampleAppManager
2+
{
3+
public static bool IsConnected { get; set; }
4+
}

sample/Assets/Scripts/SampleAppManager.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sample/Assets/Scripts/UnauthenticatedScript.cs

Lines changed: 107 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ public class UnauthenticatedScript : MonoBehaviour
1010
{
1111
#pragma warning disable CS8618
1212
[SerializeField] private Text Output;
13-
13+
[SerializeField] private Button LoginButton;
1414
[SerializeField] private Button ConnectButton;
15-
[SerializeField] private Button TryAgainButton;
15+
[SerializeField] private Button ReloginButton;
16+
[SerializeField] private Button ReconnectButton;
1617

1718
private Passport passport;
1819
#pragma warning restore CS8618
@@ -22,8 +23,10 @@ async void Start()
2223
try
2324
{
2425
ShowOutput("Starting...");
26+
LoginButton.gameObject.SetActive(false);
2527
ConnectButton.gameObject.SetActive(false);
26-
TryAgainButton.gameObject.SetActive(false);
28+
ReloginButton.gameObject.SetActive(false);
29+
ReconnectButton.gameObject.SetActive(false);
2730

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

4750
// Check if user's logged in before
4851
bool hasCredsSaved = await passport.HasCredentialsSaved();
49-
if (hasCredsSaved)
52+
Debug.Log(hasCredsSaved ? "Has credentials saved" : "Does not have credentials saved");
53+
ReloginButton.gameObject.SetActive(hasCredsSaved);
54+
ReconnectButton.gameObject.SetActive(hasCredsSaved);
55+
LoginButton.gameObject.SetActive(!hasCredsSaved);
56+
ConnectButton.gameObject.SetActive(!hasCredsSaved);
57+
58+
ShowOutput("Ready");
59+
}
60+
catch (Exception ex)
61+
{
62+
ShowOutput($"Start() error: {ex.Message}");
63+
}
64+
}
65+
66+
public async void Login()
67+
{
68+
try
69+
{
70+
ShowOutput("Called Login()...");
71+
LoginButton.gameObject.SetActive(false);
72+
73+
// macOS editor (play scene) does not support deeplinking
74+
#if UNITY_ANDROID || UNITY_IPHONE || (UNITY_STANDALONE_OSX && !UNITY_EDITOR_OSX)
75+
await passport.LoginPKCE();
76+
#else
77+
await passport.Login();
78+
#endif
79+
80+
SampleAppManager.IsConnected = false;
81+
NavigateToAuthenticatedScene();
82+
}
83+
catch (Exception ex)
84+
{
85+
string error;
86+
if (ex is PassportException passportException && passportException.IsNetworkError())
87+
{
88+
error = $"Login() error: Check your internet connection and try again";
89+
}
90+
else if (ex is OperationCanceledException)
5091
{
51-
// Use existing credentials to connect to Passport
52-
ShowOutput("Connecting to Passport using saved credentials...");
53-
54-
bool connected = await passport.ConnectImxSilent();
55-
if (connected)
56-
{
57-
// Successfully connected to Passport
58-
NavigateToAuthenticatedScene();
59-
}
60-
else
61-
{
62-
// Could not connect to Passport, enable connect button
63-
ConnectButton.gameObject.SetActive(true);
64-
ShowOutput("Failed to connect using saved credentials");
65-
}
92+
error = "Login() cancelled";
6693
}
6794
else
6895
{
69-
// No existing credentials to use to connect
70-
ShowOutput("Ready");
71-
// Enable connect button
72-
ConnectButton.gameObject.SetActive(true);
96+
error = $"Login() error: {ex.Message}";
97+
// Restart everything
98+
await passport.Logout();
7399
}
74-
}
75-
catch (Exception ex)
76-
{
77-
ShowOutput($"Start() error: {ex.Message}");
78-
TryAgainButton.gameObject.SetActive(true);
100+
101+
Debug.Log(error);
102+
ShowOutput(error);
103+
#if UNITY_ANDROID || UNITY_IPHONE || UNITY_STANDALONE_OSX
104+
LoginButton.gameObject.SetActive(true);
105+
#endif
79106
}
80107
}
81108

82-
public void OnTryAgain()
109+
public async void Relogin()
83110
{
84-
Start();
111+
try
112+
{
113+
// Use existing credentials to log in to Passport
114+
ShowOutput("Logging into Passport using saved credentials...");
115+
ReloginButton.gameObject.SetActive(false);
116+
bool loggedIn = await passport.Login(useCachedSession: true);
117+
if (loggedIn)
118+
{
119+
SampleAppManager.IsConnected = false;
120+
NavigateToAuthenticatedScene();
121+
}
122+
else
123+
{
124+
ShowOutput($"Could not login using saved credentials");
125+
ClearStorageAndCache();
126+
127+
}
128+
}
129+
catch (Exception ex)
130+
{
131+
ShowOutput($"Relogin() error: {ex.Message}");
132+
ClearStorageAndCache();
133+
}
85134
}
86135

87136
public async void Connect()
@@ -98,6 +147,7 @@ public async void Connect()
98147
await passport.ConnectImx();
99148
#endif
100149

150+
SampleAppManager.IsConnected = true;
101151
NavigateToAuthenticatedScene();
102152
}
103153
catch (Exception ex)
@@ -126,6 +176,33 @@ public async void Connect()
126176
}
127177
}
128178

179+
public async void Reconnect()
180+
{
181+
try
182+
{
183+
// Use existing credentials to connect to Passport
184+
ShowOutput("Reconnecting into Passport using saved credentials...");
185+
ReconnectButton.gameObject.SetActive(false);
186+
bool connected = await passport.ConnectImx(useCachedSession: true);
187+
if (connected)
188+
{
189+
SampleAppManager.IsConnected = true;
190+
NavigateToAuthenticatedScene();
191+
}
192+
else
193+
{
194+
ShowOutput($"Could not connect using saved credentials");
195+
ClearStorageAndCache();
196+
197+
}
198+
}
199+
catch (Exception ex)
200+
{
201+
ShowOutput($"Reconnect() error: {ex.Message}");
202+
ClearStorageAndCache();
203+
}
204+
}
205+
129206
public void ClearStorageAndCache()
130207
{
131208
#if (UNITY_IPHONE && !UNITY_EDITOR) || (UNITY_ANDROID && !UNITY_EDITOR)

src/Packages/Passport/Editor/PassportPostprocess.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,6 @@ private void CopyFilesTo(string destinationPath)
178178
}
179179
}
180180
}
181-
}
182-
181+
}
182+
183183
#endif

src/Packages/Passport/Runtime/Resources/index.html

Lines changed: 7 additions & 7 deletions
Large diffs are not rendered by default.

src/Packages/Passport/Runtime/Scripts/Private/PassportFunction.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@ namespace Immutable.Passport
33
public static class PassportFunction
44
{
55
public const string INIT = "init";
6+
public const string INIT_DEVICE_FLOW = "initDeviceFlow";
7+
public const string RELOGIN = "relogin";
8+
public const string RECONNECT = "reconnect";
69
public const string CONNECT = "connect";
10+
public const string LOGIN_PKCE = "loginPKCE";
711
public const string CONNECT_PKCE = "connectPKCE";
812
public const string GET_PKCE_AUTH_URL = "getPKCEAuthUrl";
9-
public const string CONFIRM_CODE = "confirmCode";
10-
public const string RECONNECT = "reconnect";
13+
public const string LOGIN_CONFIRM_CODE = "loginConfirmCode";
14+
public const string CONNECT_CONFIRM_CODE = "connectConfirmCode";
1115
public const string GET_ACCESS_TOKEN = "getAccessToken";
1216
public const string GET_ID_TOKEN = "getIdToken";
1317
public const string GET_ADDRESS = "getAddress";

0 commit comments

Comments
 (0)