1
0
Fork 0
forked from alemi/upub
upub/web/src/auth.rs
alemi b72851fbfe
chore!: /users/ -> /actors/
sorry! this will break federation but better sooner than later,
everything is called following its AP name except users??? had to be
changed eventually
2024-05-29 20:51:30 +02:00

44 lines
883 B
Rust

use leptos::*;
use crate::URL_BASE;
pub trait AuthToken {
fn present(&self) -> bool;
fn token(&self) -> String;
fn user_id(&self) -> String;
fn username(&self) -> String;
fn outbox(&self) -> String;
}
#[derive(Debug, Clone, Copy)]
pub struct Auth {
pub token: Signal<Option<String>>,
pub userid: Signal<Option<String>>,
}
impl AuthToken for Auth {
fn token(&self) -> String {
self.token.get().unwrap_or_default()
}
fn user_id(&self) -> String {
self.userid.get().unwrap_or_default()
}
fn username(&self) -> String {
// TODO maybe cache this?? how often do i need it?
self.userid.get()
.unwrap_or_default()
.split('/')
.last()
.unwrap_or_default()
.to_string()
}
fn present(&self) -> bool {
self.token.get().map_or(false, |x| !x.is_empty())
}
fn outbox(&self) -> String {
format!("{URL_BASE}/actors/{}/outbox", self.username())
}
}