Skip to content

Commit

Permalink
fix: Added google oauth credentials even if user exits
Browse files Browse the repository at this point in the history
  • Loading branch information
supradeep2819 committed Jun 19, 2024
1 parent ee381b0 commit 751773e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 15 deletions.
58 changes: 44 additions & 14 deletions core/routes/social_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def generate_key():


# TODO response for bad request, 400
@router.post("/login", response={200: Json,400:DetailSchema})
@router.post("/login", response={200: Json, 400: DetailSchema})
def login(request, payload: SocialAuthLoginSchema):

req = payload.dict()
Expand All @@ -41,7 +41,7 @@ def login(request, payload: SocialAuthLoginSchema):
account = req["account"]

email = user_data["email"]

try:
user = User.objects.get(email=email)
except User.DoesNotExist:
Expand All @@ -57,23 +57,53 @@ def login(request, payload: SocialAuthLoginSchema):
workspace.save()
user.save()
user.organizations.add(org)
oauth = OAuthApiKeys(workspace=workspace, type='GOOGLE_LOGIN', id=uuid.uuid4())
oauth.oauth_config = {
"access_token": account["access_token"],
"refresh_token": account["refresh_token"],
"expires_at": account["expires_at"]
}
oauth.save()
token, _ = Token.objects.get_or_create(user=user)
user_id = user.id
try:
user = User.objects.get(id=user.id)
result = urlparse(os.environ["DATABASE_URL"])
username = result.username
password = result.password
database = result.path[1:]
hostname = result.hostname
port = result.port
conn = psycopg2.connect(user=username, password=password, host=hostname, port=port, database=database)
cursor = conn.cursor()
query = ("select organization_id from core_user_organizations where user_id = {user_id}").format(
user_id=user.id)
cursor.execute(query)
result = cursor.fetchone()
logger.debug(result)
conn.close()
workspace = Workspace.objects.get(organization_id=result)
logger.debug(workspace.id)
oauth = OAuthApiKeys.objects.filter(workspace=workspace.id, type='GOOGLE_LOGIN').first()
if oauth:
oauth.oauth_config = {
"access_token": account["access_token"],
"refresh_token": account["refresh_token"],
"expires_at": account["expires_at"]
}
oauth.save()
else:
oauth = OAuthApiKeys(
workspace=workspace,
type='GOOGLE_LOGIN',
id=uuid.uuid4(),
oauth_config={
"access_token": account["access_token"],
"refresh_token": account["refresh_token"],
"expires_at": account["expires_at"]
}
)
oauth.save()
result = urlparse(os.environ["DATABASE_URL"])
username = result.username
password = result.password
database = result.path[1:]
hostname = result.hostname
port = result.port
conn = psycopg2.connect(user=username, password=password, host=hostname, port=port,database=database)
conn = psycopg2.connect(user=username, password=password, host=hostname, port=port, database=database)
cursor = conn.cursor()
query = """
SELECT
Expand Down Expand Up @@ -122,15 +152,15 @@ def login(request, payload: SocialAuthLoginSchema):
"core_organization"."status"
) AS subquery;
"""
cursor.execute(query,(user_id,))
cursor.execute(query, (user_id,))
result = cursor.fetchone()
cursor.close()
conn.close()
response = {
response = {
"auth_token": token.key,
"organizations":result
"organizations": result
}
logger.debug(response)
return json.dumps(response)
except Exception as e:
return (400, {"detail": e.message})
return (400, {"detail": e.message})
2 changes: 1 addition & 1 deletion init_db/prompt_def.json
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@
{
"name": "Sales by payment method",
"description": "Get the data of overall sales by different payment methods",
"query": "select Payment_Method, count(*) as Total_Orders, round(sum(total_price)) as Total_Sales from {{schema}}.sales_by_payment_method where {{filters}} group by Payment_Method",
"query": "select md5(row(Payment_Method)::text) as id,Payment_Method, count(*) as Total_Orders, round(sum(total_price)) as Total_Sales from {{schema}}.sales_by_payment_method where {{filters}} group by Payment_Method",
"filters": [
{
"display_column": "payment_method",
Expand Down

0 comments on commit 751773e

Please sign in to comment.