Install the Bundle
Add to composer.json:
composer require appshed/auth-bundle
Register in config/bundles.php (Symfony 3/4/5):
return [
// ...
AppShed\AuthBundle\AppShedAuthBundle::class => ['all' => true],
];
Configure the Bundle Publish the default config:
php bin/console config:dump-reference AppShed\AuthBundle
Update config/packages/appshed_auth.yaml:
appshed_auth:
api_url: 'https://api.appshed.com/auth'
cookie_name: 'appshed_auth_token'
secret_key: '%env(APPSHED_SECRET_KEY)%' # Store in .env
First Use Case: API Login Trigger login via a controller:
use AppShed\AuthBundle\Service\AuthService;
class LoginController extends AbstractController
{
public function login(AuthService $authService, Request $request)
{
$token = $authService->loginWithCookie($request->cookies->get('appshed_auth_token'));
// Use $token for authenticated API calls
}
}
Frontend Integration
appshed_auth_token cookie after user login on AppShed’s platform.document.cookie = `appshed_auth_token=${response.token}; path=/; secure`;
Backend Validation
// src/EventListener/AuthListener.php
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
$token = $request->cookies->get('appshed_auth_token');
if (!$token && !$request->isXmlHttpRequest()) {
$authService->validateToken($token)
->then(fn() => $event->setResponse(new RedirectResponse('/dashboard')))
->otherwise(fn() => $event->setResponse(new RedirectResponse('/login')));
}
}
API Requests Attach the token to API calls:
$client = new Client(['headers' => ['Authorization' => 'Bearer ' . $token]]);
$response = $client->request('GET', 'https://api.appshed.com/user');
Cookie facade for cookie handling:
$token = request()->cookie('appshed_auth_token');
AuthenticatorInterface for custom logic:
public function supports(Request $request)
{
return $request->cookies->has('appshed_auth_token');
}
refreshToken() method in a service:
$authService->refreshToken($expiredToken)->then(fn($newToken) => $this->setCookie($newToken));
Cookie Security
secure/httponly flags expose tokens to XSS.document.cookie = `appshed_auth_token=${token}; path=/; secure; httponly; SameSite=Lax`;
Response::makeCookie() with secure and httpOnly flags.Token Expiry
AuthService::validateToken() returns false for expired tokens.if (!$authService->isTokenValid($token)) {
$authService->refreshToken($token)->wait();
}
API URL Mismatch
api_url in config fails in staging/prod.appshed_auth:
api_url: '%env(APPSHED_API_URL)%'
config/packages/appshed_auth.yaml:
appshed_auth:
guzzle:
debug: true
$this->assertEquals(
'expected_token',
$request->cookies->get('appshed_auth_token')
);
Custom Token Storage
Override AuthService to store tokens in a database:
class CustomAuthService extends AuthService
{
public function saveToken($token, $userId)
{
Token::updateOrCreate(['user_id' => $userId], ['token' => $token]);
}
}
Multi-Tenant Support Extend the bundle to support tenant-specific tokens:
$authService->loginWithCookie($token, $tenantId);
Event Dispatching
Listen for auth.login.success events to trigger post-login actions:
$dispatcher->addListener('auth.login.success', function ($token) {
// Send welcome email, log activity, etc.
});
How can I help you explore Laravel packages today?