mirror of
https://github.com/a-mayb3/Kanban_clone_backend.git
synced 2026-03-21 18:15:37 +01:00
Started implementing auth
This commit is contained in:
parent
e557c25789
commit
b909a23fa3
7 changed files with 279 additions and 19 deletions
45
routers/me.py
Normal file
45
routers/me.py
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
from fastapi import APIRouter, Depends, HTTPException, status, Response, Request
|
||||
from database import db_dependency
|
||||
from jose import JWTError, jwt
|
||||
from datetime import datetime, timedelta, timezone
|
||||
import models
|
||||
import os
|
||||
|
||||
from routers import auth
|
||||
import schemas.users as user_schemas
|
||||
import routers.users as user_router
|
||||
|
||||
router = APIRouter(prefix="/me", tags=["me"])
|
||||
|
||||
@router.get("/", response_model=user_schemas.UserBase)
|
||||
def get_me(request: Request, db: db_dependency):
|
||||
"""Get current authenticated user"""
|
||||
token = request.cookies.get("access_token")
|
||||
|
||||
if not token:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Not logged in"
|
||||
)
|
||||
|
||||
try:
|
||||
payload = jwt.decode(token, auth.SECRET_KEY, algorithms=[auth.ALGORITHM])
|
||||
user_id: str = str(payload.get("sub"))
|
||||
if user_id is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Not logged in"
|
||||
)
|
||||
except JWTError:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Could not validate credentials"
|
||||
)
|
||||
|
||||
db_user = db.query(models.User).filter(models.User.id == int(user_id)).first()
|
||||
if db_user is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="User not found"
|
||||
)
|
||||
return db_user
|
||||
Loading…
Add table
Add a link
Reference in a new issue