surrealpatch/src/net/signup.rs

44 lines
1.3 KiB
Rust
Raw Normal View History

use crate::err::Error;
use crate::net::session;
use bytes::Bytes;
use std::str;
use surrealdb::sql::Value;
use surrealdb::Session;
use warp::http::Response;
2020-06-29 15:36:01 +00:00
use warp::Filter;
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
let post = base
.and(warp::post())
.and(session::build())
.and(warp::body::content_length_limit(MAX))
.and(warp::body::bytes())
.and_then(handler);
2020-06-29 15:36:01 +00:00
// Specify route
opts.or(post)
2020-06-29 15:36:01 +00:00
}
async fn handler(mut session: Session, body: Bytes) -> Result<impl warp::Reply, warp::Rejection> {
// Convert the HTTP body into text
let data = str::from_utf8(&body).unwrap();
// Parse the provided data as JSON
match surrealdb::sql::json(data) {
// The provided value was an object
Ok(Value::Object(vars)) => match crate::iam::signup::signup(&mut session, vars).await {
// Authentication was successful
Ok(v) => Ok(Response::builder().body(v)),
// There was an error with authentication
Err(e) => Err(warp::reject::custom(e)),
},
// The provided value was not an object
_ => Err(warp::reject::custom(Error::Request)),
}
}