Please anyone help me, I don't know what's wrong with my code. the funny thing is, even the login successfully I keep getting this message ```Status Code: 500 Internal Server Error``. Can anyone figure out what is wrong with my logic, I think this is something wrong with my typescript code:
ApiService.
post('sign-in', {
email: user.email,
password: user.password
})
.then((response) => {
if (response.data.success) {
store.commit(Mutations.SET_AUTH,response);
// Save token to localstorage
SanctumService.saveToken(response.data.data.token);
Swal.fire({
text: "You have successfully logged in!",
icon: "success",
buttonsStyling: false,
confirmButtonText: "Ok, got it!",
customClass: {
confirmButton: "btn fw-semobold btn-light-primary",
},
}).then(() => {
// Go to page after successfully login
router.push({ name: "dashboard" });
});
} else {
Swal.fire({
text: 'Your email or password is wrong!',
icon: "error",
buttonsStyling: false,
confirmButtonText: "Try again!",
customClass: {
confirmButton: "btn fw-semobold btn-light-danger",
},
});
}
})
.catch((e) => {
alert(e);
});
Here is my API Route:
Route::controller(AuthController::class)->group(function (){
Route::post('sign-up', 'register');
Route::post('sign-in', 'login');
});
Here my controller code:
public function login(Request $request)
{
if(Auth::attempt(['email' => $request->email, 'password' => $request->password])){
$authUser = Auth::user();
//$authUser = $request->user();
$success['token'] = $authUser->createToken('MOKI')->plainTextToken;
$success['name'] = $authUser->name;
$response = [
'success' => true,
'data' => $success,
'message' => 'User login successfully!'
];
return response()->json($response, 200);
}
else
{
$response = [
'success' => false,
'message' => 'Unauthorized'
];
return response()->json($response, 500);
}
}
Every time I check the tab Network for Response it always gives me the right one as at my AuthController, For example, if I input the wrong email the response will display like this {"success":false,"message":"Unauthorized"}.
What makes me confuse is, why sign-up work perfectly but not with sign-in, please help, much appreciate it.