1 Commits

Author SHA1 Message Date
Jayden Pyles
8703f706a1 feat: add in optional registration (#65)
* feat: add in optional registration

* fix: issue with registration var

* fix: issue with registration var

* fix: issue with registration var
2025-05-11 11:11:19 -05:00
6 changed files with 88 additions and 8 deletions

View File

@@ -67,4 +67,4 @@ async def ai(c: AI):
@ai_router.get("/ai/check")
async def check():
return JSONResponse(content=bool(open_ai_key or llama_model))
return JSONResponse(content={"ai_enabled": bool(open_ai_key or llama_model)})

View File

@@ -1,5 +1,6 @@
# STL
from datetime import timedelta
import os
# PDM
from fastapi import Depends, APIRouter, HTTPException, status
@@ -61,3 +62,8 @@ async def create_user(user: UserCreate):
@auth_router.get("/auth/users/me", response_model=User)
async def read_users_me(current_user: User = Depends(get_current_user)):
return current_user
@auth_router.get("/auth/check")
async def check_auth():
return {"registration": os.environ.get("REGISTRATION_ENABLED", "True") == "True"}

View File

@@ -1,6 +1,9 @@
import os
from api.backend.database.common import connect, QUERIES
import logging
from api.backend.auth.auth_utils import get_password_hash
LOG = logging.getLogger(__name__)
@@ -12,4 +15,29 @@ def init_database():
LOG.info(f"Executing query: {query}")
_ = cursor.execute(query)
if os.environ.get("REGISTRATION_ENABLED", "True") == "False":
default_user_email = os.environ.get("DEFAULT_USER_EMAIL")
default_user_password = os.environ.get("DEFAULT_USER_PASSWORD")
default_user_full_name = os.environ.get("DEFAULT_USER_FULL_NAME")
if (
not default_user_email
or not default_user_password
or not default_user_full_name
):
LOG.error(
"DEFAULT_USER_EMAIL, DEFAULT_USER_PASSWORD, or DEFAULT_USER_FULL_NAME is not set!"
)
exit(1)
query = "INSERT INTO users (email, hashed_password, full_name) VALUES (?, ?, ?)"
_ = cursor.execute(
query,
(
default_user_email,
get_password_hash(default_user_password),
default_user_full_name,
),
)
cursor.close()

View File

@@ -48,14 +48,14 @@ export const checkAI = async (
) => {
const token = Cookies.get("token");
try {
const response = await fetch("/api/ai/check", {
const response = await fetch("/api/check", {
headers: {
"content-type": "application/json",
Authorization: `Bearer ${token}`,
},
});
const data = await response.json();
setAiEnabled(data);
setAiEnabled(data.ai_enabled);
} catch (error) {
console.error("Error fetching jobs:", error);
throw error;

View File

@@ -17,12 +17,21 @@ export default async function handler(
}
);
const checksResponse = await fetch(
`${global.process.env.NEXT_PUBLIC_API_URL}/api/auth/check`,
{
method: "GET",
headers,
}
);
if (!response.ok) {
throw new Error(`Error: ${response.statusText}`);
}
const result = await response.json();
res.status(200).json(result);
const checksResult = await checksResponse.json();
res.status(200).json({ ...result, ...checksResult });
} catch (error) {
console.error("Error submitting scrape job:", error);
res.status(500).json({ error: "Internal Server Error" });

View File

@@ -1,6 +1,6 @@
"use client";
import React, { useState } from "react";
import React, { useEffect, useState } from "react";
import axios from "axios";
import { Button, TextField, Typography, Box } from "@mui/material";
import { useTheme } from "@mui/material/styles";
@@ -18,7 +18,16 @@ const AuthForm: React.FC = () => {
const theme = useTheme();
const router = useRouter();
const { login } = useAuth();
const [registrationEnabled, setRegistrationEnabled] = useState<boolean>(true);
const checkRegistrationEnabled = async () => {
const response = await axios.get(`/api/check`);
setRegistrationEnabled(response.data.registration);
};
useEffect(() => {
checkRegistrationEnabled();
}, []);
const handleSubmit = async (event: React.FormEvent) => {
event.preventDefault();
try {
@@ -124,9 +133,37 @@ const AuthForm: React.FC = () => {
>
{mode.charAt(0).toUpperCase() + mode.slice(1)}
</Button>
<Button onClick={toggleMode} fullWidth variant="text" color="primary">
{mode === "login" ? "No Account? Sign up" : "Login"}
</Button>
{registrationEnabled && (
<Button
onClick={toggleMode}
fullWidth
variant="text"
color="primary"
>
{mode === "login" ? "No Account? Sign up" : "Login"}
</Button>
)}
{!registrationEnabled && (
<div
style={{
marginTop: 10,
width: "100%",
textAlign: "center",
border: "1px solid #ccc",
backgroundColor: "#f8f8f8",
padding: 8,
borderRadius: 4,
display: "flex",
justifyContent: "center",
alignItems: "center",
}}
>
<Typography variant="body2" color="text">
Registration has been disabled
</Typography>
</div>
)}
</Box>
</Box>
</Box>