Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 593
Jason Spick Avatar asked Apr 29 '15 16:04

Jason Spick


1 Answers

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'));
});
like image 112
Styphon Avatar answered Nov 11 '22 18:11

Styphon