badge-checkValidate

Verify an access token and get the associated user.

circle-info

When to use: On every protected API request to verify the token is valid.

chevron-rightPrerequisiteshashtag
  • Completed Login

  • Have an access token from authentication

5-Line Core

result = await gateway.validate_session(
    access_token=token.access_token,
    organization_id="default",
)
user = result.unwrap()

Step-by-Step

1

Extract Token from Request

# Typically from Authorization header
authorization = request.headers.get("Authorization")
if authorization and authorization.startswith("Bearer "):
    access_token = authorization.removeprefix("Bearer ")
2

Validate Token

result = await gateway.validate_session(
    access_token=access_token,
    organization_id="default",
)
3

Handle Result

if result.is_ok:
    user = result.value
    print(f"Token valid! User: {user.email}")
else:
    print(f"Token invalid: {result.error.code}")
4

Expected Output

Token valid! User: alice@example.com
circle-check

Complete Example

Create validate.py:

Run it:

Expected output:

FastAPI Integration

For a ready-made dependency, use get_current_user from gl_iam.fastapi. To build your own:

Common Pitfalls

Pitfall
Solution

Using query params for tokens

Use Authorization header instead

Forgetting organization_id

Always validate in correct tenant context

Logging full tokens

Log only a short prefix if needed

Next Steps

  • Refresh - Renew expired tokens

  • User - Work with the returned User object


circle-info

Found an issue on this page? Report it on our feedback formarrow-up-right.

Last updated

Was this helpful?