diff --git a/src/callers/login.rs b/src/callers/login.rs index 07a0959..7b234b1 100644 --- a/src/callers/login.rs +++ b/src/callers/login.rs @@ -189,8 +189,7 @@ pub async fn user_login( let key = textsender_models::envy::environment::get_secret_key() .await .value; - let cst = - token_stuff::create_token(&key, &user.id).unwrap(); + let cst = token_stuff::create_token(&key, &user.id).unwrap(); if token_stuff::verify_token(&key, &cst.access_token) { let current_time = time::OffsetDateTime::now_utc(); @@ -355,7 +354,7 @@ pub async fn service_user_login( textsender_models::token::TOKEN_TYPE, ), issued_at: cst.issued, - expires_in: cst.expires_in + expires_in: cst.expires_in, }], }), ) @@ -440,12 +439,13 @@ pub async fn refresh_token( if token_stuff::verify_token(&key, &payload.access_token) { match token_stuff::extract_id_from_token(&key, &payload.access_token) { Ok(id) => { - let generate_service_token = |id, key| -> Option { - match token_stuff::create_service_refresh_token(key, id) { - Ok(cst) => Some(cst), - Err(_err) => None, - } - }; + let generate_service_token = + |id, key| -> Option { + match token_stuff::create_service_refresh_token(key, id) { + Ok(cst) => Some(cst), + Err(_err) => None, + } + }; let mut response = response::RefreshTokenResponse { message: String::new(), @@ -453,9 +453,33 @@ pub async fn refresh_token( }; match repo::user::get_with_id(&pool, &id).await { - Ok(_user) => { - match generate_service_token(&id, &key) { - Some(cst) => { + Ok(_user) => match generate_service_token(&id, &key) { + Some(cst) => { + response.message = + String::from(super::messages::SUCCESSFUL_MESSAGE); + let lr = textsender_models::token::LoginResult { + user_id: id, + access_token: cst.access_token, + issued_at: cst.issued, + expires_in: cst.expires_in, + token_type: String::from(textsender_models::token::TOKEN_TYPE), + }; + response.data.push(lr); + (axum::http::StatusCode::OK, axum::Json(response)) + } + None => ( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, + axum::Json(response::RefreshTokenResponse { + message: String::from("Refresh token not generated"), + data: Vec::new(), + }), + ), + }, + Err(err) => { + println!("Unable to find user, checking service user: {err:?}"); + match repo::service::get(&pool, &id).await { + Ok(_service_user) => match generate_service_token(&id, &key) { + Some(cst) => { response.message = String::from(super::messages::SUCCESSFUL_MESSAGE); let lr = textsender_models::token::LoginResult { @@ -469,46 +493,15 @@ pub async fn refresh_token( }; response.data.push(lr); (axum::http::StatusCode::OK, axum::Json(response)) + } + None => ( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, + axum::Json(response::RefreshTokenResponse { + message: String::from("Issued at not returned"), + data: Vec::new(), + }), + ), }, - None => ( - axum::http::StatusCode::INTERNAL_SERVER_ERROR, - axum::Json(response::RefreshTokenResponse { - message: String::from("Refresh token not generated"), - data: Vec::new(), - }), - ), - } - } - Err(err) => { - println!("Unable to find user, checking service user: {err:?}"); - match repo::service::get(&pool, &id).await { - Ok(_service_user) => { - match generate_service_token(&id, &key) { - Some(cst) => { - response.message = String::from( - super::messages::SUCCESSFUL_MESSAGE, - ); - let lr = textsender_models::token::LoginResult { - user_id: id, - access_token: cst.access_token, - issued_at: cst.issued, - expires_in: cst.expires_in, - token_type: String::from( - textsender_models::token::TOKEN_TYPE, - ), - }; - response.data.push(lr); - (axum::http::StatusCode::OK, axum::Json(response)) - } - None => ( - axum::http::StatusCode::INTERNAL_SERVER_ERROR, - axum::Json(response::RefreshTokenResponse { - message: String::from("Issued at not returned"), - data: Vec::new(), - }), - ), - } - } Err(err) => ( axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response::RefreshTokenResponse {