|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Auth; |
|
4
|
|
|
|
|
5
|
|
|
use App\Http\Controllers\Controller; |
|
6
|
|
|
use App\Notifications\EmailVerification; |
|
7
|
|
|
use App\Role; |
|
8
|
|
|
use App\Session\Flash; |
|
9
|
|
|
use App\User; |
|
10
|
|
|
use DB; |
|
11
|
|
|
use Exception; |
|
12
|
|
|
use Illuminate\Auth\Events\Registered; |
|
13
|
|
|
use Illuminate\Foundation\Auth\RegistersUsers; |
|
14
|
|
|
use Illuminate\Http\Request; |
|
15
|
|
|
use Illuminate\Support\Facades\Validator; |
|
16
|
|
|
|
|
17
|
|
|
class RegisterController extends Controller |
|
18
|
|
|
{ |
|
19
|
|
|
/* |
|
20
|
|
|
|-------------------------------------------------------------------------- |
|
21
|
|
|
| Register Controller |
|
22
|
|
|
|-------------------------------------------------------------------------- |
|
23
|
|
|
| |
|
24
|
|
|
| This controller handles the registration of new users as well as their |
|
25
|
|
|
| validation and creation. By default this controller uses a trait to |
|
26
|
|
|
| provide this functionality without requiring any additional code. |
|
27
|
|
|
| |
|
28
|
|
|
*/ |
|
29
|
|
|
|
|
30
|
|
|
use RegistersUsers; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Where to redirect users after registration. |
|
34
|
|
|
* |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $redirectTo = '/'; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Create a new controller instance. |
|
41
|
|
|
* |
|
42
|
|
|
* @return void |
|
|
|
|
|
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct() |
|
45
|
|
|
{ |
|
46
|
|
|
$this->middleware('guest'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get a validator for an incoming registration request. |
|
51
|
|
|
* |
|
52
|
|
|
* @param array $data |
|
53
|
|
|
* @return \Illuminate\Contracts\Validation\Validator |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function validator(array $data) |
|
56
|
|
|
{ |
|
57
|
|
|
return Validator::make($data, [ |
|
58
|
|
|
'name' => 'required|max:255', |
|
59
|
|
|
'email' => 'required|email|max:255|unique:users', |
|
60
|
|
|
'password' => 'required|min:6|confirmed', |
|
61
|
|
|
]); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Create a new user instance after a valid registration. |
|
66
|
|
|
* |
|
67
|
|
|
* @param array $data |
|
68
|
|
|
* @return User |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function create(array $data) |
|
71
|
|
|
{ |
|
72
|
|
|
// Create the user |
|
73
|
|
|
$user = new User(); |
|
74
|
|
|
$user->fill([ |
|
75
|
|
|
'name' => $data['name'], |
|
76
|
|
|
'email' => $data['email'], |
|
77
|
|
|
'password' => bcrypt($data['password']), |
|
78
|
|
|
'email_token' => $user->createEmailToken(), |
|
79
|
|
|
])->save(); |
|
80
|
|
|
|
|
81
|
|
|
// Add role |
|
82
|
|
|
$role = Role::whereName('User')->first(); |
|
83
|
|
|
|
|
84
|
|
|
$user->assignRole($role); |
|
85
|
|
|
|
|
86
|
|
|
return $user; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Handle a registration request for the application. |
|
91
|
|
|
* |
|
92
|
|
|
* @param \Illuminate\Http\Request $request |
|
93
|
|
|
* @return \Illuminate\Http\RedirectResponse |
|
94
|
|
|
*/ |
|
95
|
|
|
public function register(Request $request) |
|
96
|
|
|
{ |
|
97
|
|
|
$this->validator($request->all())->validate(); |
|
98
|
|
|
|
|
99
|
|
|
// A number of things are happening here so we are enclosing in transaction |
|
100
|
|
|
// In case anything goes wrong in the process |
|
101
|
|
|
DB::beginTransaction(); |
|
102
|
|
|
|
|
103
|
|
|
try { |
|
104
|
|
|
$user = $this->create($request->all()); |
|
105
|
|
|
event(new Registered($user)); |
|
106
|
|
|
|
|
107
|
|
|
// Send verification email |
|
108
|
|
|
$user->notify(new EmailVerification($user)); |
|
109
|
|
|
|
|
110
|
|
|
Flash::success('Signup successful. Please check your email for activation instructions'); |
|
111
|
|
|
DB::commit(); |
|
112
|
|
|
|
|
113
|
|
|
return redirect()->to('/'); |
|
114
|
|
|
} catch (Exception $e) { |
|
115
|
|
|
dd($e->getMessage()); |
|
116
|
|
|
|
|
117
|
|
|
DB::rollback(); |
|
118
|
|
|
|
|
119
|
|
|
return back(); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function showRegistrationForm() |
|
124
|
|
|
{ |
|
125
|
|
|
return view('frontend.auth.register'); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function verify($token) |
|
129
|
|
|
{ |
|
130
|
|
|
User::where('email_token', $token)->firstOrFail()->verified(); |
|
131
|
|
|
|
|
132
|
|
|
Flash::success('Activation successful!'); |
|
133
|
|
|
|
|
134
|
|
|
return redirect('login'); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.