| Home | Getting Started | Core Concepts | Helpers | Extensions | Repo |
The OAuth helper simplifies social authentication integration in your application.
Configure OAuth providers in your .env file:
OAUTH_GOOGLE_ID=your_client_id
OAUTH_GOOGLE_SECRET=your_client_secret
OAUTH_GOOGLE_REDIRECT=https://your-app.com/auth/google/callback
OAUTH_GITHUB_ID=your_client_id
OAUTH_GITHUB_SECRET=your_client_secret
OAUTH_GITHUB_REDIRECT=https://your-app.com/auth/github/callback
// Get current user
$user = tiny::user();
// Check if user is authenticated
if (tiny::isAuthenticated()) {
// User is logged in
}
// Get user property
$email = tiny::user()->email;
$name = tiny::user()->name;
// Check user role
if (tiny::user()->is_admin) {
// User is an admin
}
// Configure provider
tiny::oauth()->configure('google', [
'client_id' => $_ENV['OAUTH_GOOGLE_ID'],
'client_secret' => $_ENV['OAUTH_GOOGLE_SECRET'],
'redirect_uri' => $_ENV['OAUTH_GOOGLE_REDIRECT']
]);
// Get authorization URL
$url = tiny::oauth()->getAuthUrl('google', [
'scope' => ['email', 'profile']
]);
// Handle OAuth callback
try {
$token = tiny::oauth()->handleCallback('google');
$profile = tiny::oauth()->getUserProfile('google');
// Create or update user
$user = tiny::user()->createFromOAuth($profile);
// Log user in
tiny::auth()->login($user);
} catch (OAuthException $e) {
// Handle authentication error
}
// Request specific permissions
$url = tiny::oauth()->getAuthUrl('github', [
'scope' => ['user', 'repo'],
'state' => csrf_token()
]);
// Get access token
$token = tiny::oauth()->getAccessToken('google');
// Refresh token
$newToken = tiny::oauth()->refreshToken('google', $refreshToken);
// Revoke token
tiny::oauth()->revokeToken('google', $token);
// Make authenticated API request
$response = tiny::oauth()->request('google', 'GET', 'userinfo');
// With custom parameters
$repos = tiny::oauth()->request('github', 'GET', 'user/repos', [
'sort' => 'updated',
'per_page' => 10
]);