forked from UTSAVS26/PyVerse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
83 lines (65 loc) · 3.32 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from fastapi import FastAPI, Depends, HTTPException, status
from sqlalchemy.orm import Session
from fastapi.security import OAuth2PasswordRequestForm
from . import models, schemas, crud, auth, database
from fastapi.middleware.cors import CORSMiddleware
#frontend imports
from fastapi.staticfiles import StaticFiles
app = FastAPI()
# app.mount("/static", StaticFiles(directory="frontend"), name="static")
origins = [
"http://localhost:3000",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins, # Allow specific origins
allow_credentials=True,
allow_methods=["*"], # Allow all HTTP methods
allow_headers=["*"], # Allow all headers
)
models.Base.metadata.create_all(bind=database.engine)
#Login route
@app.post("/token", response_model=schemas.Token)
def login_for_access_token(db: Session = Depends(database.get_db), form_data: OAuth2PasswordRequestForm = Depends()):
user = auth.authenticate_user(db, form_data.username, form_data.password)
if not user:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect email or password",
headers={"WWW-Authenticate": "Bearer"},
)
access_token = auth.create_access_token(data={"sub": user.email})
return {"access_token": access_token, "token_type": "bearer","role":user.role}
#Registration route
@app.post("/register", response_model=schemas.UserOut)
def register_user(user: schemas.UserCreate, db: Session = Depends(database.get_db)):
db_user = crud.get_user_by_email(db, email=user.email)
if db_user:
raise HTTPException(status_code=400, detail="Email already registered")
return crud.create_user(db=db, user=user)
#Logged-In User's Info fetch
@app.get("/users/me", response_model=schemas.UserOut)
def read_users_me(current_user: models.User = Depends(auth.get_current_user)):
return current_user
#Logged-In User's Info Update
@app.put("/users/me", response_model=schemas.UserOut)
def update_user_me(user_update: schemas.UserUpdate, db: Session = Depends(database.get_db), current_user: models.User = Depends(auth.get_current_user)):
return crud.update_user(db=db, user=current_user, user_update=user_update)
#Logged-In User(Admin) -> Info fetch (of All Users)
@app.get("/admin/users", response_model=list[schemas.UserOut])
def get_all_users(db: Session = Depends(database.get_db), current_user: models.User = Depends(auth.get_current_admin)):
return crud.get_users(db=db)
#Logged-In User(Admin) -> Info Update (of All Users)
@app.put("/admin/users/{user_id}", response_model=schemas.UserOut)
def update_user(user_id: int, user_update: schemas.UserUpdate, db: Session = Depends(database.get_db), current_user: models.User = Depends(auth.get_current_admin)):
user = crud.get_user(db=db, user_id=user_id)
if user is None:
raise HTTPException(status_code=404, detail="User not found")
return crud.update_user(db=db, user=user, user_update=user_update)
#User Delete by Admin
@app.delete("/admin/users/{user_id}", response_model=dict)
def delete_user(user_id: int, db: Session = Depends(database.get_db), current_user: models.User = Depends(auth.get_current_admin)):
success = crud.delete_user(db=db, user_id=user_id)
if not success:
raise HTTPException(status_code=404, detail="User not found")
return {"status": "User deleted successfully"}