Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jovialcore/44ffc1b01e974fed0b582d92536e3c51 to your computer and use it in GitHub Desktop.
Save jovialcore/44ffc1b01e974fed0b582d92536e3c51 to your computer and use it in GitHub Desktop.
legendary way laravel authenticates SPAs expecially with sanctum
<?php
namespace Illuminate\Foundation\Http\Middleware;
//legendary way laravel authenticates SPAs expecially with sanctum
// compare the hash_equals of the server session token (session value generated by hashing the user's password) with the cookie sent to the frontend
class VerifyCsrfToken
{
/**
* Determine if the session and input CSRF tokens match.
*
* @param \Illuminate\Http\Request $request
* @return bool
*/
protected function tokensMatch($request)
{
$token = $this->getTokenFromRequest($request);
return is_string($request->session()->token()) &&
is_string($token) &&
hash_equals($request->session()->token(), $token);
}
// the rest of the code here
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment