Skip to content

Commit

Permalink
HAN-79: Devolver un session token en el login (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
violetaperezandrade authored Apr 14, 2024
1 parent 4b8793e commit 4236140
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 6 deletions.
1 change: 1 addition & 0 deletions .env.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Users service
LOGGING_LEVEL=
PORT=
JWT_SECRET=

# Postgres
POSTGRES_USER=
Expand Down
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ default: docker-compose-up

all:

docker-image:
create-network:
@if ! docker network inspect common_network >/dev/null 2>&1; then \
docker network create common_network; \
fi
.PHONY: create-network

docker-image: create-network
docker build -f ./Dockerfile -t "app:latest" .
.PHONY: docker-image

Expand All @@ -17,4 +23,4 @@ docker-compose-down:

docker-compose-logs:
docker-compose -f docker-compose.yaml logs -f
.PHONY: docker-compose-logs
.PHONY: docker-compose-logs
7 changes: 5 additions & 2 deletions app/controller/Users.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,16 @@ def handle_create_user(self, user_data: dict):
)

def handle_login(self, auth_code: str):
user = self.users_service.login(auth_code)
user, jwt_token = self.users_service.login(auth_code)
return JSONResponse(
status_code=status.HTTP_200_OK,
content=jsonable_encoder({
"message": user,
"status": status.HTTP_200_OK,
})
}),
headers={
"x-access-token": f"{jwt_token}"
},
)

def handle_update_user(self, user_id: int, update_data: dict):
Expand Down
9 changes: 7 additions & 2 deletions app/service/Users.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import requests
import os
import re
import jwt


class UsersService:
Expand Down Expand Up @@ -50,8 +51,12 @@ def login(self, auth_code: str):
if user is None:
self.user_repository.add(User(**user_info))
user = self.user_repository.get_user_by_email(user_info["email"])

return user
user_id = user.get("id")
payload = {"user_id": user_id}
jwt_token = jwt.encode(payload,
os.environ["JWT_SECRET"],
algorithm="HS256")
return user, jwt_token

def _get_access_token(self, authorization_code):
token_url = "https://oauth2.googleapis.com/token"
Expand Down
7 changes: 7 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ services:
volumes:
- ./app:/app
tty: true
networks:
- common_network

sql:
build:
Expand Down Expand Up @@ -46,3 +48,8 @@ services:
timeout: 3s
retries: 10
start_period: 50s
networks:
- common_network
networks:
common_network:
external: true
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ flake8
psycopg2-binary
SQLAlchemy
requests_oauthlib
pyjwt

0 comments on commit 4236140

Please sign in to comment.