tailwindcss + wip auth

This commit is contained in:
Per Stark committed 2024-12-12 20:59:27 +01:00
1 parent 1c4b3284bf
commit 1d8e19c88f
26 files changed
+2496 -300

No files matched your search

+7 -7
View File
@@ -1,14 +1,14 @@
use super::types::StoredObject;
use std::ops::Deref;
use surrealdb::{
engine::remote::ws::{Client, Ws},
engine::any::{connect, Any},
opt::auth::Root,
Error, Surreal,
};
#[derive(Clone)]
pub struct SurrealDbClient {
pub client: Surreal<Client>,
pub client: Surreal<Any>,
}
impl SurrealDbClient {
@@ -19,7 +19,7 @@ impl SurrealDbClient {
/// # Returns
/// * `SurrealDbClient` initialized
pub async fn new() -> Result<Self, Error> {
let db = Surreal::new::<Ws>("127.0.0.1:8000").await?;
let db = connect("ws://127.0.0.1:8000").await?;
// Sign in to database
db.signin(Root {
@@ -60,7 +60,7 @@ impl SurrealDbClient {
}
impl Deref for SurrealDbClient {
type Target = Surreal<Client>;
type Target = Surreal<Any>;
fn deref(&self) -> &Self::Target {
&self.client
@@ -75,7 +75,7 @@ impl Deref for SurrealDbClient {
///
/// # Returns
/// * `Result` - Item or Error
pub async fn store_item<T>(db_client: &Surreal<Client>, item: T) -> Result<Option<T>, Error>
pub async fn store_item<T>(db_client: &Surreal<Any>, item: T) -> Result<Option<T>, Error>
where
T: StoredObject + Send + Sync + 'static,
{
@@ -92,7 +92,7 @@ where
///
/// # Returns
/// * `Result` - Vec<T> or Error
pub async fn get_all_stored_items<T>(db_client: &Surreal<Client>) -> Result<Vec<T>, Error>
pub async fn get_all_stored_items<T>(db_client: &Surreal<Any>) -> Result<Vec<T>, Error>
where
T: for<'de> StoredObject,
{
@@ -107,7 +107,7 @@ where
///
/// # Returns
/// * `Result<Option<T>, Error>` - The found item or Error
pub async fn get_item<T>(db_client: &Surreal<Client>, id: &str) -> Result<Option<T>, Error>
pub async fn get_item<T>(db_client: &Surreal<Any>, id: &str) -> Result<Option<T>, Error>
where
T: for<'de> StoredObject,
{
+2 -2
View File
@@ -1,5 +1,5 @@
use crate::{error::ProcessingError, stored_object};
use surrealdb::{engine::remote::ws::Client, Surreal};
use surrealdb::{engine::any::Any, Surreal};
use tracing::debug;
use uuid::Uuid;
@@ -28,7 +28,7 @@ impl KnowledgeRelationship {
}
pub async fn store_relationship(
&self,
db_client: &Surreal<Client>,
db_client: &Surreal<Any>,
) -> Result<(), ProcessingError> {
let query = format!(
"RELATE knowledge_entity:`{}` -> relates_to -> knowledge_entity:`{}`",
+1
View File
@@ -5,6 +5,7 @@ pub mod knowledge_entity;
pub mod knowledge_relationship;
pub mod text_chunk;
pub mod text_content;
pub mod user;
#[async_trait]
pub trait StoredObject: Serialize + for<'de> Deserialize<'de> {
+100
View File
@@ -0,0 +1,100 @@
use crate::{
error::ApiError,
storage::db::{get_item, SurrealDbClient},
stored_object,
};
use axum_session_auth::Authentication;
use surrealdb::{engine::any::Any, Surreal};
use uuid::Uuid;
stored_object!(User, "user", {
email: String,
password: String,
anonymous: bool
});
#[async_trait]
impl Authentication<User, String, Surreal<Any>> for User {
async fn load_user(userid: String, pool: Option<&Surreal<Any>>) -> Result<User, anyhow::Error> {
let pool = pool.unwrap();
Ok(get_item::<Self>(&pool, userid.as_str()).await?.unwrap())
// User::get_user(userid, pool)
// .await
// .ok_or_else(|| anyhow::anyhow!("Could not load user"))
}
fn is_authenticated(&self) -> bool {
!self.anonymous
}
fn is_active(&self) -> bool {
!self.anonymous
}
fn is_anonymous(&self) -> bool {
self.anonymous
}
}
impl User {
pub async fn create_new(
email: String,
password: String,
db: &SurrealDbClient,
) -> Result<Self, ApiError> {
// Check if user exists
if let Some(_) = Self::find_by_email(&email, db).await? {
return Err(ApiError::UserAlreadyExists);
}
let id = Uuid::new_v4().to_string();
let user: Option<User> = db
.client
.query(
"CREATE type::thing('user', $id) SET
email = $email,
password = crypto::argon2::generate($password),
anonymous = false",
)
.bind(("id", id))
.bind(("email", email))
.bind(("password", password))
.await?
.take(0)?;
user.ok_or(ApiError::UserAlreadyExists)
}
pub async fn authenticate(
email: String,
password: String,
db: &SurrealDbClient,
) -> Result<Self, ApiError> {
let user: Option<User> = db
.client
.query(
"SELECT * FROM user
WHERE email = $email
AND crypto::argon2::compare(password, $password)",
)
.bind(("email", email))
.bind(("password", password))
.await?
.take(0)?;
user.ok_or(ApiError::UserAlreadyExists)
}
pub async fn find_by_email(
email: &str,
db: &SurrealDbClient,
) -> Result<Option<Self>, ApiError> {
let user: Option<User> = db
.client
.query("SELECT * FROM user WHERE email = $email LIMIT 1")
.bind(("email", email.to_string()))
.await?
.take(0)?;
Ok(user)
}
}