Skip to content

Commit

Permalink
Merge pull request #104 from hotosm/refactor
Browse files Browse the repository at this point in the history
refactor: user routes are kept in a single file
  • Loading branch information
nrjadkry authored Jul 30, 2024
2 parents e14f1e9 + d83b37d commit aea4cf0
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 79 deletions.
2 changes: 0 additions & 2 deletions src/backend/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from app.projects import project_routes
from app.drones import drone_routes
from app.waypoints import waypoint_routes
from app.users import oauth_routes
from app.users import user_routes
from app.tasks import task_routes
from app.db.database import db_connection
Expand Down Expand Up @@ -99,7 +98,6 @@ def get_application() -> FastAPI:
_app.include_router(project_routes.router)
_app.include_router(waypoint_routes.router)
_app.include_router(user_routes.router)
_app.include_router(oauth_routes.router)
_app.include_router(task_routes.router)

return _app
Expand Down
75 changes: 0 additions & 75 deletions src/backend/app/users/oauth_routes.py

This file was deleted.

71 changes: 69 additions & 2 deletions src/backend/app/users/user_routes.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
from fastapi import APIRouter, Response, HTTPException, Depends
import os
from fastapi import APIRouter, Response, HTTPException, Depends, Request
from typing import Annotated
from fastapi.security import OAuth2PasswordRequestForm
from app.users.user_schemas import (
Token,
ProfileUpdate,
AuthUser,
)
from app.users.user_deps import login_required
from app.users.user_deps import login_required, init_google_auth
from app.config import settings
from app.users import user_crud
from app.db import database
from app.models.enums import HTTPStatus
from databases import Database
from fastapi.responses import JSONResponse
from loguru import logger as log


if settings.DEBUG:
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"


router = APIRouter(
Expand Down Expand Up @@ -79,3 +86,63 @@ async def update_user_profile(

user = await user_crud.update_user_profile(db, user_id, profile_update)
return Response(status_code=HTTPStatus.OK)


@router.get("/google-login")
async def login_url(google_auth=Depends(init_google_auth)):
"""Get Login URL for Google Oauth Application.
The application must be registered on google oauth.
Open the download url returned to get access_token.
Args:
request: The GET request.
google_auth: The Auth object.
Returns:
login_url (string): URL to authorize user in Google OAuth.
Includes URL params: client_id, redirect_uri, permission scope.
"""
login_url = google_auth.login()
log.debug(f"Login URL returned: {login_url}")
return JSONResponse(content=login_url, status_code=200)


@router.get("/callback/")
async def callback(request: Request, google_auth=Depends(init_google_auth)):
"""Performs token exchange between Google and DTM API"""

# Enforce https callback url
callback_url = str(request.url).replace("http://", "https://")

access_token = google_auth.callback(callback_url).get("access_token")

user_data = google_auth.deserialize_access_token(access_token)
access_token, refresh_token = await user_crud.create_access_token(user_data)

return Token(access_token=access_token, refresh_token=refresh_token)


@router.get("/refresh-token", response_model=Token)
async def update_token(user_data: AuthUser = Depends(login_required)):
"""Refresh access token"""

access_token, refresh_token = await user_crud.create_access_token(
user_data.model_dump()
)
return Token(access_token=access_token, refresh_token=refresh_token)


@router.get("/my-info/")
async def my_data(
db: Database = Depends(database.get_db),
user_data: AuthUser = Depends(login_required),
):
"""Read access token and get user details from Google"""

user_info = await user_crud.get_or_create_user(db, user_data)
has_user_profile = await user_crud.get_userprofile_by_userid(db, user_info.id)

user_info_dict = user_info.model_dump()
user_info_dict["has_user_profile"] = bool(has_user_profile)
return user_info_dict

0 comments on commit aea4cf0

Please sign in to comment.