600) throw new RuntimeException('Invalid or expired login state.'); unset($_SESSION['oauth_state']); if (!is_string($_GET['code']??null)) throw new RuntimeException('Login was not completed.'); $cfg=config(); $r=authRequest('/oauth/token',['grant_type'=>'authorization_code','code'=>$_GET['code'],'client_id'=>$cfg['client_id'],'client_secret'=>$cfg['client_secret'],'redirect_uri'=>baseUrl().'/callback.php']); $profile=authRequest('/api/me',null,$r['access_token'])['user']; loginUser('auth:'.$profile['id'],$profile['username']); $_SESSION['token']=$r['access_token']; $_SESSION['expires']=time()+(int)$r['expires_in']; $_SESSION['validated']=time(); $invite=$_SESSION['return_invite']??'';unset($_SESSION['return_invite']); header('Location: '.baseUrl().'/'.($invite?'?invite='.rawurlencode($invite):'')); } catch(Throwable $e) {http_response_code(401);echo 'Sign-in failed. '.htmlspecialchars($e->getMessage()).' Try again';}