<?php
require __DIR__.'/lib/bootstrap.php';
try {
    $cfg=config();
    if (!$cfg['client_id'] || !$cfg['client_secret']) throw new RuntimeException('Production authentication needs a registered client ID and secret. See web/README.md.');
    $_SESSION['oauth_state']=bin2hex(random_bytes(32)); $_SESSION['oauth_started']=time();
    $game=$_GET['game']??'';
    $_SESSION['return_game']=is_string($game)&&preg_match('/^[1-9][0-9]{0,14}$/D',$game)?$game:'';
    $invite=$_GET['invite']??'';
    $_SESSION['return_invite']=is_string($invite)&&preg_match('/^[a-f0-9]{48}$/D',$invite)?$invite:'';
    header('Location: https://auth.timfalken.com/oauth/authorize?'.http_build_query(['client_id'=>$cfg['client_id'],'redirect_uri'=>baseUrl().'/callback.php','response_type'=>'code','state'=>$_SESSION['oauth_state']]));
} catch (Throwable $e) {http_response_code(503); echo htmlspecialchars($e->getMessage());}
