invalid_credentials on JWT auth laravel
I’m using jwt-auth for auth API with laravel and write following code.
My register API working good bug my login API return invalid_credentials
error.
why?
laravel version: 5.4
public function login(Request $request) {
$credentials = $request->only('username', 'password');
try {
$token = JWTAuth::attempt($credentials);
if (!$token) {
return response()->json(['error' => 'invalid_credentials'], 401);
}
} catch (JWTException $e) {
return response()->json(['error' => 'could_not_create_token'], 400);
}
return response()->json(compact('token'), 200);
}
public function register(Request $request) {
$credentials = $request->only('email', 'username', 'password', 'name');
$validator = Validator::make($credentials, [
'name' => 'required|max:80',
'username' => 'required|max:80|unique:users',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6',
]);
if ($validator->fails()) {
$errors = $validator->errors();
return response()->json(['error' => array(
'name' => $errors->first('name'),
'email' => $errors->first('email'),
'username' => $errors->first('username'),
'password' => bcrypt('password'),
)], 400);
}
$user = User::create([
'name' => $request->name,
'email' => $request->email,
'username' => $request->username,
'password' => $request->password,
]);
$token = JWTAuth::fromUser($user);
return response()->json(compact('token'));
}
from Laravel Questions and Answers https://laravelquestions.com/php/invalid_credentials-on-jwt-auth-laravel/
via Lzo Media
No comments:
Post a Comment