| 1 | <?php |
| 2 | require __DIR__.'/lib/bootstrap.php'; |
| 3 | try { |
| 4 | if (!is_string($_GET['state']??null)||!isset($_SESSION['oauth_state'])||!hash_equals($_SESSION['oauth_state'],$_GET['state'])||time()-($_SESSION['oauth_started']??0)>600) throw new RuntimeException('Invalid or expired login state.'); |
| 5 | unset($_SESSION['oauth_state']); |
| 6 | if (!is_string($_GET['code']??null)) throw new RuntimeException('Login was not completed.'); |
| 7 | $cfg=config(); |
| 8 | $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']); |
| 9 | $profile=authRequest('/api/me',null,$r['access_token'])['user']; |
| 10 | loginUser('auth:'.$profile['id'],$profile['username']); |
| 11 | $_SESSION['token']=$r['access_token']; $_SESSION['expires']=time()+(int)$r['expires_in']; $_SESSION['validated']=time(); |
| 12 | $invite=$_SESSION['return_invite']??'';unset($_SESSION['return_invite']); |
| 13 | header('Location: '.baseUrl().'/'.($invite?'?invite='.rawurlencode($invite):'')); |
| 14 | } catch(Throwable $e) {http_response_code(401);echo 'Sign-in failed. '.htmlspecialchars($e->getMessage()).' <a href="auth.php">Try again</a>';} |
projects-timfalken-com microchess