Skip to content

Commit

Permalink
Try making session one-shot
Browse files Browse the repository at this point in the history
  • Loading branch information
hongquan committed Sep 9, 2023
1 parent 33a238e commit 1497543
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
Binary file added admin/bun.lockb
Binary file not shown.
5 changes: 4 additions & 1 deletion admin/src/views/LoginPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
<div class='flex items-center h-5'>
<input
id='remember'
v-model='remember'
name='remember'
type='checkbox'
class='w-4 h-4 border border-gray-300 rounded bg-gray-50 focus:ring-3 focus:ring-blue-300 dark:bg-gray-700 dark:border-gray-600 dark:focus:ring-blue-600 dark:ring-offset-gray-800 dark:focus:ring-offset-gray-800'
>
Expand Down Expand Up @@ -82,12 +84,13 @@ const route = useRoute()
const store = useStore()
const email = ref('')
const password = ref('')
const remember = ref(false)
const isSubmitting = ref(false)
async function onSubmit() {
isSubmitting.value = true
try {
const resp = await kyClient.post(API_LOGIN, { json: { email: email.value, password: password.value } }).json()
const resp = await kyClient.post(API_LOGIN, { json: { email: email.value, password: password.value, remember_me: remember.value } }).json()
const user = UserSchema.parse(resp)
store.user = user
toast.success('Login successfully')
Expand Down
3 changes: 2 additions & 1 deletion src/auth/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub struct LoginReqData {
pub email: String,
#[serde(serialize_with = "expose_secret")]
pub password: Secret<String>,
pub remember_me: bool,
}

// The `#[derive(Validate)]` macro failed to generate code due to the type of `password` field.
Expand Down Expand Up @@ -36,6 +37,6 @@ impl validify::Validate for LoginReqData {

pub fn validate_password(value: &Secret<String>) -> Result<(), ValidationError> {
(value.expose_secret().len() >= 8).then_some(()).ok_or_else(|| {
ValidationError::new_field("password", "Too short")
ValidationError::new_field("password", "too-short")
})
}
31 changes: 18 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,25 @@ mod errors;
mod front;
mod models;
mod stores;
mod thingsup;
mod types;
mod utils;
mod thingsup;

use std::net::SocketAddr;
use std::{fs, path::Path};
use std::os::unix::fs::PermissionsExt;
use std::{fs, path::Path};

use axum::routing::Router;
use axum_login::{axum_sessions::SessionLayer, AuthLayer};
use axum_login::AuthLayer;
use axum_sessions::{PersistencePolicy, SessionLayer};
use clap::Parser;
use hyperlocal::UnixServerExt;
use miette::{miette, IntoDiagnostic};
use tower_http::trace::TraceLayer;
use hyperlocal::UnixServerExt;

use auth::store::EdgeDbStore;
use thingsup::{config_jinja, config_logging, get_binding_addr, get_listening_addr, AppOptions};
use types::AppState;
use thingsup::{AppOptions, config_jinja, config_logging, get_listening_addr, get_binding_addr};

#[tokio::main]
async fn main() -> miette::Result<()> {
Expand All @@ -43,7 +44,10 @@ async fn main() -> miette::Result<()> {
db: client.clone(),
jinja,
};
let session_layer = SessionLayer::new(redis_store, &secret_bytes).with_secure(false);
let session_layer = SessionLayer::new(redis_store, &secret_bytes)
.with_session_ttl(None)
.with_persistence_policy(PersistencePolicy::ChangedOnly)
.with_secure(false);
let user_store: EdgeDbStore<models::User> = EdgeDbStore::new(client);
let auth_layer = AuthLayer::new(user_store, &secret_bytes);

Expand All @@ -60,20 +64,21 @@ async fn main() -> miette::Result<()> {
.layer(TraceLayer::new_for_http());

let addr_result = match &app_opts.bind {
Some(saddr) => {
get_binding_addr(saddr)
},
Some(saddr) => get_binding_addr(saddr),
None => {
let port = conf::get_listening_port(&config);
Ok(SocketAddr::from((get_listening_addr(), port)))
},
}
};
let main_service = app.into_make_service();
match addr_result {
Ok(addr) => {
tracing::info!("Listening on http://{}", addr);
axum::Server::bind(&addr).serve(main_service).await.into_diagnostic()?;
},
axum::Server::bind(&addr)
.serve(main_service)
.await
.into_diagnostic()?;
}
_ => {
let original_bind = app_opts.bind.unwrap_or("web.sock".into());
let path = Path::new(&original_bind);
Expand All @@ -85,7 +90,7 @@ async fn main() -> miette::Result<()> {
let perm = fs::Permissions::from_mode(0o664);
fs::set_permissions(path, perm).into_diagnostic()?;
server.serve(main_service).await.into_diagnostic()?;
},
}
}
Ok(())
}

0 comments on commit 1497543

Please sign in to comment.