added signup api

This commit is contained in:
Colin McKechney
2023-04-28 00:46:47 -04:00
parent 2a47588eb1
commit 7f1e4a0808
2 changed files with 29 additions and 0 deletions

View File

@@ -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<Entry>) -> 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<NewUser>) -> 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<User> {
info!("Authenticating user: {}", username);

View File

@@ -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))