implement redirect to source on admin login endpoint

This commit is contained in:
Aravinth Manivannan 2021-10-13 14:18:13 +05:30
parent bc63926d18
commit 4491fbb6f2
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
4 changed files with 21 additions and 10 deletions

View file

@ -17,10 +17,10 @@
use actix_identity::Identity;
use actix_web::http::header;
use actix_web::{web, HttpResponse, Responder};
use actix_web::{web, HttpResponse, Responder};
use serde::{Deserialize, Serialize};
use super::get_random;
use super::{RedirectQuery, get_random};
use crate::errors::*;
use crate::AppData;
@ -231,12 +231,20 @@ async fn register(
async fn login(
id: Identity,
payload: web::Json<runners::Login>,
path: web::Query<RedirectQuery>,
data: AppData,
) -> ServiceResult<impl Responder> {
let payload = payload.into_inner();
let username = runners::login_runner(&payload, &data).await?;
let path = path.into_inner();
id.remember(username);
Ok(HttpResponse::Ok())
if let Some(redirect_to) = path.redirect_to {
Ok(HttpResponse::Found()
.insert_header((header::LOCATION, redirect_to))
.finish())
} else {
Ok(HttpResponse::Ok().into())
}
}
#[my_codegen::get(
path = "crate::V1_API_ROUTES.admin.auth.logout",

View file

@ -22,7 +22,7 @@ pub mod campaigns;
#[cfg(test)]
mod tests;
pub use super::{get_random, get_uuid};
pub use super::{get_random, get_uuid, RedirectQuery};
pub fn services(cfg: &mut ServiceConfig) {
auth::services(cfg);

View file

@ -24,7 +24,7 @@ use serde::{Deserialize, Serialize};
use sqlx::types::time::OffsetDateTime;
use uuid::Uuid;
use super::get_uuid;
use super::{RedirectQuery, get_uuid};
use crate::errors::*;
use crate::AppData;
@ -121,16 +121,13 @@ pub mod runners {
}
}
#[derive(Deserialize)]
pub struct Query {
pub redirect_to: Option<String>,
}
#[my_codegen::get(path = "crate::V1_API_ROUTES.benches.register")]
async fn register(
data: AppData,
id: Identity,
path: web::Query<Query>,
path: web::Query<RedirectQuery>,
) -> ServiceResult<HttpResponse> {
let uuid = runners::register_runner(&data).await?;
id.remember(uuid.to_string());

View file

@ -15,6 +15,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use actix_web::web::ServiceConfig;
use serde::Deserialize;
use uuid::Uuid;
pub mod admin;
@ -45,3 +46,8 @@ pub fn get_random(len: usize) -> String {
pub fn get_uuid() -> Uuid {
Uuid::new_v4()
}
#[derive(Deserialize)]
pub struct RedirectQuery {
pub redirect_to: Option<String>,
}