2024-04-17 22:07:47 +02:00
|
|
|
use leptos::*;
|
2024-05-13 19:13:40 +02:00
|
|
|
use crate::URL_BASE;
|
2024-04-17 22:07:47 +02:00
|
|
|
|
2024-04-17 23:07:56 +02:00
|
|
|
pub trait AuthToken {
|
|
|
|
fn present(&self) -> bool;
|
|
|
|
fn token(&self) -> String;
|
2024-05-02 02:07:11 +02:00
|
|
|
fn user_id(&self) -> String;
|
2024-05-01 18:22:25 +02:00
|
|
|
fn username(&self) -> String;
|
|
|
|
fn outbox(&self) -> String;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
pub struct Auth {
|
|
|
|
pub token: Signal<Option<String>>,
|
2024-05-02 02:07:11 +02:00
|
|
|
pub userid: Signal<Option<String>>,
|
2024-04-17 23:07:56 +02:00
|
|
|
}
|
|
|
|
|
2024-05-01 18:22:25 +02:00
|
|
|
impl AuthToken for Auth {
|
2024-04-17 22:07:47 +02:00
|
|
|
fn token(&self) -> String {
|
2024-05-01 18:22:25 +02:00
|
|
|
self.token.get().unwrap_or_default()
|
|
|
|
}
|
2024-05-02 02:07:11 +02:00
|
|
|
|
|
|
|
fn user_id(&self) -> String {
|
|
|
|
self.userid.get().unwrap_or_default()
|
|
|
|
}
|
2024-05-01 18:22:25 +02:00
|
|
|
|
|
|
|
fn username(&self) -> String {
|
2024-05-02 02:07:11 +02:00
|
|
|
// TODO maybe cache this?? how often do i need it?
|
|
|
|
self.userid.get()
|
|
|
|
.unwrap_or_default()
|
|
|
|
.split('/')
|
|
|
|
.last()
|
|
|
|
.unwrap_or_default()
|
|
|
|
.to_string()
|
2024-04-17 22:07:47 +02:00
|
|
|
}
|
2024-05-01 18:22:25 +02:00
|
|
|
|
2024-04-17 22:07:47 +02:00
|
|
|
fn present(&self) -> bool {
|
2024-05-01 18:22:25 +02:00
|
|
|
self.token.get().map_or(false, |x| !x.is_empty())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn outbox(&self) -> String {
|
2024-05-02 02:07:11 +02:00
|
|
|
format!("{URL_BASE}/users/{}/outbox", self.username())
|
2024-04-17 22:07:47 +02:00
|
|
|
}
|
|
|
|
}
|