curl -X POST "http://localhost:8080/token?grant_type=password" \ -H "Content-Type: application/json" \ -d '{ "email": "[email protected]", "password": "securepassword123" }'
{ "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJhdXRoZW50aWNhdGVkIiwiZXhwIjoxNjQwOTk1MjAwLCJpYXQiOjE2NDA5MDg4MDAsImlzcyI6Imh0dHBzOi8veW91ci1wcm9qZWN0LnN1cGFiYXNlLmNvL2F1dGgvdjEiLCJzdWIiOiIxMjNlNDU2Ny1lODliLTEyZDMtYTQ1Ni00MjY2MTQxNzQwMDAiLCJlbWFpbCI6InVzZXJAZXhhbXBsZS5jb20iLCJyb2xlIjoiYXV0aGVudGljYXRlZCJ9...", "token_type": "bearer", "expires_in": 3600, "expires_at": 1640995200, "refresh_token": "refresh_token_string_here", "user": { "id": "123e4567-e89b-12d3-a456-426614174000", "aud": "authenticated", "role": "authenticated", "email": "[email protected]", "phone": null, "email_confirmed_at": "2023-01-01T00:00:00Z", "phone_confirmed_at": null, "last_sign_in_at": "2023-01-01T12:00:00Z", "app_metadata": { "provider": "email", "providers": ["email"] }, "user_metadata": { "first_name": "John", "last_name": "Doe" }, "created_at": "2023-01-01T00:00:00Z", "updated_at": "2023-01-01T12:00:00Z" } }
Login and token refresh using OAuth2 token endpoint
password
refresh_token
curl -X POST "http://localhost:8080/token?grant_type=refresh_token" \ -H "Content-Type: application/json" \ -d '{ "refresh_token": "your_refresh_token_here" }'
{ "code": 400, "msg": "Invalid credentials", "details": "Email or password is incorrect" }
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
curl -X GET "http://localhost:8080/user" \ -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..." \ -H "Content-Type: application/json"
class AuthClient { constructor() { this.accessToken = null; this.refreshToken = null; } async refreshAccessToken() { const response = await fetch('/token?grant_type=refresh_token', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ refresh_token: this.refreshToken }) }); const data = await response.json(); this.accessToken = data.access_token; this.refreshToken = data.refresh_token; } async makeRequest(url, options = {}) { // Add auth header const headers = { 'Authorization': `Bearer ${this.accessToken}`, ...options.headers }; let response = await fetch(url, { ...options, headers }); // If token expired, refresh and retry if (response.status === 401) { await this.refreshAccessToken(); headers['Authorization'] = `Bearer ${this.accessToken}`; response = await fetch(url, { ...options, headers }); } return response; } }
{ "phone": "+1234567890", "password": "securepassword123" }