Redirect to requested resource after login
This commit is contained in:
parent
6ad8426e7f
commit
c8de447334
|
@ -49,10 +49,16 @@ struct LoginForm {
|
||||||
password: String,
|
password: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct LoginQuery {
|
||||||
|
redirect_to: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
#[post("/login")]
|
#[post("/login")]
|
||||||
async fn login(
|
async fn login(
|
||||||
req: HttpRequest,
|
req: HttpRequest,
|
||||||
form: web::Form<LoginForm>,
|
form: web::Form<LoginForm>,
|
||||||
|
query: web::Query<LoginQuery>,
|
||||||
) -> Result<impl Responder, error::Error> {
|
) -> Result<impl Responder, error::Error> {
|
||||||
// Very basic authentication for now (only password, hardcoded in environment variable)
|
// Very basic authentication for now (only password, hardcoded in environment variable)
|
||||||
if form.password
|
if form.password
|
||||||
|
@ -61,9 +67,12 @@ async fn login(
|
||||||
{
|
{
|
||||||
Identity::login(&req.extensions(), "superuser".into())
|
Identity::login(&req.extensions(), "superuser".into())
|
||||||
.map_err(error::ErrorInternalServerError)?;
|
.map_err(error::ErrorInternalServerError)?;
|
||||||
Ok(web::Redirect::to("/".to_owned()).see_other())
|
Ok(
|
||||||
|
web::Redirect::to(query.into_inner().redirect_to.unwrap_or("/".to_string()))
|
||||||
|
.see_other(),
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
Ok(web::Redirect::to("/login".to_owned()).see_other())
|
Ok(web::Redirect::to(req.uri().to_string()).see_other())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,10 +8,14 @@ use actix_web::{dev::ServiceResponse, error, HttpResponse};
|
||||||
pub fn redirect_to_login<B>(
|
pub fn redirect_to_login<B>(
|
||||||
res: ServiceResponse<B>,
|
res: ServiceResponse<B>,
|
||||||
) -> Result<ErrorHandlerResponse<B>, error::Error> {
|
) -> Result<ErrorHandlerResponse<B>, error::Error> {
|
||||||
|
let redirect_to = format!(
|
||||||
|
"/login?{}",
|
||||||
|
serde_urlencoded::to_string(&[("redirect_to", res.request().uri().to_string())])?
|
||||||
|
);
|
||||||
Ok(ErrorHandlerResponse::Response(
|
Ok(ErrorHandlerResponse::Response(
|
||||||
res.into_response(
|
res.into_response(
|
||||||
HttpResponse::SeeOther()
|
HttpResponse::SeeOther()
|
||||||
.insert_header(("Location", "/login"))
|
.insert_header(("Location", redirect_to))
|
||||||
.finish()
|
.finish()
|
||||||
.map_into_right_body(),
|
.map_into_right_body(),
|
||||||
),
|
),
|
||||||
|
|
Loading…
Reference in a new issue