feat: auth confirmation message after authenticating on websocket

This commit is contained in:
Ninjdai 2025-08-08 11:54:43 +02:00
parent c0f0d0521b
commit 44be2c83ba
2 changed files with 17 additions and 5 deletions

View file

@ -1,5 +1,6 @@
use std::sync::Arc;
use axum::extract::ws::{Message, Utf8Bytes};
use serde_json::{json, Value};
use crate::entities::owner;
@ -11,6 +12,7 @@ pub enum Event {
#[derive(Clone, Debug)]
pub enum WebsocketMessage {
AuthSuccess,
NewOwner(Arc<owner::Model>),
Error(String),
Ping
@ -20,11 +22,13 @@ impl WebsocketMessage {
pub fn to_json(&self) -> Value {
json!({
"type": match self {
Self::AuthSuccess => "auth_success",
Self::NewOwner(_) => "new_owner",
Self::Error(_) => "error",
Self::Ping => "ping",
},
"data": match self {
Self::AuthSuccess => json!(null),
Self::NewOwner(owner) => json!(owner),
Self::Error(error) => json!(error),
Self::Ping => json!(null),
@ -32,6 +36,10 @@ impl WebsocketMessage {
})
}
pub fn to_text_message(&self) -> Message {
Message::Text(Utf8Bytes::from(self.to_json().to_string()))
}
pub fn should_user_receive(&self, user_id: u32) -> bool {
match self {
Self::NewOwner(owner) => owner.user_id == user_id,