AhmadYarAI commited on
Commit
28c68a3
·
1 Parent(s): b658da4

get User Info

Browse files
Files changed (5) hide show
  1. README.md +1 -17
  2. app/deps/deps.py +9 -6
  3. app/routers/auth.py +0 -1
  4. app/routers/users.py +8 -2
  5. main.py +2 -0
README.md CHANGED
@@ -8,20 +8,4 @@ sdk_version: "1.0"
8
  app_file: main.py
9
  pinned: false
10
  container_port: 7860
11
- ---
12
-
13
- # FastAPI Posts & Comments App
14
-
15
- This is a simple FastAPI application where users can create posts, add comments to posts, and fetch posts with their comments.
16
-
17
- ## Features
18
- - Create a post
19
- - Add comments to a post
20
- - Get all posts (with comments, newest first)
21
- - Get a single post with comments
22
-
23
- ## Installation
24
- ```bash
25
- git clone https://github.com/AhmadDurrani579/fast_api.git
26
- cd <your-repo-folder>
27
- pip install -r requirements.txt
 
8
  app_file: main.py
9
  pinned: false
10
  container_port: 7860
11
+ ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/deps/deps.py CHANGED
@@ -8,28 +8,31 @@ from app.db.database import get_db
8
  from app.db.models import UserDB
9
 
10
  bearer_scheme = HTTPBearer()
 
 
 
 
11
 
12
  def get_current_user(
13
  token: HTTPAuthorizationCredentials = Depends(bearer_scheme),
14
  db: Session = Depends(get_db)
15
  ):
16
- credentials_exception = HTTPException(
17
- status_code=401,
18
- detail="Invalid or expired token",
19
- )
20
  try:
21
  payload = jwt.decode(
22
  token.credentials,
23
  settings.JWT_SECRET,
24
  algorithms=[settings.JWT_ALGORITHM]
25
  )
26
- user_id: int = payload.get("id")
27
- if user_id is None:
 
28
  raise credentials_exception
29
 
30
  except JWTError:
31
  raise credentials_exception
 
32
  user = db.query(UserDB).filter(UserDB.id == user_id).first()
 
33
  if not user:
34
  raise credentials_exception
35
 
 
8
  from app.db.models import UserDB
9
 
10
  bearer_scheme = HTTPBearer()
11
+ credentials_exception = HTTPException(
12
+ status_code=401,
13
+ detail="Invalid or expired token",
14
+ )
15
 
16
  def get_current_user(
17
  token: HTTPAuthorizationCredentials = Depends(bearer_scheme),
18
  db: Session = Depends(get_db)
19
  ):
 
 
 
 
20
  try:
21
  payload = jwt.decode(
22
  token.credentials,
23
  settings.JWT_SECRET,
24
  algorithms=[settings.JWT_ALGORITHM]
25
  )
26
+ user_id = payload.get("id")
27
+
28
+ if not user_id:
29
  raise credentials_exception
30
 
31
  except JWTError:
32
  raise credentials_exception
33
+
34
  user = db.query(UserDB).filter(UserDB.id == user_id).first()
35
+
36
  if not user:
37
  raise credentials_exception
38
 
app/routers/auth.py CHANGED
@@ -15,7 +15,6 @@ router = APIRouter(prefix="/auth", tags=["Auth"])
15
  def generate_family_code():
16
  return ''.join(random.choices(string.ascii_uppercase + string.digits, k=6))
17
 
18
-
19
  @router.post("/signup/head")
20
  def signup_head(payload: SignupHead, db: Session = Depends(get_db)):
21
  existing = db.query(UserDB).filter(UserDB.email == payload.email).first()
 
15
  def generate_family_code():
16
  return ''.join(random.choices(string.ascii_uppercase + string.digits, k=6))
17
 
 
18
  @router.post("/signup/head")
19
  def signup_head(payload: SignupHead, db: Session = Depends(get_db)):
20
  existing = db.query(UserDB).filter(UserDB.email == payload.email).first()
app/routers/users.py CHANGED
@@ -1,10 +1,16 @@
1
  from fastapi import APIRouter, Depends
 
 
2
  from app.schemas.schemas import UserOut
3
  from app.db.models import UserDB
4
  from app.deps.deps import get_current_user
5
 
6
  router = APIRouter(prefix="/users", tags=["users"])
7
 
8
- @router.get("/me", response_model=UserOut)
9
  def me(current_user: UserDB = Depends(get_current_user)):
10
- return current_user
 
 
 
 
 
1
  from fastapi import APIRouter, Depends
2
+ from sqlalchemy.orm import Session
3
+
4
  from app.schemas.schemas import UserOut
5
  from app.db.models import UserDB
6
  from app.deps.deps import get_current_user
7
 
8
  router = APIRouter(prefix="/users", tags=["users"])
9
 
10
+ @router.get("/me")
11
  def me(current_user: UserDB = Depends(get_current_user)):
12
+ return {
13
+ "status": True,
14
+ "message": "User profile fetched successfully",
15
+ "user": UserOut.from_orm(current_user)
16
+ }
main.py CHANGED
@@ -32,6 +32,8 @@ def root():
32
  # include routers
33
  app.include_router(auth.router)
34
  app.include_router(profile.router)
 
 
35
  # app.include_router(users.router)
36
  # app.include_router(posts.router)
37
  # app.include_router(comments.router)
 
32
  # include routers
33
  app.include_router(auth.router)
34
  app.include_router(profile.router)
35
+
36
+
37
  # app.include_router(users.router)
38
  # app.include_router(posts.router)
39
  # app.include_router(comments.router)