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:
parent
9c620d1943
commit
ff0a06e019
@ -5,20 +5,20 @@ edition = "2021"
|
|||||||
workspace = "../.."
|
workspace = "../.."
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = "3"
|
actix-web = "4.0.0-beta.21"
|
||||||
actix-identity = "0.3"
|
actix-identity = "0.4.0-beta.8"
|
||||||
|
|
||||||
chrono = { version = "0.4.6", features = ["serde"] }
|
chrono = { version = "0.4.6", features = ["serde"] }
|
||||||
derive_more = "0.99.0"
|
derive_more = "0.99.0"
|
||||||
diesel = { version = "1.4.5", features = ["postgres", "uuidv07", "r2d2", "chrono"] }
|
diesel = { version = "1.4.5", features = ["postgres", "uuidv07", "r2d2", "chrono"] }
|
||||||
dotenv = "0.15"
|
dotenv = "0.15"
|
||||||
env_logger = "0.8"
|
env_logger = "0.9.0"
|
||||||
futures = "0.3.1"
|
futures = "0.3.1"
|
||||||
r2d2 = "0.8"
|
r2d2 = "0.8"
|
||||||
rust-argon2 = "0.8"
|
rust-argon2 = "1.0.0"
|
||||||
lazy_static = "1.4.0"
|
lazy_static = "1.4.0"
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
sparkpost = "0.5.2"
|
sparkpost = "0.5.2"
|
||||||
uuid = { version = "0.8", features = ["serde", "v4"] }
|
uuid = { version = "0.8.2", features = ["serde", "v4"] }
|
||||||
time = "0.2"
|
time = "0.3.7"
|
||||||
|
@ -1,8 +1,5 @@
|
|||||||
use actix_identity::Identity;
|
use actix_identity::Identity;
|
||||||
use actix_web::{
|
use actix_web::{dev::Payload, web, Error, FromRequest, HttpRequest, HttpResponse};
|
||||||
dev::Payload, error::BlockingError, web, Error, FromRequest, HttpRequest,
|
|
||||||
HttpResponse,
|
|
||||||
};
|
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
use diesel::PgConnection;
|
use diesel::PgConnection;
|
||||||
use futures::future::{err, ok, Ready};
|
use futures::future::{err, ok, Ready};
|
||||||
@ -23,7 +20,6 @@ pub struct AuthData {
|
|||||||
pub type LoggedUser = SlimUser;
|
pub type LoggedUser = SlimUser;
|
||||||
|
|
||||||
impl FromRequest for LoggedUser {
|
impl FromRequest for LoggedUser {
|
||||||
type Config = ();
|
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = Ready<Result<LoggedUser, Error>>;
|
type Future = Ready<Result<LoggedUser, Error>>;
|
||||||
|
|
||||||
@ -48,20 +44,12 @@ pub async fn login(
|
|||||||
auth_data: web::Json<AuthData>,
|
auth_data: web::Json<AuthData>,
|
||||||
id: Identity,
|
id: Identity,
|
||||||
pool: web::Data<Pool>,
|
pool: web::Data<Pool>,
|
||||||
) -> Result<HttpResponse, ServiceError> {
|
) -> Result<HttpResponse, actix_web::Error> {
|
||||||
let res = web::block(move || query(auth_data.into_inner(), pool)).await;
|
let user = web::block(move || query(auth_data.into_inner(), pool)).await??;
|
||||||
|
|
||||||
match res {
|
let user_string = serde_json::to_string(&user).unwrap();
|
||||||
Ok(user) => {
|
id.remember(user_string);
|
||||||
let user_string = serde_json::to_string(&user).unwrap();
|
Ok(HttpResponse::Ok().finish())
|
||||||
id.remember(user_string);
|
|
||||||
Ok(HttpResponse::Ok().finish())
|
|
||||||
}
|
|
||||||
Err(err) => match err {
|
|
||||||
BlockingError::Error(service_error) => Err(service_error),
|
|
||||||
BlockingError::Canceled => Err(ServiceError::InternalServerError),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_me(logged_user: LoggedUser) -> HttpResponse {
|
pub async fn get_me(logged_user: LoggedUser) -> HttpResponse {
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
use actix_web::{error::BlockingError, web, HttpResponse};
|
use actix_web::{web, HttpResponse};
|
||||||
use diesel::{prelude::*, PgConnection};
|
use diesel::{prelude::*, PgConnection};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use crate::email_service::send_invitation;
|
use crate::email_service::send_invitation;
|
||||||
use crate::errors::ServiceError;
|
|
||||||
use crate::models::{Invitation, Pool};
|
use crate::models::{Invitation, Pool};
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
@ -14,19 +13,12 @@ pub struct InvitationData {
|
|||||||
pub async fn post_invitation(
|
pub async fn post_invitation(
|
||||||
invitation_data: web::Json<InvitationData>,
|
invitation_data: web::Json<InvitationData>,
|
||||||
pool: web::Data<Pool>,
|
pool: web::Data<Pool>,
|
||||||
) -> Result<HttpResponse, ServiceError> {
|
) -> Result<HttpResponse, actix_web::Error> {
|
||||||
// run diesel blocking code
|
// run diesel blocking code
|
||||||
let res =
|
web::block(move || create_invitation(invitation_data.into_inner().email, pool))
|
||||||
web::block(move || create_invitation(invitation_data.into_inner().email, pool))
|
.await??;
|
||||||
.await;
|
|
||||||
|
|
||||||
match res {
|
Ok(HttpResponse::Ok().finish())
|
||||||
Ok(_) => Ok(HttpResponse::Ok().finish()),
|
|
||||||
Err(err) => match err {
|
|
||||||
BlockingError::Error(service_error) => Err(service_error),
|
|
||||||
BlockingError::Canceled => Err(ServiceError::InternalServerError),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_invitation(
|
fn create_invitation(
|
||||||
|
@ -37,7 +37,7 @@ async fn main() -> std::io::Result<()> {
|
|||||||
// Start http server
|
// Start http server
|
||||||
HttpServer::new(move || {
|
HttpServer::new(move || {
|
||||||
App::new()
|
App::new()
|
||||||
.data(pool.clone())
|
.app_data(web::Data::new(pool.clone()))
|
||||||
// enable logger
|
// enable logger
|
||||||
.wrap(middleware::Logger::default())
|
.wrap(middleware::Logger::default())
|
||||||
.wrap(IdentityService::new(
|
.wrap(IdentityService::new(
|
||||||
@ -45,10 +45,10 @@ async fn main() -> std::io::Result<()> {
|
|||||||
.name("auth")
|
.name("auth")
|
||||||
.path("/")
|
.path("/")
|
||||||
.domain(domain.as_str())
|
.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
|
.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
|
// everything under '/api/' route
|
||||||
.service(
|
.service(
|
||||||
web::scope("/api")
|
web::scope("/api")
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use actix_web::{error::BlockingError, web, HttpResponse};
|
use actix_web::{web, HttpResponse};
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
@ -15,23 +15,17 @@ pub async fn register_user(
|
|||||||
invitation_id: web::Path<String>,
|
invitation_id: web::Path<String>,
|
||||||
user_data: web::Json<UserData>,
|
user_data: web::Json<UserData>,
|
||||||
pool: web::Data<Pool>,
|
pool: web::Data<Pool>,
|
||||||
) -> Result<HttpResponse, ServiceError> {
|
) -> Result<HttpResponse, actix_web::Error> {
|
||||||
let res = web::block(move || {
|
let user = web::block(move || {
|
||||||
query(
|
query(
|
||||||
invitation_id.into_inner(),
|
invitation_id.into_inner(),
|
||||||
user_data.into_inner().password,
|
user_data.into_inner().password,
|
||||||
pool,
|
pool,
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.await;
|
.await??;
|
||||||
|
|
||||||
match res {
|
Ok(HttpResponse::Ok().json(&user))
|
||||||
Ok(user) => Ok(HttpResponse::Ok().json(&user)),
|
|
||||||
Err(err) => match err {
|
|
||||||
BlockingError::Error(service_error) => Err(service_error),
|
|
||||||
BlockingError::Canceled => Err(ServiceError::InternalServerError),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn query(
|
fn query(
|
||||||
|
Loading…
Reference in New Issue
Block a user