return user from login call

This commit is contained in:
Ilion Beyst 2022-01-04 23:24:31 +01:00
parent 32131da678
commit 9ccea2ea17
2 changed files with 25 additions and 22 deletions

View file

@ -5,6 +5,7 @@ use axum::extract::{FromRequest, RequestParts, TypedHeader};
use axum::headers::authorization::Bearer; use axum::headers::authorization::Bearer;
use axum::headers::Authorization; use axum::headers::Authorization;
use axum::http::StatusCode; use axum::http::StatusCode;
use axum::response::{Headers, IntoResponse, Response};
use axum::{async_trait, Json}; use axum::{async_trait, Json};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -70,10 +71,7 @@ pub struct LoginParams {
pub password: String, pub password: String,
} }
pub async fn login( pub async fn login(conn: DatabaseConnection, params: Json<LoginParams>) -> Response {
conn: DatabaseConnection,
params: Json<LoginParams>,
) -> Result<String, StatusCode> {
let credentials = Credentials { let credentials = Credentials {
username: &params.username, username: &params.username,
password: &params.password, password: &params.password,
@ -82,10 +80,13 @@ pub async fn login(
let authenticated = users::authenticate_user(&credentials, &conn); let authenticated = users::authenticate_user(&credentials, &conn);
match authenticated { match authenticated {
None => Err(StatusCode::FORBIDDEN), None => StatusCode::FORBIDDEN.into_response(),
Some(user) => { Some(user) => {
let session = sessions::create_session(&user, &conn); let session = sessions::create_session(&user, &conn);
Ok(session.token) let user_data: UserData = user.into();
let headers = Headers(vec![("Token", &session.token)]);
(headers, Json(user_data)).into_response()
} }
} }
} }

View file

@ -5,8 +5,8 @@
let username: string | undefined; let username: string | undefined;
let password: string | undefined; let password: string | undefined;
const onSubmit = () => { async function login() {
fetch("/api/login", { let response = await fetch("/api/login", {
method: "POST", method: "POST",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
@ -15,21 +15,23 @@
username, username,
password, password,
}), }),
}) });
.then((response) => {
if (!response.ok) { if (!response.ok) {
throw Error(response.statusText); throw Error(response.statusText);
} }
return response.text();
}) let token = response.headers.get("Token");
.then((token) => { set_session_token(token);
set_session_token(token);
goto("/"); let user = await response.json();
});
}; goto("/");
}
function loggedIn(): boolean { function loggedIn(): boolean {
return get_session_token() != null; let session = get_session_token();
return session !== null && session !== undefined;
} }
</script> </script>
@ -37,7 +39,7 @@
you are logged in you are logged in
{/if} {/if}
<form on:submit|preventDefault={onSubmit}> <form on:submit|preventDefault={login}>
<label for="username">Username</label> <label for="username">Username</label>
<input name="username" bind:value={username} /> <input name="username" bind:value={username} />
<label for="password">Password</label> <label for="password">Password</label>