hoppinworld_backend

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | LICENSE

commit 76899db53d5afc05c2701f3f037d96c64069e66b
parent 694c41a7c35ba231a82f137cf1812913eb6b702b
Author: Joël Lupien (Jojolepro) <jojolepromain@gmail.com>
Date:   Fri, 15 Feb 2019 22:57:45 -0500

Token validation endpoint.

Diffstat:
Msrc/endpoint.rs | 32++++++++++++++++++++++++++------
Msrc/main.rs | 1+
2 files changed, 27 insertions(+), 6 deletions(-)

diff --git a/src/endpoint.rs b/src/endpoint.rs @@ -135,10 +135,10 @@ pub fn register(db: DbConn, register: Json<RegisterRequest>, req: UserIp) -> Res match password_reset_insert_or_replace(&db, password_reset) { Ok(_) => { let key = env::var("MAILGUN_KEY").expect("MAILGUN_KEY must be set!"); - let creds = Credentials::new(&key, "hoppinworld.net"); - let from = EmailAddress::name_address("HoppinWorld", "noreply@hoppinworld.net"); + let creds = Credentials::new(&key, "mail.hoppinworld.net"); + let from = EmailAddress::name_address("HoppinWorld", "noreply@mail.hoppinworld.net"); let msg = Message { - to: vec![EmailAddress::address("jojolepromain@gmail.com")], + to: vec![EmailAddress::address(&register.email)], cc: vec![], bcc: vec![], subject: String::from("HoppinWorld Register"), @@ -203,10 +203,10 @@ pub fn request_password_reset(db: DbConn, passreset: Json<PasswordResetRequest>, match password_reset_insert_or_replace(&db, password_reset) { Ok(_) => { let key = env::var("MAILGUN_KEY").expect("MAILGUN_KEY must be set!"); - let creds = Credentials::new(&key, "hoppinworld.net"); - let from = EmailAddress::name_address("HoppinWorld", "noreply@hoppinworld.net"); + let creds = Credentials::new(&key, "mail.hoppinworld.net"); + let from = EmailAddress::name_address("HoppinWorld", "noreply@mail.hoppinworld.net"); let msg = Message { - to: vec![EmailAddress::address("jojolepromain@gmail.com")], + to: vec![EmailAddress::address(&passreset.email)], cc: vec![], bcc: vec![], subject: String::from("HoppinWorld Password Reset"), @@ -470,3 +470,23 @@ pub fn list_maps(db: DbConn) -> Result<Json<Vec<Map>>, ReturnStatus> { } } } + +/// Checks if a given user token is valid +#[post("/validatetoken", format = "application/json", data = "<token>")] +pub fn validate_token(token: Json<String>, db: DbConn) -> Result<Json<bool>, ReturnStatus> { + // Pass from email + let user = user_from_token(&db, &token); + + match user.as_ref() { + Ok(_) => { + Ok(Json(true)) + } + Err(diesel::result::Error::NotFound) => { + Ok(Json(false)) + } + Err(e) => { + error!("Failed to get user from email: {:?}", e); + return Err(ReturnStatus::new(Status::InternalServerError)); + } + } +} diff --git a/src/main.rs b/src/main.rs @@ -51,6 +51,7 @@ fn main() { list_maps, map_info, request_password_reset, + validate_token, ], ) .attach(options)