1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LearnParty\Http\Controllers\Auth; |
4
|
|
|
|
5
|
|
|
use Validator; |
6
|
|
|
use LearnParty\Http\Controllers\Controller; |
7
|
|
|
use Illuminate\Foundation\Auth\ThrottlesLogins; |
8
|
|
|
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers; |
9
|
|
|
use LearnParty\User; |
10
|
|
|
use Socialite; |
11
|
|
|
use Auth; |
12
|
|
|
|
13
|
|
|
class AuthController extends Controller |
14
|
|
|
{ |
15
|
|
|
/* |
16
|
|
|
|-------------------------------------------------------------------------- |
17
|
|
|
| Registration & Login Controller |
18
|
|
|
|-------------------------------------------------------------------------- |
19
|
|
|
| |
20
|
|
|
| This controller handles the registration of new users, as well as the |
21
|
|
|
| authentication of existing users. By default, this controller uses |
22
|
|
|
| a simple trait to add these behaviors. Why don't you explore it? |
23
|
|
|
| |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
use AuthenticatesAndRegistersUsers, ThrottlesLogins; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Where to redirect users after login / registration. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $redirectTo = '/'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Create a new authentication controller instance. |
37
|
|
|
* @codeCoverageIgnore |
38
|
|
|
* |
39
|
|
|
* @return void |
|
|
|
|
40
|
|
|
*/ |
41
|
|
|
public function __construct() |
42
|
|
|
{ |
43
|
|
|
parent::__construct(); |
44
|
|
|
$this->middleware($this->guestMiddleware(), ['except' => 'logout']); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get a validator for an incoming registration request. |
49
|
|
|
* |
50
|
|
|
* @param array $data |
51
|
|
|
* @return \Illuminate\Contracts\Validation\Validator |
52
|
|
|
*/ |
53
|
|
|
protected function validator(array $data) |
54
|
|
|
{ |
55
|
|
|
return Validator::make($data, [ |
56
|
|
|
'name' => 'required|max:255', |
57
|
|
|
'username' => 'required|max:255|unique:users', |
58
|
|
|
'email' => 'required|email|max:255|unique:users', |
59
|
|
|
'password' => 'required|min:6|confirmed', |
60
|
|
|
]); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Create a new user instance after a valid registration. |
65
|
|
|
* |
66
|
|
|
* @codeCoverageIgnore |
67
|
|
|
* |
68
|
|
|
* @param array $data |
69
|
|
|
* @return User |
70
|
|
|
*/ |
71
|
|
|
public function create(array $data) |
72
|
|
|
{ |
73
|
|
|
return User::create([ |
74
|
|
|
'name' => $data['name'], |
75
|
|
|
'username' => $data['username'], |
76
|
|
|
'email' => $data['email'], |
77
|
|
|
'password' => bcrypt($data['password']), |
78
|
|
|
'provider_id' => 'traditional', |
79
|
|
|
'provider' => 'traditional', |
80
|
|
|
'avatar' => 'https://en.gravatar.com/userimage/102347280/b3e9c138c1548147b7ff3f9a2a1d9bb0.png?size=200', |
81
|
|
|
]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Redirect the user to the authentication service. |
86
|
|
|
* |
87
|
|
|
* @codeCoverageIgnore |
88
|
|
|
* |
89
|
|
|
* @return Response |
90
|
|
|
*/ |
91
|
|
|
public function redirectToProvider($provider) |
92
|
|
|
{ |
93
|
|
|
return Socialite::driver($provider)->redirect(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Obtain the user information from authentication service. |
98
|
|
|
* |
99
|
|
|
* @codeCoverageIgnore |
100
|
|
|
* |
101
|
|
|
* @return Response |
102
|
|
|
*/ |
103
|
|
|
public function handleProviderCallback($provider) |
104
|
|
|
{ |
105
|
|
|
$user = Socialite::driver($provider)->user(); |
106
|
|
|
Auth::login($this->userRepository->authenticateUser($user, $provider), true); |
107
|
|
|
|
108
|
|
|
return redirect($this->redirectTo); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
Adding a
@return
annotation 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.