Send a CSRF/XSRF token.
Jul 29, 2021
4
Level 1
419 (unknown status)
I can't solve that error. I can register at regres.in (postman an with fetch), and I can register in my laravel sanctum API with postman. But when I trie with fetch I get this 419(unknown status) and SyntaxError: Unexpected token < in JSON at position 0.
my fetch looks like this:
const url = "http://localhost:9000/api/register";
const myForm = document.querySelector("#loginForm");
myForm.addEventListener("submit", async (e) => {
e.preventDefault();
const formData = new FormData(myForm);
const formDataSerialized = Object.fromEntries(formData);
const jsonObj = { ...formDataSerialized };
try {
const response = await fetch(url, {
method: "POST",
body: JSON.stringify(jsonObj),
headers: {
"Content-Type": "application/json",
},
});
const json = await response.json();
console.log(json);
} catch (e) {
console.error(e);
}
});
and my Route(public):
Route::post('/register', [AuthController::class, 'register']);
wich is:
public function register(Request $request){
$fields = $request->validate([
'name' => 'required|string',
'email' => 'required|string|unique:users,email',
'password' => 'required|string|confirmed',
]);
$user = User::create([
'name' => $fields['name'],
'email' => $fields['email'],
'password' => Hash::make($fields['password']),
]);
$token = $user->createToken('myapptoken')->plainTextToken;
$response = [
'user' => $user,
'token' => $token
];
return response($response, 201);
}
at the end my form looks:
<form action="#" id="loginForm">
<label for="name" class="form-label">Your Name</label>
<input
type="text"
class="form-control"
id="name"
name="name"
placeholder="Ihr Name"
autocomplete="off"
autocapitalize="on"
/>
<label for="email" class="form-label">Email Adresse</label>
<input
type="email"
class="form-control"
id="email"
name="email"
placeholder="[email protected]"
autocomplete="on"
autocapitalize="on"
/>
<label for="password" class="form-label">Passwort </label>
<input
type="password"
class="form-control"
id="password"
name="password"
placeholder="min 8 Zeichen mit Zahl und Sonderzeichen"
autocomplete="off"
autocapitalize="on"
/>
<label for="password_confirmation" class="form-label"
>Passwort Bestätigung
</label>
<input
type="password"
class="form-control"
id="password_confirmation"
name="password_confirmation"
placeholder="Confirm Password"
autocomplete="off"
autocapitalize="on"
/>
<div class="card-body">
<a href="#" class="pe-auto">Passwort vergessen</a
><a href="#" class="pe-auto">Zum Login</a>
</div>
<button type="submit" id="postLogin" class="btn btn-primary">
Los geht's
</button>
</form>
So nothing special. Does anyone have any suggestions? Thank's and have a great day! German comments are also welcome.
Please or to participate in this conversation.