I used this link http://localhost:8000/api/do_login? but it failed. Although when i freshly did it,it was working fine but later i started getting this error .The error was
{
"status": "unauthorized",
"message": "invalid credentials"
}
I used ngrok after that but its still same
public function login(Request $request)
{
$validator = Validator::make($request->all(), [
'username' => 'required|email|exists:customers,email',
'password' => 'required'
]);
if($validator->fails())
return response()->json([
'status' => 'error',
'message' => $validator->getMessageBag()->first()
], 422);
$customer = customer::where('email', $request->email)->first();
if($customer and Hash::check($request->password, $customer->password)) {
//authentication successful
$customer->access_token = $customer->createToken('login')->accessToken;
return response()->json([
'status' => 'success',
'message' => 'login successful',
'data' => $customer
]);
}else {
//invalid credentials
return response()->json([
'status' => 'unauthorized',
'message' => 'invalid credentials'
], 401);
}
if( Auth::attempt(['email'=>$request->email, 'password'=>$request->password]) ) {
$user = Auth::user();
$token = $user->createToken($user->email.'-'.now());
return response()->json([
'token' => $token->accessToken
]);
}
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire