Update routes/auth.py
Browse files- routes/auth.py +22 -28
routes/auth.py
CHANGED
@@ -11,7 +11,7 @@ from datetime import datetime, timedelta
|
|
11 |
|
12 |
router = APIRouter()
|
13 |
app = FastAPI()
|
14 |
-
|
15 |
class AuthViews:
|
16 |
def __init__(self):
|
17 |
self.pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
@@ -87,9 +87,26 @@ from app.database import get_user_by_email
|
|
87 |
|
88 |
# ...
|
89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
# User authentication (protected route)
|
91 |
-
@
|
92 |
-
def protected_route(token: str = Depends(oauth2_scheme), db: Session = Depends(get_db)):
|
93 |
# Verify the access token
|
94 |
user = verify_token(token, SECRET_KEY, ALGORITHM)
|
95 |
if user is None:
|
@@ -101,28 +118,5 @@ def protected_route(token: str = Depends(oauth2_scheme), db: Session = Depends(g
|
|
101 |
if db_user is None:
|
102 |
raise HTTPException(status_code=401, detail="User not found in the database")
|
103 |
|
104 |
-
# The user exists in the database, and you can
|
105 |
-
return
|
106 |
-
|
107 |
-
|
108 |
-
auth_views = AuthViews()
|
109 |
-
|
110 |
-
# User registration route
|
111 |
-
@router.post("/register", response_model=User)
|
112 |
-
def register(user: UserCreate, db: Session = Depends(get_db)):
|
113 |
-
return auth_views.register(user, db)
|
114 |
-
|
115 |
-
# Email verification route
|
116 |
-
@router.get("/verify/{verification_token}")
|
117 |
-
def verify_email(verification_token: str, db: Session = Depends(get_db)):
|
118 |
-
return auth_views.verify_email(verification_token, db)
|
119 |
-
|
120 |
-
# User login route
|
121 |
-
@router.post("/login", response_model=dict)
|
122 |
-
def login(form_data: OAuth2PasswordRequestForm = Depends()):
|
123 |
-
return auth_views.login(form_data)
|
124 |
-
|
125 |
-
# User authentication (protected route)
|
126 |
-
@router.get("/protected", response_model=str)
|
127 |
-
def protected_route(token: str = Depends(oauth2_scheme), db: Session = Depends(get_db)):
|
128 |
-
return auth_views.protected_route(token)
|
|
|
11 |
|
12 |
router = APIRouter()
|
13 |
app = FastAPI()
|
14 |
+
templates = Jinja2Templates(directory="templates")
|
15 |
class AuthViews:
|
16 |
def __init__(self):
|
17 |
self.pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
|
|
87 |
|
88 |
# ...
|
89 |
|
90 |
+
|
91 |
+
|
92 |
+
auth_views = AuthViews()
|
93 |
+
|
94 |
+
@app.get("/login", response_class=HTMLResponse)
|
95 |
+
async def login(request: Request):
|
96 |
+
return templates.TemplateResponse("login.html", {"request": request})
|
97 |
+
|
98 |
+
@app.get("/register", response_class=HTMLResponse)
|
99 |
+
async def register(request: Request):
|
100 |
+
return templates.TemplateResponse("register.html", {"request": request})
|
101 |
+
|
102 |
+
@app.get("/verify/{verification_token}", response_class=HTMLResponse)
|
103 |
+
async def verify_email(verification_token: str, request: Request):
|
104 |
+
# Perform verification and return an appropriate template
|
105 |
+
return templates.TemplateResponse("verify.html", {"request": request})
|
106 |
+
|
107 |
# User authentication (protected route)
|
108 |
+
@app.get("/protected", response_model=str)
|
109 |
+
async def protected_route(request: Request, token: str = Depends(oauth2_scheme), db: Session = Depends(get_db)):
|
110 |
# Verify the access token
|
111 |
user = verify_token(token, SECRET_KEY, ALGORITHM)
|
112 |
if user is None:
|
|
|
118 |
if db_user is None:
|
119 |
raise HTTPException(status_code=401, detail="User not found in the database")
|
120 |
|
121 |
+
# The user exists in the database, and you can render the protected route template
|
122 |
+
return templates.TemplateResponse("protected.html", {"request": request, "user": db_user.username})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|