So I am following this angular/Laravel 5 tutorial using JSON web Tokens. I am running into an issue.
Here is how it says create a user:
Route::post('/signup', function () {
$credentials = Input::only('email', 'password','name');
try {
$user = User::create($credentials);
} catch (Exception $e) {
return Response::json(['error' => 'User already exists.'], Illuminate\Http\Response::HTTP_CONFLICT);
}
$token = JWTAuth::fromUser($user);
return Response::json(compact('token'));
});
The problem is, User::create($credentials);
does not encrypt the password, meaning, logins will always fail. I found this out by using Laravel's default registration.
My question is, how do I create a new user that creates it the correct way?
You have to hash the password yourself using the Hash
helper class. Try this:
Route::post('/signup', function () {
$credentials = Input::only('email', 'password','name');
$credentials['password'] = Hash::make($credentials['password']);
try {
$user = User::create($credentials);
} catch (Exception $e) {
return Response::json(['error' => 'User already exists.'], Illuminate\Http\Response::HTTP_CONFLICT);
}
$token = JWTAuth::fromUser($user);
return Response::json(compact('token'));
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With