1
0
mirror of https://github.com/actix/examples synced 2024-11-27 16:02:57 +01:00

Update database-interactions/simple-auth-server to v4 (#511)

Co-authored-by: Rob Ede <robjtede@icloud.com>
This commit is contained in:
Luca Palmieri 2022-02-02 15:19:49 +00:00 committed by GitHub
parent 9c620d1943
commit ff0a06e019
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 25 additions and 51 deletions

View File

@ -5,20 +5,20 @@ edition = "2021"
workspace = "../.."
[dependencies]
actix-web = "3"
actix-identity = "0.3"
actix-web = "4.0.0-beta.21"
actix-identity = "0.4.0-beta.8"
chrono = { version = "0.4.6", features = ["serde"] }
derive_more = "0.99.0"
diesel = { version = "1.4.5", features = ["postgres", "uuidv07", "r2d2", "chrono"] }
dotenv = "0.15"
env_logger = "0.8"
env_logger = "0.9.0"
futures = "0.3.1"
r2d2 = "0.8"
rust-argon2 = "0.8"
rust-argon2 = "1.0.0"
lazy_static = "1.4.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
sparkpost = "0.5.2"
uuid = { version = "0.8", features = ["serde", "v4"] }
time = "0.2"
uuid = { version = "0.8.2", features = ["serde", "v4"] }
time = "0.3.7"

View File

@ -1,8 +1,5 @@
use actix_identity::Identity;
use actix_web::{
dev::Payload, error::BlockingError, web, Error, FromRequest, HttpRequest,
HttpResponse,
};
use actix_web::{dev::Payload, web, Error, FromRequest, HttpRequest, HttpResponse};
use diesel::prelude::*;
use diesel::PgConnection;
use futures::future::{err, ok, Ready};
@ -23,7 +20,6 @@ pub struct AuthData {
pub type LoggedUser = SlimUser;
impl FromRequest for LoggedUser {
type Config = ();
type Error = Error;
type Future = Ready<Result<LoggedUser, Error>>;
@ -48,20 +44,12 @@ pub async fn login(
auth_data: web::Json<AuthData>,
id: Identity,
pool: web::Data<Pool>,
) -> Result<HttpResponse, ServiceError> {
let res = web::block(move || query(auth_data.into_inner(), pool)).await;
) -> Result<HttpResponse, actix_web::Error> {
let user = web::block(move || query(auth_data.into_inner(), pool)).await??;
match res {
Ok(user) => {
let user_string = serde_json::to_string(&user).unwrap();
id.remember(user_string);
Ok(HttpResponse::Ok().finish())
}
Err(err) => match err {
BlockingError::Error(service_error) => Err(service_error),
BlockingError::Canceled => Err(ServiceError::InternalServerError),
},
}
let user_string = serde_json::to_string(&user).unwrap();
id.remember(user_string);
Ok(HttpResponse::Ok().finish())
}
pub async fn get_me(logged_user: LoggedUser) -> HttpResponse {

View File

@ -1,9 +1,8 @@
use actix_web::{error::BlockingError, web, HttpResponse};
use actix_web::{web, HttpResponse};
use diesel::{prelude::*, PgConnection};
use serde::Deserialize;
use crate::email_service::send_invitation;
use crate::errors::ServiceError;
use crate::models::{Invitation, Pool};
#[derive(Deserialize)]
@ -14,19 +13,12 @@ pub struct InvitationData {
pub async fn post_invitation(
invitation_data: web::Json<InvitationData>,
pool: web::Data<Pool>,
) -> Result<HttpResponse, ServiceError> {
) -> Result<HttpResponse, actix_web::Error> {
// run diesel blocking code
let res =
web::block(move || create_invitation(invitation_data.into_inner().email, pool))
.await;
web::block(move || create_invitation(invitation_data.into_inner().email, pool))
.await??;
match res {
Ok(_) => Ok(HttpResponse::Ok().finish()),
Err(err) => match err {
BlockingError::Error(service_error) => Err(service_error),
BlockingError::Canceled => Err(ServiceError::InternalServerError),
},
}
Ok(HttpResponse::Ok().finish())
}
fn create_invitation(

View File

@ -37,7 +37,7 @@ async fn main() -> std::io::Result<()> {
// Start http server
HttpServer::new(move || {
App::new()
.data(pool.clone())
.app_data(web::Data::new(pool.clone()))
// enable logger
.wrap(middleware::Logger::default())
.wrap(IdentityService::new(
@ -45,10 +45,10 @@ async fn main() -> std::io::Result<()> {
.name("auth")
.path("/")
.domain(domain.as_str())
.max_age_time(Duration::days(1))
.max_age(Duration::days(1))
.secure(false), // this can only be true if you have https
))
.data(web::JsonConfig::default().limit(4096))
.app_data(web::JsonConfig::default().limit(4096))
// everything under '/api/' route
.service(
web::scope("/api")

View File

@ -1,4 +1,4 @@
use actix_web::{error::BlockingError, web, HttpResponse};
use actix_web::{web, HttpResponse};
use diesel::prelude::*;
use serde::Deserialize;
@ -15,23 +15,17 @@ pub async fn register_user(
invitation_id: web::Path<String>,
user_data: web::Json<UserData>,
pool: web::Data<Pool>,
) -> Result<HttpResponse, ServiceError> {
let res = web::block(move || {
) -> Result<HttpResponse, actix_web::Error> {
let user = web::block(move || {
query(
invitation_id.into_inner(),
user_data.into_inner().password,
pool,
)
})
.await;
.await??;
match res {
Ok(user) => Ok(HttpResponse::Ok().json(&user)),
Err(err) => match err {
BlockingError::Error(service_error) => Err(service_error),
BlockingError::Canceled => Err(ServiceError::InternalServerError),
},
}
Ok(HttpResponse::Ok().json(&user))
}
fn query(