added signup api
This commit is contained in:
@@ -24,6 +24,14 @@ pub struct User {
|
|||||||
last_name: String
|
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 {
|
pub async fn login(request: HttpRequest, body: web::Json<Entry>) -> impl Responder {
|
||||||
//TODO: finish login, for now will simply login for everyone
|
//TODO: finish login, for now will simply login for everyone
|
||||||
@@ -51,6 +59,23 @@ pub async fn logout(user: Identity) -> impl Responder {
|
|||||||
HttpResponse::Ok()
|
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> {
|
fn authenticate(username: &str, password: &str) -> Option<User> {
|
||||||
|
|
||||||
info!("Authenticating user: {}", username);
|
info!("Authenticating user: {}", username);
|
||||||
|
|||||||
@@ -46,6 +46,10 @@ async fn main() -> std::io::Result<()> {
|
|||||||
.route(web::post().to(api::user::login))
|
.route(web::post().to(api::user::login))
|
||||||
.route(web::delete().to(api::user::logout))
|
.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(api_index))
|
||||||
)
|
)
|
||||||
.route("/", web::get().to(index))
|
.route("/", web::get().to(index))
|
||||||
|
|||||||
Reference in New Issue
Block a user