|
2 | 2 |
|
3 | 3 | namespace App\Http\Controllers\Auth;
|
4 | 4 |
|
| 5 | +use JWTAuth; |
5 | 6 | use App\User;
|
6 |
| -use Validator; |
7 | 7 | use App\Http\Controllers\Controller;
|
8 |
| -use Illuminate\Foundation\Auth\ThrottlesLogins; |
9 |
| -use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers; |
10 | 8 |
|
11 | 9 | class AuthController extends Controller
|
12 | 10 | {
|
13 |
| - /* |
14 |
| - |-------------------------------------------------------------------------- |
15 |
| - | Registration & Login Controller |
16 |
| - |-------------------------------------------------------------------------- |
17 |
| - | |
18 |
| - | This controller handles the registration of new users, as well as the |
19 |
| - | authentication of existing users. By default, this controller uses |
20 |
| - | a simple trait to add these behaviors. Why don't you explore it? |
21 |
| - | |
22 |
| - */ |
23 |
| - |
24 |
| - use AuthenticatesAndRegistersUsers, ThrottlesLogins; |
25 |
| - |
26 |
| - /** |
27 |
| - * Where to redirect users after login / registration. |
28 |
| - * |
29 |
| - * @var string |
30 |
| - */ |
31 |
| - protected $redirectTo = '/'; |
32 |
| - |
33 |
| - /** |
34 |
| - * Create a new authentication controller instance. |
35 |
| - * |
36 |
| - * @return void |
37 |
| - */ |
38 |
| - public function __construct() |
| 11 | + public function postLogin(Request $request) |
39 | 12 | {
|
40 |
| - $this->middleware('guest', ['except' => 'logout']); |
| 13 | + $credentials = $request->only('email', 'password'); |
| 14 | + |
| 15 | + try { |
| 16 | + // verify the credentials and create a token for the user |
| 17 | + if (!$token = JWTAuth::attempt($credentials)) { |
| 18 | + return response()->error('Invalid credentials', Response::HTTP_UNAUTHORIZED); |
| 19 | + } |
| 20 | + } catch (\JWTException $e) { |
| 21 | + return response()->error('Could not create token', Response::HTTP_INTERNAL_SERVER_ERROR); |
| 22 | + } |
| 23 | + |
| 24 | + return response()->success(compact('token')); |
41 | 25 | }
|
42 | 26 |
|
43 |
| - /** |
44 |
| - * Get a validator for an incoming registration request. |
45 |
| - * |
46 |
| - * @param array $data |
47 |
| - * @return \Illuminate\Contracts\Validation\Validator |
48 |
| - */ |
49 |
| - protected function validator(array $data) |
| 27 | + public function postRegister() |
50 | 28 | {
|
51 |
| - return Validator::make($data, [ |
52 |
| - 'name' => 'required|max:255', |
53 |
| - 'email' => 'required|email|max:255|unique:users', |
54 |
| - 'password' => 'required|confirmed|min:6', |
| 29 | + $this->validate($request, [ |
| 30 | + 'name' => 'required|min:3', |
| 31 | + 'email' => 'required|email|unique:users', |
| 32 | + 'password' => 'required|min:8', |
55 | 33 | ]);
|
56 |
| - } |
57 | 34 |
|
58 |
| - /** |
59 |
| - * Create a new user instance after a valid registration. |
60 |
| - * |
61 |
| - * @param array $data |
62 |
| - * @return User |
63 |
| - */ |
64 |
| - protected function create(array $data) |
65 |
| - { |
66 |
| - return User::create([ |
67 |
| - 'name' => $data['name'], |
68 |
| - 'email' => $data['email'], |
69 |
| - 'password' => bcrypt($data['password']), |
70 |
| - ]); |
| 35 | + $user = new User; |
| 36 | + $user->name = trim($request->name); |
| 37 | + $user->email = trim(strtolower($request->email)); |
| 38 | + $user->password = bcrypt($request->password); |
| 39 | + $user->save(); |
| 40 | + |
| 41 | + $token = JWTAuth::fromUser($user); |
| 42 | + |
| 43 | + return response()->success(compact('user', 'token')); |
71 | 44 | }
|
72 | 45 | }
|
0 commit comments