feat: use resp message from server
This commit is contained in:
parent
6803ff9cca
commit
3097169143
|
|
@ -17,11 +17,10 @@ body {
|
|||
.button {
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
margin: 4px 4px;
|
||||
margin: 4px;
|
||||
padding: 4px 8px;
|
||||
font-size: 16px;
|
||||
cursor: pointer;
|
||||
margin: auto;
|
||||
}
|
||||
|
||||
.purple:hover {
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
use std::borrow::Cow;
|
||||
|
||||
use axum::{response::IntoResponse, Json};
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
|
|
@ -6,7 +8,7 @@ where
|
|||
T: serde::Serialize,
|
||||
{
|
||||
pub code: i16,
|
||||
pub message: String,
|
||||
pub message: Cow<'static, str>,
|
||||
pub data: T,
|
||||
}
|
||||
|
||||
|
|
@ -14,10 +16,10 @@ impl<T> WebApiResponse<T>
|
|||
where
|
||||
T: serde::Serialize,
|
||||
{
|
||||
pub fn new(data: T) -> Self {
|
||||
pub fn new(message: Cow<'static, str>, data: T) -> Self {
|
||||
Self {
|
||||
code: 0,
|
||||
message: "success".to_owned(),
|
||||
message,
|
||||
data,
|
||||
}
|
||||
}
|
||||
|
|
@ -37,6 +39,12 @@ where
|
|||
T: serde::Serialize,
|
||||
{
|
||||
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, ())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -228,7 +228,7 @@ pub async fn login_handler(
|
|||
State(state): State<AppState>,
|
||||
jar: CookieJar,
|
||||
Form(param): Form<WebApiLoginRequest>,
|
||||
) -> Result<(CookieJar, HeaderMap), WebApiError<'static>> {
|
||||
) -> Result<(CookieJar, HeaderMap, WebApiResponse<()>), WebApiError<'static>> {
|
||||
// Attempt to sign in with email and password
|
||||
let token_res = state
|
||||
.gotrue_client
|
||||
|
|
@ -246,7 +246,7 @@ pub async fn login_handler(
|
|||
GoTrueError::ClientError(client_err) => {
|
||||
match (
|
||||
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
|
||||
("invalid_grant", Some("Invalid login credentials")) => {
|
||||
|
|
@ -261,10 +261,16 @@ pub async fn login_handler(
|
|||
SignUpResponse::Authenticated(token) => {
|
||||
session_login(State(state), token, jar).await
|
||||
},
|
||||
|
||||
SignUpResponse::NotAuthenticated(user) => match user.identities {
|
||||
Some(_identities) => todo!(), // new user
|
||||
None => Err(err.into()), // user exists but sign in password not correct
|
||||
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
|
||||
},
|
||||
},
|
||||
Err(err) => Err(err.into()),
|
||||
|
|
@ -313,7 +319,7 @@ async fn session_login(
|
|||
State(state): State<AppState>,
|
||||
token: GotrueTokenResponse,
|
||||
jar: CookieJar,
|
||||
) -> Result<(CookieJar, HeaderMap), WebApiError<'static>> {
|
||||
) -> Result<(CookieJar, HeaderMap, WebApiResponse<()>), WebApiError<'static>> {
|
||||
let new_session_id = uuid::Uuid::new_v4();
|
||||
let new_session = session::UserSession::new(new_session_id.to_string(), token);
|
||||
state.session_store.put_user_session(&new_session).await?;
|
||||
|
|
@ -321,5 +327,6 @@ async fn session_login(
|
|||
Ok((
|
||||
jar.add(new_session_cookie(new_session_id)),
|
||||
htmx_redirect("/web/home"),
|
||||
().into(),
|
||||
))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,7 +22,8 @@
|
|||
});
|
||||
</script>
|
||||
|
||||
<!-- prettier-ignore -->
|
||||
{% include "components/logout.html" %}
|
||||
<div class="button yellow" id="logoutBtn" hx-post="/web-api/logout">
|
||||
Logout
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -1,3 +0,0 @@
|
|||
<button class="button yellow" id="logoutBtn" hx-post="/web-api/logout">
|
||||
Logout
|
||||
</button>
|
||||
|
|
@ -25,7 +25,8 @@
|
|||
</script>
|
||||
{% endif %}
|
||||
|
||||
<!-- prettier-ignore -->
|
||||
{% include "components/logout.html" %}
|
||||
<div class="button yellow" id="logoutBtn" hx-post="/web-api/logout">
|
||||
Logout
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@
|
|||
const xhr = detail.xhr;
|
||||
displayHttpFail(xhr.status, xhr.statusText, xhr.responseText);
|
||||
} else if (detail.target.id === "none") {
|
||||
displaySuccess("Success");
|
||||
displaySuccess(JSON.parse(detail.xhr.responseText).message);
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@
|
|||
</div>
|
||||
|
||||
<h3>Email Login</h3>
|
||||
<form hx-post="/web-api/login">
|
||||
<form hx-post="/web-api/login" hx-target="#none">
|
||||
<table style="width: 100%">
|
||||
<tr>
|
||||
<td>Email:</td>
|
||||
|
|
|
|||
Loading…
Reference in New Issue