Initial work towards authentication

This commit is contained in:
2026-10-17 19:37:31 -05:00
parent ec3b1fa068
commit cb80364e68
5 changed files with 122 additions and 30 deletions

View File

@@ -10,13 +10,64 @@ module W3DHubLauncher
end
stack(style_class: [:dialog_container]) do
button "Sign In", tip: "https://sso.w3d.cyberarm.dev" do
# use SSO service
pop_state
end
caption "Sign in with your W3D Hub forum account", margin_bottom: PADDING
tagline "Username"
@username = edit_line "", width: 1.0
tagline "Password"
@password = edit_line "", type: :password, width: 1.0
end
flow(width: 1.0, padding: PADDING, background_nine_slice: NINE_SLICE_ROUNDED_BOTTOM, background_nine_slice_from_edge: NINE_SLICE_EDGE, background_nine_slice_color: 0) do
button "Cancel" do
pop_state
end
flow(fill: true)
button "Register", tip: "https://w3dhub.com/forum/register/" do
SDL.OpenURL("https://w3dhub.com/forum/register/")
end
flow(fill: true)
button "Sign In", **CTA_BUTTON_THEME do |btn|
# use SSO service, eventually. , tip: "https://sso.w3d.cyberarm.dev"
btn.enabled = false
Worker::Api.user_login(@username.value, @password.value) do |result|
if result.okay?
hash = JSON.parse(result.data)
MemCache[:settings].account = W3DHubLauncher::Worker::Api::Settings::Account.create(
user_id: hash["userid"],
username: hash["username"],
displayname: hash["displayname"],
avatar_uri: "",
access_token: hash["accessToken"],
refresh_token: hash["refreshToken"],
access_token_expiration_time: hash["accessTokenExpiry"]
)
Worker::Api.update_settings(MemCache[:settings]) do |settings_result|
if settings_result.okay?
on_main_thread(-> {
pop_state
window.current_state&.populate_account_information
})
end
# FIXME: Retry? Show error? ???
pp settings_result
end
end
# FIXME: Show error message!
pp result
end
end
end
end
end

View File

@@ -56,32 +56,8 @@ module W3DHubLauncher
# self account container
@account_container = flow(width: 400, height: 80, margin_left: LARGE_PADDING) do
# self avatar container
stack(width: 80, height: 1.0, background_image: rounded_avatar(safe_get_image("#{ROOT_PATH}/media/default.png")), tag: :account_avatar) do |i|
i.subscribe(:clicked_left_mouse_button) do
dialog(Dialog::Account)
end
# self online state container
stack(width: 20, height: 20, v_align: :bottom, h_align: :right, background_image: safe_get_image("#{ROOT_PATH}/media/ui/circle_small.png"), background_image_color: 0xff_26a269, tag: :account_online_state)
end
stack(fill: true, height: 1.0, margin_left: HALF_PADDING) do
flow(fill: true)
# self name
caption "cyberarm", font: FONT_BLACK, text_wrap: :none, width: 1.0, tag: :account_name
# self set online state
link "Online ▼", text_size: 18 do |l|
menu(parent: l) do
menu_item("Online")
menu_item("Do Not Disturb")
menu_item("Away")
menu_item("Invisible")
menu_item("Sign Out")
end.show
end
flow(fill: true)
end
end
populate_account_information
end
# layout container
@@ -357,6 +333,52 @@ module W3DHubLauncher
composite.save(path)
end
def populate_account_information
account = MemCache[:settings].account
@account_container.clear do
# self avatar container
stack(width: 80, height: 1.0, background_image: rounded_avatar(safe_get_image("#{ROOT_PATH}/media/default.png")), tag: :account_avatar) do
# self online state container
stack(
width: 20,
height: 20,
v_align: :bottom,
h_align: :right,
background_image: safe_get_image("#{ROOT_PATH}/media/ui/circle_small.png"),
background_image_color: account.signed_in? ? 0xff_26a269 : 0xff_77767b,
tag: :account_online_state
)
end
stack(fill: true, height: 1.0, margin_left: HALF_PADDING) do
flow(fill: true)
# self name
caption account.signed_in? ? account.displayname : MemCache[:settings].preferences.nickname, font: FONT_BLACK, text_wrap: :none, width: 1.0, tag: :account_name
if account.signed_in?
# self set online state
link "Online ▼", text_size: 18 do |l|
menu(parent: l) do
menu_item("Online")
menu_item("Do Not Disturb")
menu_item("Away")
menu_item("Invisible")
menu_item("Sign Out") do
# sign out
end
end.show
end
else
button "Sign In", text_size: 18 do
dialog(Dialog::Account)
end
end
flow(fill: true)
end
end
end
def button_up(id)
super

View File

@@ -516,6 +516,10 @@ module W3DHubLauncher
FileUtils.mkdir_p(W3DHubLauncher::CONFIG_PATH)
File.write(W3DHubLauncher::SETTINGS_FILE_PATH, query.data)
if @settings&.account&.signed_in?
@w3dhub_api.access_token = @settings.account.access_token
end
result.data = query.data
rescue => e
result.error = e

View File

@@ -30,6 +30,14 @@ module W3DHubLauncher
def self.account
end
def self.user_login(username, password, &block)
W3DHubLauncher::Worker::Request.new(:w3dhub_api_call, { call: :user_login, arguments: [username, password] }, &block)
end
def self.refresh_user_login(refresh_token, &block)
W3DHubLauncher::Worker::Request.new(:w3dhub_api_call, { call: :refresh_user_login, arguments: [refresh_token] }, &block)
end
# returns launcher settings
def self.settings
end

View File

@@ -8,6 +8,8 @@ module W3DHubLauncher
attr_reader :applications
attr_writer :access_token
def initialize
@access_token = nil
@@ -91,8 +93,13 @@ module W3DHubLauncher
result
end
def user_login()
def user_login(username, password, &block)
result = CyberarmEngine::Result.new
body = URI.encode_www_form("data": JSON.dump({username: username, password: password}))
response = fetch("#{PRIMARY_W3DHUB_API_ENDPOINT}/apis/launcher/1/user-login", method: :post, body: body, headers: headers(form_encoded: true))
pp response
end
def refresh_user_login()