diff --git a/src/api/user.rs b/src/api/user.rs index 2dbf123..c01e588 100644 --- a/src/api/user.rs +++ b/src/api/user.rs @@ -24,6 +24,14 @@ pub struct User { last_name: String } +#[derive(Deserialize, Serialize, Debug, Default)] +pub struct NewUser { + netid: String, + password: String, + first_name: String, + last_name: String +} + pub async fn login(request: HttpRequest, body: web::Json) -> impl Responder { //TODO: finish login, for now will simply login for everyone @@ -51,6 +59,23 @@ pub async fn logout(user: Identity) -> impl Responder { HttpResponse::Ok() } +pub async fn signup(request: HttpRequest, body: web::Json) -> impl Responder { + let body = body.into_inner(); + + match create_user(body.netid.as_str(), body.password.as_str(), body.first_name.as_str(), body.last_name.as_str()) { + Ok(_) => { + Identity::login(&request.extensions(), body.netid.clone()); + web::Json(User { id: body.netid, first_name: body.first_name, last_name: body.last_name}); + HttpResponse::Ok() + } + Err(e) => { + error!("Error creating new user: {}", e); + HttpResponse::InternalServerError() + } + } + +} + fn authenticate(username: &str, password: &str) -> Option { info!("Authenticating user: {}", username); diff --git a/src/main.rs b/src/main.rs index d97575f..a03802f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -46,6 +46,10 @@ async fn main() -> std::io::Result<()> { .route(web::post().to(api::user::login)) .route(web::delete().to(api::user::logout)) ) + .service( + web::resource("/signup") + .route(web::post().to(api::user::signup)) + ) .route("/", web::get().to(api_index)) ) .route("/", web::get().to(index))