Answer the question
In order to leave comments, you need to log in
Laravel + Nuxt: How to overcome error 419 - CSRF token mismatch?
Test on localhost
export const actions = {
async nuxtServerInit({ state }) {
await this.$axios.post('/init')
.then((result) => {
state.data = result.data;
});
}
}
public function index(Request $request)
{
$state = array(
'token' => csrf_token()
);
return json_encode($state);
}
protected $middlewareGroups = [
...
'api' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
'throttle:60,1',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];
class VerifyCsrfToken extends Middleware
{
protected $except = [
'api/init'
];
}
<form @submit.prevent="submit">
...
</form>
<script>
export default {
components: {
},
data() {
return {
form: {
_token: this.$store.getters.params.token,
name: '',
email: '',
message: ''
}
}
},
methods: {
async submit() {
await this.$axios.post('contacts', this.form)
.then((result) => {
console.log('post >', result.data);
});
}
}
}
</script>
419: CSRF token mismatch.
Answer the question
In order to leave comments, you need to log in
CSRF is not needed with Nuxt and Laravel. You are doing everything wrong. I advise you to first learn the basics of the tools used
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question