Jason Spick
Jason Spick

Reputation: 6098

Creating user in Laravel 5

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?

Upvotes: 6

Views: 12157

Answers (1)

Styphon
Styphon

Reputation: 10447

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'));
});

Upvotes: 12

Related Questions