How OAuth Works
June 29, 2025How OAuth Works
OAuth is a protocol that allows third-party applications to access user data without sharing passwords. It's the backbone of secure authorization used by platforms like Google, Twitter, and Facebook.
π What Is OAuth?
OAuth (Open Authorization) enables users to approve an app to act on their behalf without giving away their credentials. For example, you can allow a scheduling app to access your Google Calendar.
π Step 1: Authorization Request
Redirect the user to the authorization server:
const params = {
response_type: 'code',
client_id: 'your-client-id',
redirect_uri: 'https://yourapp.com/callback',
scope: 'email profile',
state: 'secureRandomString'
};
const url = 'https://auth.example.com/oauth/authorize?' + new URLSearchParams(params);
// window.location = url;
π Step 2: Callback with Code
Once the user approves, they'll be redirected with a ?code
and ?state
.
π οΈ Step 3: Exchange Code for Token
const response = await fetch('https://auth.example.com/oauth/token', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: new URLSearchParams({
grant_type: 'authorization_code',
code: 'the-code-from-query',
redirect_uri: 'https://yourapp.com/callback',
client_id: 'your-client-id',
client_secret: 'your-client-secret'
})
});
const data = await response.json();
const accessToken = data.access_token;
π‘ What is PKCE?
PKCE (Proof Key for Code Exchange) is an enhancement for public clients (like mobile apps) that can't store secrets securely.
// Frontend
const codeVerifier = generateRandomString();
const codeChallenge = await sha256(codeVerifier);
sessionStorage.setItem('verifier', codeVerifier);
const params = {
response_type: 'code',
code_challenge_method: 'S256',
code_challenge: codeChallenge
};
π’ Summary
- Use OAuth to authorize apps securely.
- Always verify the state parameter.
- Use PKCE for frontend and mobile apps.
Blog
Mastering Laravel Context: Advanced Logging and Contextual Job Tracing
Jul 20, 2025
Laravel Context is one of the most powerful new features in Laravel 12. It allows you to attach contextual data (like user ID, IP address, request pat...
Essential React Native UI & Interaction Components
Jul 01, 2025
Essential React Native UI & Interaction Components React Native provides a powerful set of built-in components for creating native mobile apps....
Top Laravel & PHP Updates for Cleaner, Faster Code
Aug 17, 2025
Laravel Global Scopes: Automatic Query Filtering Eloquent Importance: Enforce consistent filters across all model queries (e.g., Soft Del...
The Difference Between Redux, Context & React Components in State Management
Aug 06, 2025
When building applications with React, there’s always a need to manage data that changes based on user interaction or from fetching external r...
React History β A Professional Developerβs Perspective
Jul 26, 2025
1. Origins: Born Inside Facebook In 2011, Facebook engineers faced the increasing complexity of building interactive UIs at scale. They developed...
Stop Copy-Pasting Code! Learn How to Use Traits in Laravel the Right Way
Jul 01, 2025
π« Stop Copy-Pasting Code! Ever duplicated slug logic or logging across multiple models? Laravel's Traits got your back. 1. What’s a Trait?...
