Implemented user authentication.
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
use cfg_if::cfg_if;
|
||||
use leptos::*;
|
||||
|
||||
cfg_if! { if #[cfg(feature = "ssr")] {
|
||||
use sqlx::{query_as, Error, PgPool, query};
|
||||
use actix_session::*;
|
||||
use leptos_actix::{extract, redirect};
|
||||
use crate::backend::data::User;
|
||||
|
||||
pub async fn has_admin_user(pool: &PgPool) -> Result<bool, Error> {
|
||||
let count: (i64,) = query_as(r#"SELECT COUNT(id) FROM "user" WHERE admin = $1"#)
|
||||
.bind(true)
|
||||
.fetch_one(pool)
|
||||
.await?;
|
||||
|
||||
Ok(count.0 > 0)
|
||||
}
|
||||
|
||||
pub async fn create_admin(pool: &PgPool) -> Result<(), Error> {
|
||||
if !has_admin_user(pool).await? {
|
||||
let pwd = pwhash::bcrypt::hash("admin");
|
||||
query(r#"INSERT INTO "user"(login, password, full_name, admin) VALUES($1, $2, $3, $4)"#)
|
||||
.bind("admin")
|
||||
.bind(pwd.unwrap())
|
||||
.bind("Admin User")
|
||||
.bind(true)
|
||||
.execute(pool).await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn user_from_login(pool: &PgPool, login: &str) -> Result<User, Error> {
|
||||
let usr = query_as::<_, User>(r#"SELECT * FROM "user" WHERE login=$1"#)
|
||||
.bind(login)
|
||||
.fetch_one(pool).await?;
|
||||
Ok(usr)
|
||||
}
|
||||
|
||||
pub async fn logged_in_user() -> Option<User> {
|
||||
extract(|session: Session| async move {
|
||||
session.get::<User>("user").unwrap_or(None)
|
||||
}).await.unwrap_or(None)
|
||||
}
|
||||
|
||||
pub async fn is_logged_in() -> bool {
|
||||
logged_in_user().await.is_some()
|
||||
}
|
||||
|
||||
pub async fn is_admin() -> bool {
|
||||
if let Some(user) = logged_in_user().await {
|
||||
user.admin
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
}}
|
||||
|
||||
#[server(Login, "/api")]
|
||||
pub async fn login(username: String, password: String) -> Result<(), ServerFnError> {
|
||||
use crate::backend::AppData;
|
||||
use actix_session::*;
|
||||
use actix_web::web::Data;
|
||||
use leptos_actix::extract;
|
||||
|
||||
let pool = extract(|data: Data<AppData>| async move { data.db_pool().clone() }).await?;
|
||||
|
||||
let user = user_from_login(&pool, &username).await?;
|
||||
|
||||
if pwhash::bcrypt::verify(password, &user.password) {
|
||||
extract(|session: Session| async move {
|
||||
let _ = session.insert("user", user);
|
||||
})
|
||||
.await?;
|
||||
|
||||
redirect("/admin");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
Err(ServerFnError::ServerError("Bad login".to_string()))
|
||||
}
|
||||
|
||||
#[server]
|
||||
pub async fn logout() -> Result<(), ServerFnError> {
|
||||
extract(|session: Session| async move {
|
||||
session.clear();
|
||||
}).await?;
|
||||
|
||||
redirect("/login");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[server]
|
||||
pub async fn auth_check() -> Result<bool, ServerFnError> {
|
||||
Ok(is_logged_in().await)
|
||||
}
|
||||
|
||||
#[server]
|
||||
pub async fn admin_check() -> Result<bool, ServerFnError> {
|
||||
Ok(is_admin().await)
|
||||
}
|
||||
Reference in New Issue
Block a user