Last active
May 6, 2024 18:35
-
-
Save jovialcore/44ffc1b01e974fed0b582d92536e3c51 to your computer and use it in GitHub Desktop.
legendary way laravel authenticates SPAs expecially with sanctum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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