Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
levkk committed Dec 4, 2024
1 parent 8271b74 commit c1126d7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
5 changes: 1 addition & 4 deletions examples/users/src/controllers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ pub struct Signup;
#[async_trait]
impl PageController for Signup {
async fn get(&self, request: &Request) -> Result<Response, Error> {
let user = {
let mut conn = Pool::connection().await?;
request.user::<User>(&mut conn).await?
};
let user = request.user::<User>(Pool::pool()).await?;

if let Some(_) = user {
return Ok(Response::new().redirect("/profile"));
Expand Down
8 changes: 2 additions & 6 deletions examples/users/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@ impl User {
_ => (),
}

let mut conn = Pool::connection().await?;

let user = User::create(&[
("email", email.to_value()),
("password", encrypted_password.to_value()),
])
.fetch(&mut conn)
.fetch(Pool::pool())
.await?;

Ok(UserLogin::Ok(user))
Expand All @@ -44,10 +42,8 @@ impl User {
/// Return a user if one exists and the passwords match.
/// Return `None` otherwise.
pub async fn login(email: &str, password: &str) -> Result<UserLogin, Error> {
let mut conn = Pool::connection().await?;

if let Some(user) = User::filter("email", email)
.fetch_optional(&mut conn)
.fetch_optional(Pool::pool())
.await?
{
if hash_validate(password.as_bytes(), &user.password)? {
Expand Down
11 changes: 9 additions & 2 deletions rwf/src/http/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use time::OffsetDateTime;
use tokio::io::{AsyncRead, AsyncReadExt};

use super::{Cookies, Error, FormData, FromFormData, Head, Params, Response, ToParameter};
use crate::prelude::ToConnectionRequest;
use crate::{
config::get_config,
controller::{Session, SessionId},
Expand Down Expand Up @@ -265,7 +266,10 @@ impl Request {
/// let conn = Pool::connection().await?;
/// let user = request.user::<User>(&mut conn).await?;
/// ```
pub async fn user<T: Model>(&self, conn: &mut ConnectionGuard) -> Result<Option<T>, Error> {
pub async fn user<T: Model>(
&self,
conn: impl ToConnectionRequest<'_>,
) -> Result<Option<T>, Error> {
match self.session_id() {
SessionId::Authenticated(user_id) => Ok(Some(T::find(user_id).fetch(conn).await?)),

Expand All @@ -275,7 +279,10 @@ impl Request {

/// Same function as [`Request::user`], except if returns a [`Result`] instead of an [`Option`].
/// If used with the `?` operator, returns `403 - Unauthorized` automatically.
pub async fn user_required<T: Model>(&self, conn: &mut ConnectionGuard) -> Result<T, Error> {
pub async fn user_required<T: Model>(
&self,
conn: impl ToConnectionRequest<'_>,
) -> Result<T, Error> {
match self.user(conn).await? {
Some(user) => Ok(user),
None => Err(Error::Forbidden),
Expand Down

0 comments on commit c1126d7

Please sign in to comment.