Skip to content

Commit bb29dd8

Browse files
committed
AuthController #202
1 parent b5cd6d2 commit bb29dd8

File tree

4 files changed

+29
-121
lines changed

4 files changed

+29
-121
lines changed

app/Http/Controllers/Auth/AuthController.php

Lines changed: 28 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,71 +2,44 @@
22

33
namespace App\Http\Controllers\Auth;
44

5+
use JWTAuth;
56
use App\User;
6-
use Validator;
77
use App\Http\Controllers\Controller;
8-
use Illuminate\Foundation\Auth\ThrottlesLogins;
9-
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
108

119
class AuthController extends Controller
1210
{
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)
3912
{
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'));
4125
}
4226

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()
5028
{
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',
5533
]);
56-
}
5734

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

app/Http/Controllers/Auth/PasswordController.php

Lines changed: 0 additions & 32 deletions
This file was deleted.

app/Http/Controllers/LoginController.php

Lines changed: 0 additions & 33 deletions
This file was deleted.

app/Http/routes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
$api->group([], function ($api) {
3434

35-
$api->post('users/login', 'LoginController@login');
35+
$api->controller('auth', 'Auth\AuthController');
3636

3737
});
3838

0 commit comments

Comments
 (0)