From f0aa0c5540ed64efab6ea8977d5a275fc2f6f06f Mon Sep 17 00:00:00 2001
From: RossAscends <124905043+RossAscends@users.noreply.github.com>
Date: Mon, 8 Apr 2024 02:22:44 +0900
Subject: [PATCH] imp user creation, split out from loader.js (still disabled)
---
public/scripts/loader.js | 71 ++-------------
public/scripts/userManagement.js | 152 +++++++++++++++++++++++++++++++
2 files changed, 161 insertions(+), 62 deletions(-)
create mode 100644 public/scripts/userManagement.js
diff --git a/public/scripts/loader.js b/public/scripts/loader.js
index 432484096..179a0437b 100644
--- a/public/scripts/loader.js
+++ b/public/scripts/loader.js
@@ -1,6 +1,6 @@
const ELEMENT_ID = 'loader';
-import { delay } from "./utils.js";
+import { populateUserList } from './userManagement.js'
export function showLoader() {
const container = $('
').attr('id', ELEMENT_ID);
@@ -9,55 +9,23 @@ export function showLoader() {
$('body').append(container);
}
-
-//placeholder user data
-const user1 =
-{
- handle: 'user0',
- avatarSrc: 'https://cdn-icons-png.flaticon.com/256/147/147144.png',
- name: 'Admin',
- password: true
-}
-
-const user2 =
-{
- handle: 'user1',
- avatarSrc: 'https://cdn.iconscout.com/icon/free/png-256/free-avatar-370-456322.png',
- name: 'Guest',
- password: true
-}
-
-const userSelectMessage = `
-
-
Select User
-
This is merely a test.
Click a user, and then click Login to proceed.
-
-
-
-
-
-
-
- `
-
-
export async function hideLoader() {
+
//Sets up a 2-step animation. Spinner blurs/fades out, and then the loader shadow does the same.
$('#load-spinner').on('transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd', function () {
+ //uncomment this as part of user selection enabling
//$('#loader-spinner')
+
+ //comment this instead
$(`#${ELEMENT_ID}`)
//only fade out the spinner and replace with login screen
.animate({ opacity: 0 }, 300, function () {
- //dont remove the loader container just yet
+ //when enabling user select, dont remove the loader container just yet
+ //comment this out
$(`#${ELEMENT_ID}`).remove();
});
});
-
//console.log('BLURRING SPINNER')
$('#load-spinner')
.css({
@@ -65,29 +33,8 @@ export async function hideLoader() {
'opacity': '0',
});
- //add login screen
- //$('#loader').append(userSelectMessage)
-
- $(".userSelect").on("click", function () {
- let selectedUserName = $(this).data('foruser')
- $('.userSelect').removeClass('avatar-container selected')
- $(this).addClass('avatar-container selected')
- console.log(selectedUserName)
- $("#passwordHeaderText").text(`Enter password for ${selectedUserName}`)
- $("#passwordEntryBlock").show()
- })
-
- $("#loginButton").on('click', function () {
- $('#loader')
- .animate({ opacity: 0 }, 300, function () {
-
- //insert user handle/password verification code here
-
- //.finally:
- $('#loader').remove();
- });
- })
-
+ //uncomment to make user selection live
+ //await populateUserList()
}
diff --git a/public/scripts/userManagement.js b/public/scripts/userManagement.js
new file mode 100644
index 000000000..d54869628
--- /dev/null
+++ b/public/scripts/userManagement.js
@@ -0,0 +1,152 @@
+async function getUserList() {
+ const response = await fetch('/api/users/list');
+ const userListObj = await response.json(); // Assuming the response is in JSON format
+ console.log(userListObj)
+ return userListObj;
+}
+
+async function registerNewUser() {
+ let handle = String($("#newUserHandle").val());
+ let name = String($("#newUserName").val());
+ let password = String($("#newUserPassword").val());
+ let passwordConfirm = String($("#newUserPasswordConfirm").val());
+
+ if (handle.length < 4) {
+ alert('Username must be at least 4 characters long');
+ return;
+ }
+
+ if (password.length < 8) {
+ alert('Password must be at least 8 characters long');
+ return;
+ }
+
+ if (password !== passwordConfirm) {
+ alert("Passwords don't match!")
+ return
+ }
+
+ const newUser = {
+ handle: handle,
+ name: name || 'Anonymous',
+ password: password,
+ };
+
+ try {
+ const response = await $.ajax({
+ url: '/api/users/create',
+ type: 'POST',
+ contentType: 'application/json',
+ data: JSON.stringify(newUser),
+ });
+
+ console.log(response);
+ if (response.handle) {
+ console.log('saw user created successfully')
+ alert('New user created!')
+ $("#userSelectBlock").empty()
+ populateUserList()
+ $("#userListBlock").show()
+ $("#registerNewUserBlock").hide()
+ $("#registerNewUserBlock input").val('')
+
+ }
+ } catch (error) {
+ console.error('Error creating new user:', error);
+ alert(error.responseText)
+ }
+}
+
+export async function populateUserList() {
+ const userList = await getUserList();
+
+ const registerNewUserButtonHTML = ``
+
+ const newUserRegisterationHTML = `
+
+ Register New SillyTavern User
+
Username:
+
Password:
+
Password confirm:
+ This will create a new subfolder in the /data/ directory.
+
+
+
+
+
+ `
+
+ const userSelectHTML = `
+
+
Select User
+
This is merely a test.
Click a user, and then click Login to proceed.
+
+
+
+ ${newUserRegisterationHTML}
+
+
+ `;
+
+ // Add login screen
+ $('#loader').append(userSelectHTML);
+
+ const parentDiv = $('#userList');
+
+ userList.forEach(user => {
+ const userDiv = $('')
+ .attr('id', `userSelect-${user.handle}`)
+ .attr('data-foruser', user.name)
+ .addClass('userSelect menu_button flex-container flexFlowCol');
+
+ const avatarImg = $('
')
+ .addClass('avatar')
+ .attr('src', user.avatar);
+
+ userDiv.append(avatarImg);
+
+ const userName = $('').text(user.name);
+ userDiv.append(userName);
+
+ parentDiv.append(userDiv);
+ });
+
+ parentDiv.append(registerNewUserButtonHTML)
+
+ $(".userSelect").off('click').on("click", function () {
+ let selectedUserName = $(this).data('foruser')
+ $('.userSelect').removeClass('avatar-container selected')
+ $(this).addClass('avatar-container selected')
+ console.log(selectedUserName)
+ $("#passwordHeaderText").text(`Enter password for ${selectedUserName}`)
+ $("#passwordEntryBlock").show()
+ });
+
+ $("#registerNewUserButton").off('click').on('click', function () {
+ $("#userListBlock").hide()
+ $("#registerNewUserBlock").show()
+ })
+
+ $("#newUserRegisterFinalizeButton").off('click').on('click', registerNewUser)
+
+ $("#newUserRegisterCancelButton").off('click').on('click', function () {
+ $("#userListBlock").show()
+ $("#registerNewUserBlock").hide()
+ })
+
+ $("#loginButton").off('click').on('click', function () {
+ $('#loader')
+ .animate({ opacity: 0 }, 300, function () {
+ // Insert user handle/password verification code here
+ //.finally:
+ $('#loader').remove();
+ });
+ });
+}