OAuth Login Flow
Use Auth
to implement Google OAuth2 login.
Routes
$r->addRoute('GET', '/auth/google', 'BaseApp\\Controller\\AuthController@callout');
$r->addRoute('GET', '/auth/google/callback', 'BaseApp\\Controller\\AuthController@callback');
Controller
namespace BaseApp\Controller;
use Stilmark\Base\Controller;
use Stilmark\Base\Auth;
use Stilmark\Base\Request;
class AuthController extends Controller
{
protected Auth $auth;
public function initialize()
{
$this->auth = new Auth();
}
public function callout()
{
$this->auth->callout(); // Redirects to Google
}
public function callback()
{
$user = $this->auth->callback($this->request);
// Persist to session, then redirect
$_SESSION['user'] = $user;
return $this->json(['login' => 'ok', 'user' => $user]);
}
}
Google Credentials Setup
Before using OAuth2, you need to obtain Google credentials:
Go to the Google Cloud Console
Create a new project or select an existing one
Enable the Google+ API
Create OAuth 2.0 credentials (Client ID and Client Secret)
Add your redirect URI to the authorized redirect URIs
.env requirements
GOOGLE_CLIENT_ID=... # From Google Cloud Console
GOOGLE_CLIENT_SECRET=... # From Google Cloud Console
GOOGLE_REDIRECT_URI=http://localhost:8000/auth/google/callback
Testing
Visit
/auth/google
→ consent → callback.Inspect session and response JSON.
Last updated