By using this code you can get a new token after login by using the regenerate()
method and returning a new csrf_token()
in the response.
Your Controller inside the function:
public function refreshToken(Request $request)
{
session()->regenerate();
return response()->json([
"token"=>csrf_token()],
200);
}
JavaScript:
$.ajax({
url: "{{url('refresh-token')}}",
type: 'get',
dataType: 'json',
success: function (result) {
$('meta[name="csrf-token"]').attr('content', result.token);
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': result.token
}
});
},
error: function (xhr, status, error) {
console.log(xhr);
}
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…