Skip to content

Commit

Permalink
fix: add protected user
Browse files Browse the repository at this point in the history
  • Loading branch information
luisfbl committed Feb 1, 2024
1 parent a283b06 commit 0b877e9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
1 change: 0 additions & 1 deletion rest-server/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use axum_login::{login_required, AuthManagerLayer, AuthManagerLayerBuilder};
use http::header::CONTENT_TYPE;
use http::{HeaderValue, Method};
use sqlx::{Pool, Postgres};
use tower_http::cors::Any;
use tower_sessions::{Expiry, SessionManagerLayer};
use tower_sessions::cookie::time::Duration;
use tower_sessions_sqlx_store::PostgresStore;
Expand Down
26 changes: 23 additions & 3 deletions rest-server/src/models/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};
use sqlx::{types::Uuid, FromRow, Row};

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct User {
pub struct ProtectedUser {
id: String,
pub email: String,
pub password: Option<String>,
Expand All @@ -15,7 +15,16 @@ pub struct User {
address: Option<String>,
}

impl<'r> FromRow<'r, sqlx::postgres::PgRow> for User {
#[derive(Serialize)]
pub struct User {
pub id: String,
avatar: Option<String>,
username: Option<String>,
name: Option<String>,
bio: Option<String>
}

impl<'r> FromRow<'r, sqlx::postgres::PgRow> for ProtectedUser {
fn from_row(row: &'r sqlx::postgres::PgRow) -> Result<Self, sqlx::Error> {
Ok(Self {
id: row.try_get::<Uuid, _>("id")?.to_string(),
Expand All @@ -31,8 +40,19 @@ impl<'r> FromRow<'r, sqlx::postgres::PgRow> for User {
}
}

impl<'r> FromRow<'r, sqlx::postgres::PgRow> for User {
fn from_row(row: &'r sqlx::postgres::PgRow) -> Result<Self, sqlx::Error> {
Ok(Self {
id: row.try_get::<Uuid, _>("id")?.to_string(),
avatar: row.try_get("avatar")?,
username: row.try_get("username")?,
name: row.try_get("name")?,
bio: row.try_get("bio")?,
})
}
}

impl AuthUser for User {
impl AuthUser for ProtectedUser {
type Id = String;

fn id(&self) -> Self::Id {
Expand Down
4 changes: 2 additions & 2 deletions rest-server/src/routes/auth/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use password_auth::verify_password;
use sqlx::{Pool, Postgres};
use sqlx::types::Uuid;
use crate::json::auth::Credentials;
use crate::models::users::User;
use crate::models::users::ProtectedUser;

#[derive(Clone)]
pub struct Backend {
Expand All @@ -25,7 +25,7 @@ pub enum BackendError {

#[async_trait]
impl AuthnBackend for Backend {
type User = User;
type User = ProtectedUser;
type Credentials = Credentials;
type Error = BackendError;

Expand Down
2 changes: 1 addition & 1 deletion rest-server/src/routes/auth/post.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use axum::extract::{Path, State};
use axum::{Extension, Json};
use axum::http::StatusCode;
use axum::response::{IntoResponse, Redirect};
use axum::response::IntoResponse;
use tower_sessions::Session;
use crate::app::AppState;
use crate::json::auth::{AuthUrlResponse, Credentials, LoginCreds, SignCreds};
Expand Down

0 comments on commit 0b877e9

Please sign in to comment.