Skip to content

Commit

Permalink
fix: Added isFirstLogin token param
Browse files Browse the repository at this point in the history
  • Loading branch information
josebui committed Mar 12, 2024
1 parent a2fd250 commit f5d8604
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
4 changes: 2 additions & 2 deletions terraso_backend/apps/auth/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,11 @@ def create_test_access_token(self, user):
raise ValueError("User is not a test user")
return self.create_token(user, None, {"test": True, "access": True})

def create_access_token(self, user, created_with_service=None):
def create_access_token(self, user, extra_params={}):
return self.create_token(
user,
self.JWT_ACCESS_EXP_DELTA_SECONDS,
{"access": True, "createdWithService": created_with_service},
{**extra_params, "access": True},
)

def verify_access_token(self, token):
Expand Down
7 changes: 4 additions & 3 deletions terraso_backend/apps/auth/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ def process_callback(self, req):

try:
user, created_with_service = self.process_signup()
access_token, refresh_token = terraso_login(self.request, user, created_with_service)
is_first_login = created_with_service is not None
access_token, refresh_token = terraso_login(self.request, user, is_first_login)
except Exception as exc:
logger.exception("Error attempting create access and refresh tokens")
return HttpResponse(f"Error: {exc}", status=400)
Expand Down Expand Up @@ -244,8 +245,8 @@ def post(self, request, *args, **kwargs):
return HttpResponse("OK", status=200)


def terraso_login(request, user, created_with_service=None):
access_token = jwt_service.create_access_token(user, created_with_service)
def terraso_login(request, user, is_first_login=False):
access_token = jwt_service.create_access_token(user, {"isFirstLogin": is_first_login})
refresh_token = jwt_service.create_refresh_token(user)
dj_login(request, user, backend="django.contrib.auth.backends.ModelBackend")

Expand Down
4 changes: 2 additions & 2 deletions terraso_backend/tests/auth/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def test_userinfo_claims(rf, bearer_header, user):


@mock_aws
def test_sign_up_created_with_service(client, access_tokens_google, respx_mock):
def test_sign_up_is_first_login(client, access_tokens_google, respx_mock):
respx_mock.post(GoogleProvider.GOOGLE_TOKEN_URI).mock(
return_value=Response(200, json=access_tokens_google)
)
Expand All @@ -274,4 +274,4 @@ def test_sign_up_created_with_service(client, access_tokens_google, respx_mock):

decoded_payload = JWTService().verify_access_token(token.value)

assert decoded_payload["createdWithService"] == "google"
assert decoded_payload["isFirstLogin"] is True

0 comments on commit f5d8604

Please sign in to comment.