Implement a temporary system for saving Credentials. (#38)

This handles adding the functionality for saving the Token and Channel for Twitch, as well as the Port and Password for OBS when handling connections.

Co-authored-by: Eroax <eroaxe.business@gmail.com>
Reviewed-on: https://codeberg.org/Eroax/StreamGraph/pulls/38
This commit is contained in:
Eroax 2024-01-15 05:35:16 +00:00
parent a457d48cd0
commit b8e65edcef
7 changed files with 90 additions and 10 deletions

View file

@ -1,4 +0,0 @@
extends Resource
class_name TokenSaver
@export var token : String

View file

@ -46,6 +46,7 @@ func setup_chat_connection(default_chat : String = "", token : String = token, r
if !default_chat.is_empty(): if !default_chat.is_empty():
Connections.save_credentials({"token" : token, "channel" : default_chat}, "twitch")
chat_socket.join_chat(default_chat) chat_socket.join_chat(default_chat)

View file

@ -10,3 +10,38 @@ static func _twitch_chat_received(msg_dict : Dictionary):
DeckHolder.send_event(&"twitch_chat", msg_dict) DeckHolder.send_event(&"twitch_chat", msg_dict)
## Temporary Function for loading generic Connection credentials from user:// as a [CredSaver] Resource
static func load_credentials(service : String):
if !FileAccess.file_exists("user://" + service + "_creds.res"):
DeckHolder.logger.log_system("No Credentials exist for " + service + " Service", Logger.LogType.WARN)
return null
var creds = ResourceLoader.load("user://" + service + "_creds.res")
if !creds is CredSaver:
DeckHolder.logger.log_system("Error loading Credentials for " + service + " Service", Logger.LogType.ERROR)
return null
return creds
## Temporary function for saving generic Connection credentials to user:// as a CredSaver
static func save_credentials(data : Dictionary, service: String):
var creds := CredSaver.new()
creds.data = data
var err = ResourceSaver.save(creds, "user://" + service + "_creds.res")
if err != OK:
DeckHolder.logger.log_system("Error saving Credentials for " + service + " Service", Logger.LogType.ERROR)

View file

@ -0,0 +1,4 @@
extends Resource
class_name CredSaver
@export var data : Dictionary

View file

@ -21,6 +21,7 @@ var state: ConnectionState
@onready var _old_password: String = password_line_edit.text @onready var _old_password: String = password_line_edit.text
func get_port() -> int: func get_port() -> int:
return int(port_spin_box.value) return int(port_spin_box.value)
@ -39,13 +40,24 @@ func set_button_state(p_state: ConnectionState) -> void:
ConnectionState.CONNECTING: ConnectionState.CONNECTING:
connect_button.text = "Connecting..." connect_button.text = "Connecting..."
ConnectionState.CONNECTED: ConnectionState.CONNECTED:
Connections.save_credentials({"port" : get_port(), "password" : get_password()}, "OBS")
connect_button.text = "Disconnect" connect_button.text = "Disconnect"
func _ready() -> void: func _ready() -> void:
var loaded_creds = Connections.load_credentials("OBS")
if loaded_creds != null:
password_line_edit.text = loaded_creds.data.password
port_spin_box.value = loaded_creds.data.port
connect_button.pressed.connect( connect_button.pressed.connect(
func(): func():
connect_button_pressed.emit(state) connect_button_pressed.emit(state)
) )
canceled.connect( canceled.connect(

View file

@ -8,11 +8,29 @@ func _ready():
%Authenticate.pressed.connect(authenticate_twitch) %Authenticate.pressed.connect(authenticate_twitch)
%CopyURLButton.pressed.connect(copy_auth_link) %CopyURLButton.pressed.connect(copy_auth_link)
confirmed.connect(connect_to_chat) #confirmed.connect(connect_to_chat)
%Join_Chat.pressed.connect(connect_to_chat)
func authenticate_twitch(): func authenticate_twitch():
# Temporary setup for loading credentials
var loaded_creds = Connections.load_credentials("twitch")
if loaded_creds != null:
Connections.twitch.token = loaded_creds.data.token
if loaded_creds.data.has("channel"):
%Default_Chat.text = loaded_creds.data.channel
connect_to_chat()
return
# Temporary setup for saving tokens.
Connections.twitch.token_received.connect(func(token):
Connections.save_credentials({"token" : token}, "twitch")
)
var url = Connections.twitch.authenticate_with_twitch(%Client_ID.text) var url = Connections.twitch.authenticate_with_twitch(%Client_ID.text)
OS.shell_open(url) OS.shell_open(url)
@ -24,5 +42,8 @@ func copy_auth_link():
func connect_to_chat(): func connect_to_chat():
Connections.twitch.setup_chat_connection(%Default_Chat.text) if Connections.twitch.chat_socket.get_ready_state() != WebSocketPeer.STATE_OPEN:
Connections.twitch.setup_chat_connection(%Default_Chat.text)
Connections.twitch.join_channel(%Default_Chat.text)

View file

@ -35,7 +35,7 @@ unique_name_in_owner = true
layout_mode = 2 layout_mode = 2
size_flags_horizontal = 3 size_flags_horizontal = 3
size_flags_stretch_ratio = 0.25 size_flags_stretch_ratio = 0.25
text = "Authenticate" text = "Connect"
[node name="CopyURLButton" type="Button" parent="VBoxContainer/Authentication"] [node name="CopyURLButton" type="Button" parent="VBoxContainer/Authentication"]
unique_name_in_owner = true unique_name_in_owner = true
@ -50,7 +50,18 @@ disabled = true
button_pressed = true button_pressed = true
text = "Extra Chat Info" text = "Extra Chat Info"
[node name="Default_Chat" type="LineEdit" parent="VBoxContainer"] [node name="HBoxContainer" type="HBoxContainer" parent="VBoxContainer"]
layout_mode = 2
[node name="Default_Chat" type="LineEdit" parent="VBoxContainer/HBoxContainer"]
unique_name_in_owner = true unique_name_in_owner = true
layout_mode = 2 layout_mode = 2
placeholder_text = "Default Chat Channel" size_flags_horizontal = 3
placeholder_text = "Default Channel/New Channel"
[node name="Join_Chat" type="Button" parent="VBoxContainer/HBoxContainer"]
unique_name_in_owner = true
layout_mode = 2
size_flags_horizontal = 3
size_flags_stretch_ratio = 0.24
text = "Join"