|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Auth; |
|
4
|
|
|
|
|
5
|
|
|
use App\Http\Controllers\Controller; |
|
6
|
|
|
use App\User; |
|
7
|
|
|
use Chrisbjr\ApiGuard\Models\ApiKey; |
|
8
|
|
|
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers; |
|
9
|
|
|
use Illuminate\Foundation\Auth\ThrottlesLogins; |
|
10
|
|
|
use Validator; |
|
11
|
|
|
|
|
12
|
|
|
class AuthController extends Controller |
|
13
|
|
|
{ |
|
14
|
|
|
/* |
|
15
|
|
|
|-------------------------------------------------------------------------- |
|
16
|
|
|
| Registration & Login Controller |
|
17
|
|
|
|-------------------------------------------------------------------------- |
|
18
|
|
|
| |
|
19
|
|
|
| This controller handles the registration of new users, as well as the |
|
20
|
|
|
| authentication of existing users. By default, this controller uses |
|
21
|
|
|
| a simple trait to add these behaviors. Why don't you explore it? |
|
22
|
|
|
| |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
use AuthenticatesAndRegistersUsers, ThrottlesLogins; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Where to redirect users after login / registration. |
|
29
|
|
|
* |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $redirectTo = '/upload'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Create a new authentication controller instance. |
|
36
|
|
|
*/ |
|
37
|
8 |
|
public function __construct() |
|
38
|
|
|
{ |
|
39
|
8 |
|
$this->middleware($this->guestMiddleware(), ['except' => 'logout']); |
|
40
|
8 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Get a validator for an incoming registration request. |
|
44
|
|
|
* |
|
45
|
|
|
* @param array $data |
|
46
|
|
|
* |
|
47
|
|
|
* @return \Illuminate\Contracts\Validation\Validator |
|
48
|
|
|
*/ |
|
49
|
2 |
|
protected function validator(array $data) |
|
50
|
|
|
{ |
|
51
|
2 |
|
return Validator::make($data, [ |
|
52
|
2 |
|
'name' => 'required|max:255', |
|
53
|
2 |
|
'email' => 'required|email|max:255|unique:users', |
|
54
|
2 |
|
'password' => 'required|confirmed|min:6', |
|
55
|
2 |
|
]); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Create a new user instance after a valid registration. |
|
60
|
|
|
* |
|
61
|
|
|
* @param array $data |
|
62
|
|
|
* |
|
63
|
|
|
* @return User |
|
64
|
|
|
*/ |
|
65
|
1 |
|
protected function create(array $data) |
|
66
|
|
|
{ |
|
67
|
1 |
|
$user = User::create([ |
|
68
|
1 |
|
'name' => $data['name'], |
|
69
|
1 |
|
'email' => $data['email'], |
|
70
|
1 |
|
'password' => bcrypt($data['password']), |
|
71
|
1 |
|
]); |
|
72
|
|
|
|
|
73
|
1 |
|
$this->createUserApiKey($user); |
|
74
|
|
|
|
|
75
|
1 |
|
return $user; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param User $user |
|
80
|
|
|
* |
|
81
|
|
|
* @return mixed |
|
82
|
|
|
*/ |
|
83
|
1 |
|
private function createUserApiKey(User $user) |
|
84
|
|
|
{ |
|
85
|
1 |
|
$apiKey = ApiKey::make($user->id); |
|
86
|
1 |
|
$user->apiKey()->save($apiKey); |
|
87
|
1 |
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|