2022-06-20 11:29:06 +00:00
|
|
|
use crate::err::Error;
|
2022-08-23 22:44:13 +00:00
|
|
|
use crate::net::session;
|
2022-06-20 11:29:06 +00:00
|
|
|
use bytes::Bytes;
|
|
|
|
use std::str;
|
|
|
|
use surrealdb::sql::Value;
|
2022-08-23 22:44:13 +00:00
|
|
|
use surrealdb::Session;
|
2022-06-20 11:29:06 +00:00
|
|
|
use warp::http::Response;
|
2020-06-29 15:36:01 +00:00
|
|
|
use warp::Filter;
|
|
|
|
|
2022-05-10 00:07:03 +00:00
|
|
|
const MAX: u64 = 1024; // 1 KiB
|
|
|
|
|
2020-06-29 15:36:01 +00:00
|
|
|
pub fn config() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
|
|
|
|
// Set base path
|
|
|
|
let base = warp::path("signup").and(warp::path::end());
|
|
|
|
// Set opts method
|
|
|
|
let opts = base.and(warp::options()).map(warp::reply);
|
|
|
|
// Set post method
|
2022-06-20 11:29:06 +00:00
|
|
|
let post = base
|
|
|
|
.and(warp::post())
|
2022-08-23 22:44:13 +00:00
|
|
|
.and(session::build())
|
2022-06-20 11:29:06 +00:00
|
|
|
.and(warp::body::content_length_limit(MAX))
|
|
|
|
.and(warp::body::bytes())
|
|
|
|
.and_then(handler);
|
2020-06-29 15:36:01 +00:00
|
|
|
// Specify route
|
2022-07-23 12:49:26 +00:00
|
|
|
opts.or(post)
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
2022-08-23 22:44:13 +00:00
|
|
|
async fn handler(mut session: Session, body: Bytes) -> Result<impl warp::Reply, warp::Rejection> {
|
2022-07-04 00:01:24 +00:00
|
|
|
// Convert the HTTP body into text
|
2022-06-20 11:29:06 +00:00
|
|
|
let data = str::from_utf8(&body).unwrap();
|
2022-07-04 00:01:24 +00:00
|
|
|
// Parse the provided data as JSON
|
2022-06-20 11:29:06 +00:00
|
|
|
match surrealdb::sql::json(data) {
|
|
|
|
// The provided value was an object
|
2022-08-23 22:44:13 +00:00
|
|
|
Ok(Value::Object(vars)) => match crate::iam::signup::signup(&mut session, vars).await {
|
2022-07-04 00:01:24 +00:00
|
|
|
// Authentication was successful
|
|
|
|
Ok(v) => Ok(Response::builder().body(v)),
|
|
|
|
// There was an error with authentication
|
|
|
|
Err(e) => Err(warp::reject::custom(e)),
|
|
|
|
},
|
2022-06-20 11:29:06 +00:00
|
|
|
// The provided value was not an object
|
|
|
|
_ => Err(warp::reject::custom(Error::Request)),
|
|
|
|
}
|
|
|
|
}
|