Skip to content

Commit

Permalink
get user info after login
Browse files Browse the repository at this point in the history
  • Loading branch information
feijooso committed Feb 19, 2024
1 parent 49edc87 commit 15d12a7
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
6 changes: 4 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from fastapi import FastAPI
from fastapi import Body
from controller.Users import UsersController
from service.Users import UsersService
from repository.Users import UsersRepository
from schemas.Schemas import CreateUserSchema
from schemas.Schemas import LoginRequest


app = FastAPI()
Expand Down Expand Up @@ -32,5 +34,5 @@ async def create_user(user_data: CreateUserSchema):


@app.post("/login")
def login_with_google(auth_code: str):
return users_controller.handle_login(auth_code)
def login_with_google(req: LoginRequest = Body(...)):
return users_controller.handle_login(req.auth_code)
2 changes: 1 addition & 1 deletion app/repository/Users.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def create_user(self, name: str):
new_user = User(name=name)
self.session.add(new_user)
self.session.commit()
return
return new_user

def __parse_result(self, result):
if not result:
Expand Down
4 changes: 4 additions & 0 deletions app/schemas/Schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ class UserSchema(BaseModel):

class CreateUserSchema(BaseModel):
name: str


class LoginRequest(BaseModel):
auth_code: str
8 changes: 6 additions & 2 deletions app/service/Users.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import requests
import os


def get_access_token(authorization_code):
token_url = "https://oauth2.googleapis.com/token"
payload = {
Expand All @@ -22,7 +23,8 @@ def get_access_token(authorization_code):
def get_user_info(access_token):
user_info_url = "https://www.googleapis.com/oauth2/v2/userinfo"
headers = {"Authorization": f"Bearer {access_token}"}
response = requests.get(user_info_url, headers=headers)
params = {"fields": "id,email,name,picture"}
response = requests.get(user_info_url, headers=headers, params=params)
if response.status_code == 200:
return response.json()
else:
Expand Down Expand Up @@ -52,4 +54,6 @@ def login(self, auth_code: str):

user = self.user_repository.get_user_by_name(user_info["email"])
if user is None:
self.user_repository.create_user(user_info["email"])
user = self.user_repository.create_user(user_info["email"])

return user

0 comments on commit 15d12a7

Please sign in to comment.