Code Examples

Learn how to integrate LittleAuth with practical examples.

Authentication Examples

JavaScript

Basic Authentication Flow

// Initialize LittleAuth
const auth = new LittleAuth({
  clientId: 'your-client-id',
  redirectUri: 'https://your-app.com/callback'
});

// Start the login flow
function login() {
  auth.login('google');
}

// Handle the callback
async function handleCallback() {
  try {
    const user = await auth.handleCallback();
    console.log('User authenticated:', user);
    
    // Store the user info and tokens
    localStorage.setItem('user', JSON.stringify(user));
    
    // Redirect to the dashboard
    window.location.href = '/dashboard';
  } catch (error) {
    console.error('Authentication error:', error);
  }
}

This example shows how to implement a basic authentication flow using the LittleAuth JavaScript SDK.

Python

Flask Integration

from flask import Flask, redirect, request, session
from littleauth import LittleAuth

app = Flask(__name__)
app.secret_key = 'your-secret-key'

auth = LittleAuth(
    client_id='your-client-id',
    client_secret='your-client-secret',
    redirect_uri='http://localhost:5000/callback'
)

@app.route('/')
def index():
    return 'Welcome to LittleAuth Example! Login'

@app.route('/login')
def login():
    auth_url = auth.get_authorization_url('google')
    session['oauth_state'] = auth.state
    return redirect(auth_url)

@app.route('/callback')
def callback():
    code = request.args.get('code')
    state = request.args.get('state')
    
    if state != session.get('oauth_state'):
        return 'State mismatch. Possible CSRF attack.', 400
    
    try:
        user = auth.handle_callback(code)
        session['user'] = user
        return redirect('/dashboard')
    except Exception as e:
        return f'Error: {str(e)}', 400

@app.route('/dashboard')
def dashboard():
    if 'user' not in session:
        return redirect('/login')
    
    user = session['user']
    return f'Welcome, {user["name"]}! You are logged in with {user["provider"]}.'

if __name__ == '__main__':
    app.run(debug=True)

This example demonstrates how to integrate LittleAuth with a Flask application.

More Examples Coming Soon

We're working on additional examples for calendar integration, webhooks, and more.