Skip to content

Commit 3def1d4

Browse files
committed
feat: implement authentication UI in Puter.js's test UI
- Added login and logout functionality with corresponding UI elements. - Enhanced layout for user information display and login button. - Integrated authentication state checks to update UI based on user sign-in status. - Improved button styles and interactions for better user experience.
1 parent 9c74e29 commit 3def1d4

File tree

1 file changed

+107
-2
lines changed

1 file changed

+107
-2
lines changed

src/puter-js/test/run.html

Lines changed: 107 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
margin-left: 20px;
4242
}
4343
#settings-btn {
44-
margin-left: 10px;
44+
margin-left: auto;
4545
margin-top: 20px;
4646
margin-bottom: 20px;
4747
background-color: #666;
@@ -59,6 +59,39 @@
5959
#settings-btn:hover {
6060
background-color: #555;
6161
}
62+
#auth-section {
63+
float: right;
64+
margin-right: 20px;
65+
display: flex;
66+
align-items: center;
67+
font-size: 14px;
68+
}
69+
#login-btn {
70+
background-color: #4c84af;
71+
border: none;
72+
color: white;
73+
padding: 12px 16px;
74+
text-align: center;
75+
text-decoration: none;
76+
display: inline-block;
77+
font-size: 14px;
78+
cursor: pointer;
79+
}
80+
#login-btn:hover {
81+
background-color: #3a6a8a;
82+
}
83+
#user-info {
84+
color: #333;
85+
}
86+
#logout-link {
87+
color: #666;
88+
text-decoration: underline;
89+
cursor: pointer;
90+
margin-left: 5px;
91+
}
92+
#logout-link:hover {
93+
color: #333;
94+
}
6295
#unselect-all {
6396
margin-left: 20px;
6497
cursor: pointer;
@@ -389,6 +422,40 @@
389422

390423
let currentSettings = { ...DEFAULT_SETTINGS };
391424

425+
// Authentication functionality
426+
async function updateAuthUI() {
427+
try {
428+
if (typeof puter === 'undefined' || !puter.auth) {
429+
// Puter not loaded yet, show login button
430+
$('#login-btn').show();
431+
$('#user-info').hide();
432+
return;
433+
}
434+
435+
const isSignedIn = await puter.auth.isSignedIn();
436+
437+
if (isSignedIn) {
438+
let user = await puter.auth.getUser();
439+
// User is signed in, show username and logout option
440+
$('#login-btn').hide();
441+
$('#user-info').show();
442+
443+
// Get user info - for now we'll use a placeholder
444+
// In a real app, you might want to get the actual username from puter
445+
$('#username').text(user.username); // You may need to get actual username from puter API
446+
} else {
447+
// User is not signed in, show login button
448+
$('#login-btn').show();
449+
$('#user-info').hide();
450+
}
451+
} catch (error) {
452+
console.error('Error checking auth state:', error);
453+
// If there's an error, default to showing login button
454+
$('#login-btn').show();
455+
$('#user-info').hide();
456+
}
457+
}
458+
392459
// Load settings from localStorage or use defaults
393460
function loadSettings() {
394461
const saved = localStorage.getItem('puter-test-settings');
@@ -439,6 +506,9 @@
439506
if (typeof puter !== 'undefined' && puter.setAPIOrigin) {
440507
puter.setAPIOrigin(currentSettings.apiOrigin);
441508
}
509+
510+
// Update auth UI after puter is loaded
511+
await updateAuthUI();
442512
} catch (error) {
443513
console.error('Failed to load Puter.js:', error);
444514
alert('Failed to load Puter.js. Please check the URL in settings.');
@@ -961,6 +1031,34 @@
9611031
// Initialize the counter display
9621032
updateMasterCheckboxState();
9631033

1034+
// Login functionality
1035+
$('#login-btn').click(async () => {
1036+
try {
1037+
$('#login-btn').prop('disabled', true);
1038+
await puter.auth.signIn();
1039+
await updateAuthUI();
1040+
} catch (error) {
1041+
console.error('Login error:', error);
1042+
alert('Login failed. Please try again.');
1043+
} finally {
1044+
$('#login-btn').prop('disabled', false);
1045+
}
1046+
});
1047+
1048+
// Logout functionality
1049+
$('#logout-link').click(async () => {
1050+
try {
1051+
await puter.auth.signOut();
1052+
await updateAuthUI();
1053+
} catch (error) {
1054+
console.error('Logout error:', error);
1055+
alert('Logout failed. Please try again.');
1056+
}
1057+
});
1058+
1059+
// Initialize auth UI after puter is loaded
1060+
updateAuthUI();
1061+
9641062
// Settings modal functionality
9651063
$('#settings-btn').click(() => {
9661064
// Populate modal with current settings
@@ -1014,10 +1112,17 @@
10141112
<input type="checkbox" id="master-checkbox" checked style="margin-right: 5px; transform: scale(1.2);">
10151113
</label>
10161114
<span id="test-counter"></span>
1017-
<div style="flex: 1;">
1115+
<div style="flex: 1; display: flex; align-items: center;">
10181116
<button id="run-tests" style="margin-right: 10px;">Run Tests</button>
10191117
<span id="reset-results" style="margin-right: 20px; cursor: pointer; color: #666; text-decoration: underline; font-size: 14px;">Reset results</span>
10201118
<button id="settings-btn">Settings</button>
1119+
<div id="auth-section">
1120+
<button id="login-btn" style="display: none;">Login</button>
1121+
<div id="user-info" style="display: none;">
1122+
<span id="username"></span>
1123+
<span id="logout-link">(logout)</span>
1124+
</div>
1125+
</div>
10211126
</div>
10221127
</nav>
10231128

0 commit comments

Comments
 (0)