mirror of
https://github.com/jaypyles/Scraperr.git
synced 2025-12-12 10:45:58 +00:00
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
This commit is contained in:
@@ -67,4 +67,4 @@ async def ai(c: AI):
|
|||||||
|
|
||||||
@ai_router.get("/ai/check")
|
@ai_router.get("/ai/check")
|
||||||
async def 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)})
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
# STL
|
# STL
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
import os
|
||||||
|
|
||||||
# PDM
|
# PDM
|
||||||
from fastapi import Depends, APIRouter, HTTPException, status
|
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)
|
@auth_router.get("/auth/users/me", response_model=User)
|
||||||
async def read_users_me(current_user: User = Depends(get_current_user)):
|
async def read_users_me(current_user: User = Depends(get_current_user)):
|
||||||
return current_user
|
return current_user
|
||||||
|
|
||||||
|
|
||||||
|
@auth_router.get("/auth/check")
|
||||||
|
async def check_auth():
|
||||||
|
return {"registration": os.environ.get("REGISTRATION_ENABLED", "True") == "True"}
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
|
import os
|
||||||
from api.backend.database.common import connect, QUERIES
|
from api.backend.database.common import connect, QUERIES
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
from api.backend.auth.auth_utils import get_password_hash
|
||||||
|
|
||||||
LOG = logging.getLogger(__name__)
|
LOG = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@@ -12,4 +15,29 @@ def init_database():
|
|||||||
LOG.info(f"Executing query: {query}")
|
LOG.info(f"Executing query: {query}")
|
||||||
_ = cursor.execute(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()
|
cursor.close()
|
||||||
|
|||||||
@@ -48,14 +48,14 @@ export const checkAI = async (
|
|||||||
) => {
|
) => {
|
||||||
const token = Cookies.get("token");
|
const token = Cookies.get("token");
|
||||||
try {
|
try {
|
||||||
const response = await fetch("/api/ai/check", {
|
const response = await fetch("/api/check", {
|
||||||
headers: {
|
headers: {
|
||||||
"content-type": "application/json",
|
"content-type": "application/json",
|
||||||
Authorization: `Bearer ${token}`,
|
Authorization: `Bearer ${token}`,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
setAiEnabled(data);
|
setAiEnabled(data.ai_enabled);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error fetching jobs:", error);
|
console.error("Error fetching jobs:", error);
|
||||||
throw error;
|
throw error;
|
||||||
|
|||||||
@@ -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) {
|
if (!response.ok) {
|
||||||
throw new Error(`Error: ${response.statusText}`);
|
throw new Error(`Error: ${response.statusText}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = await response.json();
|
const result = await response.json();
|
||||||
res.status(200).json(result);
|
const checksResult = await checksResponse.json();
|
||||||
|
res.status(200).json({ ...result, ...checksResult });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error submitting scrape job:", error);
|
console.error("Error submitting scrape job:", error);
|
||||||
res.status(500).json({ error: "Internal Server Error" });
|
res.status(500).json({ error: "Internal Server Error" });
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import React, { useState } from "react";
|
import React, { useEffect, useState } from "react";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import { Button, TextField, Typography, Box } from "@mui/material";
|
import { Button, TextField, Typography, Box } from "@mui/material";
|
||||||
import { useTheme } from "@mui/material/styles";
|
import { useTheme } from "@mui/material/styles";
|
||||||
@@ -18,7 +18,16 @@ const AuthForm: React.FC = () => {
|
|||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const { login } = useAuth();
|
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) => {
|
const handleSubmit = async (event: React.FormEvent) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
try {
|
try {
|
||||||
@@ -124,9 +133,37 @@ const AuthForm: React.FC = () => {
|
|||||||
>
|
>
|
||||||
{mode.charAt(0).toUpperCase() + mode.slice(1)}
|
{mode.charAt(0).toUpperCase() + mode.slice(1)}
|
||||||
</Button>
|
</Button>
|
||||||
<Button onClick={toggleMode} fullWidth variant="text" color="primary">
|
{registrationEnabled && (
|
||||||
{mode === "login" ? "No Account? Sign up" : "Login"}
|
<Button
|
||||||
</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>
|
</Box>
|
||||||
</Box>
|
</Box>
|
||||||
|
|||||||
Reference in New Issue
Block a user