right-to-bracketLogin

Authenticate a user and create a session.

circle-info

When to use: Implement sign-in in your application.

chevron-rightPrerequisiteshashtag
  • Completed Quickstart

  • A running PostgreSQL instance with GL IAM configured

5-Line Core

result = await gateway.authenticate(
    credentials=PasswordCredentials(email="alice@example.com", password="SecurePass123"),
    organization_id="default",
)
user, token = result.unwrap()

Step-by-Step

1

Setup Gateway

from gl_iam import IAMGateway
from gl_iam.core.types import PasswordCredentials
from gl_iam.providers.postgresql import PostgreSQLProvider, PostgreSQLConfig

config = PostgreSQLConfig(
    database_url="postgresql+asyncpg://postgres:postgres@localhost:5432/gliam"
)
provider = PostgreSQLProvider(config)
gateway = IAMGateway.from_fullstack_provider(provider)
2

Authenticate

result = await gateway.authenticate(
    credentials=PasswordCredentials(
        email="alice@example.com",
        password="SecurePass123"
    ),
    organization_id="default",
)
3

Handle Result

if result.is_ok:
    user, token = result.unwrap()
    print(f"Welcome, {user.display_name}!")
    print(f"Access token: {token.access_token[:20]}...")
else:
    print(f"Login failed: {result.error.message}")
4

Expected Output

Welcome, Alice!
Access token: eyJhbGciOiJIUzI1N...
circle-check

Complete Example

Create login.py:

Run it:

Common Pitfalls

Pitfall
Solution

Missing organization_id

Always pass organization context

User enumeration

Return same error for "not found" and "wrong password"

Exposing tokens

Never log full tokens or pass in URLs

Next Steps

  • User - Understand the User object you received

  • Validate - Verify tokens on protected endpoints

Last updated

Was this helpful?