mirror of
https://github.com/Bunsly/JobSpy.git
synced 2026-03-05 03:54:31 -08:00
feat(users): add register route
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
from fastapi import APIRouter
|
||||
from api.auth import router as auth_router
|
||||
from .v1 import router as v1_router
|
||||
|
||||
router = APIRouter(
|
||||
@@ -6,3 +7,4 @@ router = APIRouter(
|
||||
tags=["api"],
|
||||
)
|
||||
router.include_router(v1_router)
|
||||
router.include_router(auth_router)
|
||||
|
||||
8
api/auth/__init__.py
Normal file
8
api/auth/__init__.py
Normal file
@@ -0,0 +1,8 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
from api.auth.token import router as token_router
|
||||
from api.auth.register import router as register_router
|
||||
|
||||
router = APIRouter(prefix="/auth")
|
||||
router.include_router(token_router)
|
||||
router.include_router(register_router)
|
||||
@@ -6,10 +6,10 @@ from fastapi.security import OAuth2PasswordBearer
|
||||
|
||||
from settings import *
|
||||
from api.core.users import TokenData
|
||||
from .db_utils import UserInDB, get_user
|
||||
from api.auth.db_utils import UserInDB, get_user
|
||||
|
||||
load_dotenv()
|
||||
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/api/v1/token")
|
||||
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/api/auth/token")
|
||||
|
||||
|
||||
def create_access_token(data: dict):
|
||||
@@ -1,25 +1,41 @@
|
||||
from passlib.context import CryptContext
|
||||
|
||||
from supabase_py import create_client, Client
|
||||
from fastapi import HTTPException, status
|
||||
|
||||
from api.core.users import UserInDB
|
||||
from settings import SUPABASE_URL, SUPABASE_KEY
|
||||
|
||||
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
||||
supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
|
||||
|
||||
def get_user(username: str):
|
||||
result = supabase.table('users').select().eq('username', username).execute()
|
||||
|
||||
if 'error' in result and result['error']:
|
||||
def create_user(user_create: UserInDB):
|
||||
result = supabase.table("users").insert(user_create.dict()).execute()
|
||||
print(f"Insert result: {result}")
|
||||
|
||||
if "error" in result and result["error"]:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"User could not be created due to {result['error']['message']}",
|
||||
)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def get_user(username: str):
|
||||
result = supabase.table("users").select().eq("username", username).execute()
|
||||
|
||||
if "error" in result and result["error"]:
|
||||
print(f"Error: {result['error']['message']}")
|
||||
return None
|
||||
else:
|
||||
if result['data']:
|
||||
user_data = result['data'][0] # get the first (and should be only) user with the matching username
|
||||
if result["data"]:
|
||||
user_data = result["data"][0]
|
||||
return UserInDB(**user_data)
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
def verify_password(password: str, hashed_password: str):
|
||||
return pwd_context.verify(password, hashed_password)
|
||||
|
||||
28
api/auth/register/__init__.py
Normal file
28
api/auth/register/__init__.py
Normal file
@@ -0,0 +1,28 @@
|
||||
from fastapi import APIRouter, HTTPException, status
|
||||
from api.core.users import UserCreate, UserInDB
|
||||
from api.auth.db_utils import get_user, get_password_hash, create_user
|
||||
|
||||
router = APIRouter(prefix="/register", tags=["register"])
|
||||
|
||||
|
||||
@router.post("/")
|
||||
async def register_new_user(user: UserCreate):
|
||||
existing_user = get_user(user.username)
|
||||
if existing_user is not None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Username already exists",
|
||||
)
|
||||
|
||||
hashed_password = get_password_hash(user.password)
|
||||
print(f"Hashed password: {hashed_password}")
|
||||
user_create = UserInDB(
|
||||
username=user.username,
|
||||
email=user.email,
|
||||
full_name=user.full_name,
|
||||
hashed_password=hashed_password,
|
||||
disabled=False,
|
||||
)
|
||||
create_user(user_create)
|
||||
|
||||
return {"detail": "User created successfully"}
|
||||
@@ -2,17 +2,16 @@ from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from fastapi.security import OAuth2PasswordRequestForm
|
||||
|
||||
from api.core.users import Token
|
||||
from .db_utils import authenticate_user
|
||||
from .auth import create_access_token
|
||||
from api.auth.db_utils import authenticate_user
|
||||
from api.auth.auth_utils import create_access_token
|
||||
|
||||
ACCESS_TOKEN_EXPIRE_MINUTES = 30
|
||||
router = APIRouter(prefix="/token")
|
||||
router = APIRouter(prefix="/token", tags=["token"])
|
||||
|
||||
|
||||
@router.post("/", response_model=Token)
|
||||
async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends()):
|
||||
user = authenticate_user(form_data.username, form_data.password)
|
||||
if user is None:
|
||||
if not user:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Incorrect username or password",
|
||||
@@ -1,10 +1,18 @@
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class User(BaseModel):
|
||||
username: str
|
||||
full_name: str
|
||||
email: str
|
||||
disabled: bool
|
||||
disabled: bool = False
|
||||
|
||||
|
||||
class UserCreate(BaseModel):
|
||||
username: str
|
||||
full_name: str
|
||||
email: str
|
||||
password: str
|
||||
|
||||
|
||||
class UserInDB(User):
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
from fastapi import APIRouter
|
||||
from fastapi import APIRouter, Depends
|
||||
from .jobs import router as jobs_router
|
||||
from .token import router as token_router
|
||||
from api.auth.token import router as token_router
|
||||
from api.auth.auth_utils import get_active_current_user
|
||||
|
||||
router = APIRouter(prefix="/v1")
|
||||
router = APIRouter(prefix="/v1", dependencies=[Depends(get_active_current_user)])
|
||||
router.include_router(jobs_router)
|
||||
router.include_router(token_router)
|
||||
|
||||
@@ -4,9 +4,8 @@ from api.core.scrapers.indeed import IndeedScraper
|
||||
from api.core.scrapers.ziprecruiter import ZipRecruiterScraper
|
||||
from api.core.scrapers.linkedin import LinkedInScraper
|
||||
from api.core.scrapers import ScraperInput, Site
|
||||
from ...v1.token.auth import get_active_current_user
|
||||
|
||||
router = APIRouter(prefix="/jobs", dependencies=[Depends(get_active_current_user)])
|
||||
router = APIRouter(prefix="/jobs")
|
||||
|
||||
SCRAPER_MAPPING = {
|
||||
Site.LINKEDIN: LinkedInScraper,
|
||||
|
||||
Reference in New Issue
Block a user