How to implement custom UserStore with known User and UserId types? #75
-
I'm building a website with EdgeDB. Axum-login doesn't have built-in use uuid::Uuid;
use axum_login::{AuthUser, secrecy::SecretVec};
use edgedb_derive::Queryable;
#[derive(Debug, Default, Clone, PartialEq, Queryable)]
pub struct User {
pub id: Uuid,
pub username: String,
pub email: String,
pub password: String,
pub is_active: bool,
pub is_superuser: bool,
}
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub enum Role {
Admin,
}
impl AuthUser<Uuid, Role> for User {
fn get_id(&self) -> Uuid {
self.id
}
fn get_password_hash(&self) -> SecretVec<u8> {
SecretVec::new(self.password.clone().into())
}
fn get_role(&self) -> Option<Role> {
Some(Role::Admin)
}
} For now, this code compiles, but I don't want to have generic for use std::hash::Hash;
use std::marker::PhantomData;
use async_trait::async_trait;
use axum_login::{UserStore as UserStoreTrait, AuthUser};
use edgedb_protocol::{query_arg::ScalarArg, queryable::Queryable};
use eyre::Error;
#[derive(Clone, Debug)]
pub struct EdgeDbStore<IUser> {
client: edgedb_tokio::Client,
_user_type: PhantomData<IUser>,
}
impl<IUser> EdgeDbStore<IUser> {
pub fn new(client: edgedb_tokio::Client) -> Self {
Self {
client,
_user_type: Default::default(),
}
}
}
#[async_trait]
impl<UserId, IUser, Role> UserStoreTrait<UserId, Role> for EdgeDbStore<IUser>
where
UserId: Eq + Clone + Send + Sync + Hash + ScalarArg + 'static,
Role: PartialOrd + PartialEq + Clone + Send + Sync + 'static,
IUser: AuthUser<UserId, Role> + Queryable,
{
type User = IUser;
async fn load_user(&self, user_id: &UserId) -> Result<Option<Self::User>, Error> {
let q = "SELECT User {id, username, email, password, is_active, is_superuser} FILTER .id = <uuid>$0";
let user: Option<IUser> = self.client.query_single(q, &(user_id,)).await?;
Ok(user)
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
maxcountryman
Nov 12, 2023
Replies: 1 comment 1 reply
-
The crate API has changed and we no longer have user stores (this is replaced by the backend concept). Please let us know if the new design makes getting EdgeDB setup easier for you. |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
hongquan
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The crate API has changed and we no longer have user stores (this is replaced by the backend concept).
Please let us know if the new design makes getting EdgeDB setup easier for you.