I am building SPA with Vuejs and Laravel, I installed the laravel sanctum to authenticate the user by its token.
Api.php
Route::post('/teacher/login', 'Teacher\RegisterController@login')->middleware('auth:sanctum');
Login.Vue
login(){
axios.get('/sanctum/csrf-cookie').then(response => {
this.form.post('api/teacher/login')
.then( response => {
console.log( response );
})
.catch( error => {
console.log( error );
})
});
}
Custom Controller
public function login(Request $request){
$credentials = $request->only('email', 'password');
if(Auth::attempt($credentials)){
return response()->json(Auth::user(), 200);
}
}
I checked this without sanctum middleware it successfully logging the user, but I add the middleware inside the route it says your request is Unauthenticated. I implemented everything by following the official Laravel-7 doc, I don't why it says Unauthenticated, attaching the IMG for more detail.