feat: allow passing auth token for chat

This commit is contained in:
əlemi 2024-06-05 03:32:18 +02:00
parent 52ed43745d
commit c6f9d34490
Signed by: alemi
GPG key ID: A4895B84D311642C
3 changed files with 19 additions and 9 deletions

View file

@ -37,7 +37,7 @@ impl History {
pub async fn run<T: ratatui::backend::Backend>(term: &mut Terminal<T>, args: crate::Args) -> Result<(), Box<dyn std::error::Error>> { pub async fn run<T: ratatui::backend::Backend>(term: &mut Terminal<T>, args: crate::Args) -> Result<(), Box<dyn std::error::Error>> {
let mut chat = Chat::register(&args.server).await?; let mut chat = Chat::register(&args.server, args.token).await?;
let mut stream = crossterm::event::EventStream::new(); let mut stream = crossterm::event::EventStream::new();
let mut input = String::new(); let mut input = String::new();

View file

@ -55,15 +55,21 @@ pub struct Chat {
} }
impl Chat { impl Chat {
pub async fn register(server: &str) -> Result<Chat, ChatError> { pub async fn register(server: &str, token: Option<String>) -> Result<Chat, ChatError> {
let token = match token {
Some(t) => t,
None => {
let registration : RegisterResponse = reqwest::Client::new() let registration : RegisterResponse = reqwest::Client::new()
.post(format!("https://{server}/api/chat/register")) .post(format!("https://{server}/api/chat/register"))
.send() .send()
.await? .await?
.json() .json()
.await?; .await?;
registration.access_token
},
};
let ws_url = format!("wss://{server}/ws?accessToken={}", registration.access_token); let ws_url = format!("wss://{server}/ws?accessToken={token}");
let (ws, _response) = tokio_tungstenite::connect_async(ws_url).await?; let (ws, _response) = tokio_tungstenite::connect_async(ws_url).await?;

View file

@ -9,6 +9,10 @@ mod proto;
struct Args { struct Args {
/// owncast server to connect to /// owncast server to connect to
server: String, server: String,
/// access token to use for login
#[arg(long)]
token: Option<String>,
} }
#[tokio::main(flavor = "current_thread")] #[tokio::main(flavor = "current_thread")]