Skip to content

Instantly share code, notes, and snippets.

@joshcanhelp
Last active February 6, 2019 01:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshcanhelp/5dc610fc33513e29dcdbe6d91cc10136 to your computer and use it in GitHub Desktop.
Save joshcanhelp/5dc610fc33513e29dcdbe6d91cc10136 to your computer and use it in GitHub Desktop.
Auth callback middleware to set an access token in Laravel
<?php
// app/Http/Middleware/SetAccessTokenCookie.php
namespace App\Http\Middleware;
use Closure;
class SetAccessTokenCookie
{
/**
* Cookie name for the token.
* Used to set the cookie below and exclude from encryption.
*/
const COOKIE_NAME = 'access_token';
public function handle($request, Closure $next)
{
$accessToken = \App::make('auth0')->getAccessToken();
if ( $accessToken ) {
// Sets a cookie for 1440 minutes that is not HTTP only.
// Third argument should match the "Token Expiration (Seconds)" field in your custom API divided by 60.
\Cookie::queue(self::COOKIE_NAME, $accessToken, 1440, null, null, null, false);
}
return $next($request);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment