Fixes the infinite loop and refactores some statechanges into useEffect hooks
This commit is contained in:
@@ -24,13 +24,20 @@ pub enum ApiResponseVariant {
|
||||
// Flash(Flash<Redirect>)
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct UserWithToken {
|
||||
#[serde(flatten)]
|
||||
pub user: schema::User,
|
||||
pub jwt: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct ApiResponse {
|
||||
result: Cow<'static, str>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
message: Option<Cow<'static, str>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
jwt: Option<Cow<'static, str>>,
|
||||
user: Option<UserWithToken>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
gamenights: Option<Vec<schema::GameNight>>,
|
||||
}
|
||||
@@ -42,7 +49,7 @@ impl ApiResponse {
|
||||
const SUCCES: Self = Self {
|
||||
result: Self::SUCCES_RESULT,
|
||||
message: None,
|
||||
jwt: None,
|
||||
user: None,
|
||||
gamenights: None,
|
||||
};
|
||||
|
||||
@@ -50,16 +57,19 @@ impl ApiResponse {
|
||||
Self {
|
||||
result: Self::FAILURE_RESULT,
|
||||
message: Some(Cow::Owned(message)),
|
||||
jwt: None,
|
||||
user: None,
|
||||
gamenights: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn login_response(jwt: String) -> Self {
|
||||
fn login_response(user: schema::User, jwt: String) -> Self {
|
||||
Self {
|
||||
result: Self::SUCCES_RESULT,
|
||||
message: None,
|
||||
jwt: Some(Cow::Owned(jwt)),
|
||||
user: Some(UserWithToken {
|
||||
user: user,
|
||||
jwt: jwt
|
||||
}),
|
||||
gamenights: None,
|
||||
}
|
||||
}
|
||||
@@ -68,7 +78,7 @@ impl ApiResponse {
|
||||
Self {
|
||||
result: Self::SUCCES_RESULT,
|
||||
message: None,
|
||||
jwt: None,
|
||||
user: None,
|
||||
gamenights: Some(gamenights),
|
||||
}
|
||||
}
|
||||
@@ -190,10 +200,11 @@ pub async fn login_post_json(
|
||||
))));
|
||||
}
|
||||
|
||||
let user = login_result.user.unwrap();
|
||||
let my_claims = Claims {
|
||||
exp: Utc::now().timestamp() + chrono::Duration::days(7).num_seconds(),
|
||||
uid: login_result.id.unwrap(),
|
||||
role: login_result.role.unwrap(),
|
||||
uid: user.id,
|
||||
role: user.role,
|
||||
};
|
||||
|
||||
let secret = &config.inner().jwt_secret;
|
||||
@@ -202,7 +213,7 @@ pub async fn login_post_json(
|
||||
&my_claims,
|
||||
&EncodingKey::from_secret(secret.as_bytes()),
|
||||
) {
|
||||
Ok(token) => ApiResponseVariant::Value(json!(ApiResponse::login_response(token))),
|
||||
Ok(token) => ApiResponseVariant::Value(json!(ApiResponse::login_response(user, token))),
|
||||
Err(error) => {
|
||||
ApiResponseVariant::Value(json!(ApiResponse::error(error.to_string())))
|
||||
}
|
||||
|
||||
@@ -175,25 +175,19 @@ pub async fn login(conn: DbConn, login: Login) -> Result<LoginResult, DatabaseEr
|
||||
.verify_password(&login.password.as_bytes(), &parsed_hash)
|
||||
.is_ok()
|
||||
{
|
||||
let role: Role = match user::table
|
||||
.filter(user::id.eq(id))
|
||||
.select(user::role)
|
||||
.first(c)
|
||||
{
|
||||
Ok(role) => role,
|
||||
Err(error) => return Err(DatabaseError::Query(error.to_string())),
|
||||
let user: User = match user::table.find(id).first(c) {
|
||||
Ok(user) => user,
|
||||
Err(error) => return Err(DatabaseError::Query(error.to_string()))
|
||||
};
|
||||
|
||||
Ok(LoginResult {
|
||||
result: true,
|
||||
id: Some(id),
|
||||
role: Some(role),
|
||||
user : Some(user)
|
||||
})
|
||||
} else {
|
||||
Ok(LoginResult {
|
||||
result: false,
|
||||
id: None,
|
||||
role: None,
|
||||
user: None
|
||||
})
|
||||
}
|
||||
})
|
||||
@@ -251,7 +245,7 @@ pub async fn run_migrations(rocket: Rocket<Build>) -> Rocket<Build> {
|
||||
rocket
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, DbEnum, Clone)]
|
||||
#[derive(Debug, Serialize, Deserialize, DbEnum, Clone, Copy)]
|
||||
pub enum Role {
|
||||
Admin,
|
||||
User,
|
||||
@@ -318,6 +312,5 @@ pub struct Login {
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct LoginResult {
|
||||
pub result: bool,
|
||||
pub id: Option<i32>,
|
||||
pub role: Option<Role>,
|
||||
pub user: Option<User>,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user