I have this project where I installed passport and vue with laravel. I have my auth endpoints but now I am struggling to get these endpoints to work with my front-end side of the project.
So when I installed Laravel I use the commend php artisan ui vue --auth to generate my login/register scaffolding.
The api endpoints are in the file api.php
This is part of my code from the AuthController.php:
public function login(ApiLoginRequest $request){
//debug
//return $request;
$credentials = $request->only(['email', 'password']);
$authAttempt = auth()->attempt($credentials);
if(!$authAttempt){
return response([
'error' => 'Access forbidden',
'message' => 'Please check your email and password'
], 403 );
}
//debug result needs to be true
// return response([
// 'debug response' => $authAttempt
// ]);
$http = new Client();
$response = $http->post( 'http://laravel-xyzhub.test/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => $this->CLIENT_ID,
'client_secret' => $this->CLIENT_SECRET,
'username' => $request->email,
'password' => $request->password,
'scope' => ''
]
]);
return json_decode((string) $response->getBody(), true );
}
Code for the vue template:
<template>
<div class="mx-aut h-full flex justify-center items-center bg-gray-800">
<div class="w-96 bg-green-400 rounded-lg shadow-xl p-6" >
<div class="text-center text-white uppercase">
<h1 class="font-extrabold text-6xl">XYZ HUB</h1>
<h1 class="text-3xl pt-8">Welcome Back</h1>
<h2 class="pb-8 text-xs">Enter your credentials below</h2>
</div>
<!-- form -->
<form class="pb-8" @submit.prevent="postNow" ref="LoginForm" method="post">
<div class="relative">
<label for="email" class="uppercase text-green-400 font-bold absolute pl-3 pt-2">Email</label>
<div class="">
<input id="email" type="email" class="pt-8 w-full rounded p-3 text-green-700" name="email" v-model="credentials.email" autocomplete="email" autofocus placeholder="your@email.com">
</div>
</div>
<div class="relative pt-6">
<label for="password" class="uppercase text-green-400 font-bold absolute pl-3 pt-2">Password</label>
<div class="">
<input id="password" type="password" class="pt-8 w-full rounded p-3 text-green-700" name="password" v-model="credentials.password" placeholder="password">
</div>
</div>
<div class="pt-8">
<button type="submit" class="uppercase font-bold rounded w-full bg-white text-green-400 py-2 px-3 text-2xl" v-on:click="loginUser">Login</button>
</div>
<div class="flex justify-center pt-8 text-white uppercase font-semibold text-sm">
<a :href="recover"> Forget Password </a>
</div>
<div class="flex justify-center pt-2 text-white uppercase font-semibold text-sm">
<a :href="register"> Register </a>
</div>
</form>
</div>
</div>
<script>
export default {
data(){
return {
credentials:{
email: '',
password: ''
}
}
},
methods:{
postNow(event){
console.log("event" , {event , $form: this.$refs.LoginForm});
axios.post('/api/login' , this.credentials)
.then( response => {
console.log ('response' , response.data); router.push("/register");
})
.catch( error => {
console.log("error", error.response);
})
}
}
}
I know that my code in the template is not correct just not sure what I am doing wrong ... Any help would be much appreciated. Thanks in advance
UPDATE: So if I remove the prevent in the form and add a redirect once the response comes through then all of the sudden my submit does not go through
<form class="pb-8" @submit.prevent="postNow" ref="LoginForm" method="post">
change it to
<form class="pb-8" @submit="postNow" ref="LoginForm" method="post">
As you notice my endpoint is also not correct all of the sudden
Really confused with all of this ...
You noticed that endpoint is wrong.
So instead:
axios.post('/login' , this.credentials)
change to
axios.post('/api/login' , this.credentials)
Error is probably due to missing csrf token field in route "/login".
With api routes, you don't need csrf.
Additional, it's easier to log erros response, example...
.catch( error => {
console.log(error.response);
})