feat: use resp message from server

This commit is contained in:
Fu Zi Xiang 2023-11-09 14:40:05 +08:00
parent 6803ff9cca
commit 3097169143
No known key found for this signature in database
8 changed files with 34 additions and 21 deletions

View File

@ -17,11 +17,10 @@ body {
.button { .button {
border: none; border: none;
border-radius: 4px; border-radius: 4px;
margin: 4px 4px; margin: 4px;
padding: 4px 8px; padding: 4px 8px;
font-size: 16px; font-size: 16px;
cursor: pointer; cursor: pointer;
margin: auto;
} }
.purple:hover { .purple:hover {

View File

@ -1,3 +1,5 @@
use std::borrow::Cow;
use axum::{response::IntoResponse, Json}; use axum::{response::IntoResponse, Json};
#[derive(serde::Serialize)] #[derive(serde::Serialize)]
@ -6,7 +8,7 @@ where
T: serde::Serialize, T: serde::Serialize,
{ {
pub code: i16, pub code: i16,
pub message: String, pub message: Cow<'static, str>,
pub data: T, pub data: T,
} }
@ -14,10 +16,10 @@ impl<T> WebApiResponse<T>
where where
T: serde::Serialize, T: serde::Serialize,
{ {
pub fn new(data: T) -> Self { pub fn new(message: Cow<'static, str>, data: T) -> Self {
Self { Self {
code: 0, code: 0,
message: "success".to_owned(), message,
data, data,
} }
} }
@ -37,6 +39,12 @@ where
T: serde::Serialize, T: serde::Serialize,
{ {
fn from(data: T) -> Self { fn from(data: T) -> Self {
Self::new(data) Self::new("success".into(), data)
}
}
impl WebApiResponse<()> {
pub fn from_str(message: Cow<'static, str>) -> Self {
Self::new(message, ())
} }
} }

View File

@ -228,7 +228,7 @@ pub async fn login_handler(
State(state): State<AppState>, State(state): State<AppState>,
jar: CookieJar, jar: CookieJar,
Form(param): Form<WebApiLoginRequest>, Form(param): Form<WebApiLoginRequest>,
) -> Result<(CookieJar, HeaderMap), WebApiError<'static>> { ) -> Result<(CookieJar, HeaderMap, WebApiResponse<()>), WebApiError<'static>> {
// Attempt to sign in with email and password // Attempt to sign in with email and password
let token_res = state let token_res = state
.gotrue_client .gotrue_client
@ -246,7 +246,7 @@ pub async fn login_handler(
GoTrueError::ClientError(client_err) => { GoTrueError::ClientError(client_err) => {
match ( match (
client_err.error.as_str(), client_err.error.as_str(),
client_err.error_description.as_ref().map(|s| s.as_str()), client_err.error_description.as_deref(),
) { ) {
// Email not exist or wrong password // Email not exist or wrong password
("invalid_grant", Some("Invalid login credentials")) => { ("invalid_grant", Some("Invalid login credentials")) => {
@ -261,9 +261,15 @@ pub async fn login_handler(
SignUpResponse::Authenticated(token) => { SignUpResponse::Authenticated(token) => {
session_login(State(state), token, jar).await session_login(State(state), token, jar).await
}, },
SignUpResponse::NotAuthenticated(user) => match user.identities { SignUpResponse::NotAuthenticated(user) => match user.identities {
Some(_identities) => todo!(), // new user Some(_identities) => {
// new user, awaiting email verification
Ok((
jar,
HeaderMap::new(),
WebApiResponse::<()>::from_str("Email Verification Sent".into()),
))
},
None => Err(err.into()), // user exists but sign in password not correct None => Err(err.into()), // user exists but sign in password not correct
}, },
}, },
@ -313,7 +319,7 @@ async fn session_login(
State(state): State<AppState>, State(state): State<AppState>,
token: GotrueTokenResponse, token: GotrueTokenResponse,
jar: CookieJar, jar: CookieJar,
) -> Result<(CookieJar, HeaderMap), WebApiError<'static>> { ) -> Result<(CookieJar, HeaderMap, WebApiResponse<()>), WebApiError<'static>> {
let new_session_id = uuid::Uuid::new_v4(); let new_session_id = uuid::Uuid::new_v4();
let new_session = session::UserSession::new(new_session_id.to_string(), token); let new_session = session::UserSession::new(new_session_id.to_string(), token);
state.session_store.put_user_session(&new_session).await?; state.session_store.put_user_session(&new_session).await?;
@ -321,5 +327,6 @@ async fn session_login(
Ok(( Ok((
jar.add(new_session_cookie(new_session_id)), jar.add(new_session_cookie(new_session_id)),
htmx_redirect("/web/home"), htmx_redirect("/web/home"),
().into(),
)) ))
} }

View File

@ -22,7 +22,8 @@
}); });
</script> </script>
<!-- prettier-ignore --> <div class="button yellow" id="logoutBtn" hx-post="/web-api/logout">
{% include "components/logout.html" %} Logout
</div>
</div> </div>
</div> </div>

View File

@ -1,3 +0,0 @@
<button class="button yellow" id="logoutBtn" hx-post="/web-api/logout">
Logout
</button>

View File

@ -25,7 +25,8 @@
</script> </script>
{% endif %} {% endif %}
<!-- prettier-ignore --> <div class="button yellow" id="logoutBtn" hx-post="/web-api/logout">
{% include "components/logout.html" %} Logout
</div>
</div> </div>
</div> </div>

View File

@ -30,7 +30,7 @@
const xhr = detail.xhr; const xhr = detail.xhr;
displayHttpFail(xhr.status, xhr.statusText, xhr.responseText); displayHttpFail(xhr.status, xhr.statusText, xhr.responseText);
} else if (detail.target.id === "none") { } else if (detail.target.id === "none") {
displaySuccess("Success"); displaySuccess(JSON.parse(detail.xhr.responseText).message);
} }
}); });
</script> </script>

View File

@ -20,7 +20,7 @@
</div> </div>
<h3>Email Login</h3> <h3>Email Login</h3>
<form hx-post="/web-api/login"> <form hx-post="/web-api/login" hx-target="#none">
<table style="width: 100%"> <table style="width: 100%">
<tr> <tr>
<td>Email:</td> <td>Email:</td>