only show register if no users, otherwise show only login

This commit is contained in:
Wavering Ana 2025-01-29 20:47:04 -05:00
parent daa1323b88
commit 3585ca70e8
6 changed files with 116 additions and 185 deletions

View file

@ -16,6 +16,7 @@ use argon2::{Argon2, PasswordHash, PasswordHasher};
use jsonwebtoken::{encode, EncodingKey, Header};
use lazy_static::lazy_static;
use regex::Regex;
use serde_json::json;
use sqlx::{Postgres, Sqlite};
lazy_static! {
@ -690,3 +691,24 @@ pub async fn get_link_sources(
Ok(HttpResponse::Ok().json(sources))
}
pub async fn check_first_user(state: web::Data<AppState>) -> Result<impl Responder, AppError> {
let user_count = match &state.db {
DatabasePool::Postgres(pool) => {
sqlx::query_as::<Postgres, (i64,)>("SELECT COUNT(*)::bigint FROM users")
.fetch_one(pool)
.await?
.0
}
DatabasePool::Sqlite(pool) => {
sqlx::query_as::<Sqlite, (i64,)>("SELECT COUNT(*) FROM users")
.fetch_one(pool)
.await?
.0
}
};
Ok(HttpResponse::Ok().json(json!({
"isFirstUser": user_count == 0
})))
}

View file

@ -72,6 +72,10 @@ async fn main() -> Result<()> {
)
.route("/auth/register", web::post().to(handlers::register))
.route("/auth/login", web::post().to(handlers::login))
.route(
"/auth/check-first-user",
web::get().to(handlers::check_first_user),
)
.route("/health", web::get().to(handlers::health_check)),
)
.service(web::resource("/{short_code}").route(web::get().to(handlers::redirect_to_url)))