Creating a user in Laravel 5

So, I am following this Laravel 5 angular / tutorial using JSON testers. I ran into a problem.

Here is how it is written: 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 that User::create($credentials); does not encrypt the password, which means logins will always fail. I found this using the default Laravel login.

My question is: how do I create a new user that creates him correctly?

+6
source share
1 answer

You must use the password yourself using the Hash helper class . Try the following:

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

Source: https://habr.com/ru/post/986242/


All Articles