Skip to content

Commit a650eb9

Browse files
committed
moved Auth classes to Backpack - it's now completely independent from laravel/ui
1 parent 90683e6 commit a650eb9

13 files changed

+884
-12
lines changed

composer.json

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,22 @@
3232
}
3333
],
3434
"require": {
35-
"laravel/framework": "^7.0",
35+
"laravel/framework": "^7.0|^6.0|5.8.*",
3636
"prologue/alerts": "^0.4.1",
3737
"creativeorange/gravatar": "~1.0",
3838
"nesbot/carbon": "^2.14",
3939
"ocramius/package-versions": "^1.4",
4040
"laravel/helpers": "^1.1",
4141
"doctrine/dbal": "^2.5",
4242
"intervention/image": "^2.3",
43-
"guzzlehttp/guzzle": "^6.3",
44-
"laravel/ui": "2.*"
43+
"guzzlehttp/guzzle": "^6.3"
4544
},
4645
"require-dev": {
47-
"phpunit/phpunit": "~8.0",
48-
"scrutinizer/ocular": "~1.7",
49-
"orchestra/testbench": "^5.0",
50-
"orchestra/database": "^5.0@dev",
51-
"spatie/laravel-translatable": "^4.0"
46+
"phpunit/phpunit": "~8.0|~7.0",
47+
"scrutinizer/ocular": "~1.7|~1.1",
48+
"orchestra/testbench": "^5.0|^3.0",
49+
"orchestra/database": "^5.0@dev|3.8.x-dev",
50+
"spatie/laravel-translatable": "^4.0|^3.1.3"
5251
},
5352
"autoload": {
5453
"psr-4": {

src/app/Http/Controllers/Auth/ForgotPasswordController.php

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

33
namespace Backpack\CRUD\app\Http\Controllers\Auth;
44

5-
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;
5+
use Backpack\CRUD\app\Auth\SendsPasswordResetEmails;
66
use Illuminate\Routing\Controller;
77
use Illuminate\Support\Facades\Password;
88

src/app/Http/Controllers/Auth/LoginController.php

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

33
namespace Backpack\CRUD\app\Http\Controllers\Auth;
44

5-
use Illuminate\Foundation\Auth\AuthenticatesUsers;
5+
use Backpack\CRUD\app\Library\Auth\AuthenticatesUsers;
66
use Illuminate\Http\Request;
77
use Illuminate\Routing\Controller;
88

src/app/Http/Controllers/Auth/RegisterController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Backpack\CRUD\app\Http\Controllers\Auth;
44

55
use Illuminate\Auth\Events\Registered;
6-
use Illuminate\Foundation\Auth\RegistersUsers;
6+
use Backpack\CRUD\app\Library\Auth\RegistersUsers;
77
use Illuminate\Http\Request;
88
use Illuminate\Routing\Controller;
99
use Validator;

src/app/Http/Controllers/Auth/ResetPasswordController.php

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

33
namespace Backpack\CRUD\app\Http\Controllers\Auth;
44

5-
use Illuminate\Foundation\Auth\ResetsPasswords;
5+
use Backpack\Crud\app\Library\Auth\ResetsPasswords;
66
use Illuminate\Http\Request;
77
use Illuminate\Routing\Controller;
88
use Illuminate\Support\Facades\Password;
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
<?php
2+
3+
namespace Backpack\CRUD\app\Library\Auth;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Http\Response;
7+
use Illuminate\Support\Facades\Auth;
8+
use Illuminate\Validation\ValidationException;
9+
10+
trait AuthenticatesUsers
11+
{
12+
use RedirectsUsers, ThrottlesLogins;
13+
14+
/**
15+
* Show the application's login form.
16+
*
17+
* @return \Illuminate\Http\Response
18+
*/
19+
public function showLoginForm()
20+
{
21+
return view('auth.login');
22+
}
23+
24+
/**
25+
* Handle a login request to the application.
26+
*
27+
* @param \Illuminate\Http\Request $request
28+
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Http\JsonResponse
29+
*
30+
* @throws \Illuminate\Validation\ValidationException
31+
*/
32+
public function login(Request $request)
33+
{
34+
$this->validateLogin($request);
35+
36+
// If the class is using the ThrottlesLogins trait, we can automatically throttle
37+
// the login attempts for this application. We'll key this by the username and
38+
// the IP address of the client making these requests into this application.
39+
if (method_exists($this, 'hasTooManyLoginAttempts') &&
40+
$this->hasTooManyLoginAttempts($request)) {
41+
$this->fireLockoutEvent($request);
42+
43+
return $this->sendLockoutResponse($request);
44+
}
45+
46+
if ($this->attemptLogin($request)) {
47+
return $this->sendLoginResponse($request);
48+
}
49+
50+
// If the login attempt was unsuccessful we will increment the number of attempts
51+
// to login and redirect the user back to the login form. Of course, when this
52+
// user surpasses their maximum number of attempts they will get locked out.
53+
$this->incrementLoginAttempts($request);
54+
55+
return $this->sendFailedLoginResponse($request);
56+
}
57+
58+
/**
59+
* Validate the user login request.
60+
*
61+
* @param \Illuminate\Http\Request $request
62+
* @return void
63+
*
64+
* @throws \Illuminate\Validation\ValidationException
65+
*/
66+
protected function validateLogin(Request $request)
67+
{
68+
$request->validate([
69+
$this->username() => 'required|string',
70+
'password' => 'required|string',
71+
]);
72+
}
73+
74+
/**
75+
* Attempt to log the user into the application.
76+
*
77+
* @param \Illuminate\Http\Request $request
78+
* @return bool
79+
*/
80+
protected function attemptLogin(Request $request)
81+
{
82+
return $this->guard()->attempt(
83+
$this->credentials($request), $request->filled('remember')
84+
);
85+
}
86+
87+
/**
88+
* Get the needed authorization credentials from the request.
89+
*
90+
* @param \Illuminate\Http\Request $request
91+
* @return array
92+
*/
93+
protected function credentials(Request $request)
94+
{
95+
return $request->only($this->username(), 'password');
96+
}
97+
98+
/**
99+
* Send the response after the user was authenticated.
100+
*
101+
* @param \Illuminate\Http\Request $request
102+
* @return \Illuminate\Http\Response
103+
*/
104+
protected function sendLoginResponse(Request $request)
105+
{
106+
$request->session()->regenerate();
107+
108+
$this->clearLoginAttempts($request);
109+
110+
if ($response = $this->authenticated($request, $this->guard()->user())) {
111+
return $response;
112+
}
113+
114+
return $request->wantsJson()
115+
? new Response('', 204)
116+
: redirect()->intended($this->redirectPath());
117+
}
118+
119+
/**
120+
* The user has been authenticated.
121+
*
122+
* @param \Illuminate\Http\Request $request
123+
* @param mixed $user
124+
* @return mixed
125+
*/
126+
protected function authenticated(Request $request, $user)
127+
{
128+
//
129+
}
130+
131+
/**
132+
* Get the failed login response instance.
133+
*
134+
* @param \Illuminate\Http\Request $request
135+
* @return \Symfony\Component\HttpFoundation\Response
136+
*
137+
* @throws \Illuminate\Validation\ValidationException
138+
*/
139+
protected function sendFailedLoginResponse(Request $request)
140+
{
141+
throw ValidationException::withMessages([
142+
$this->username() => [trans('auth.failed')],
143+
]);
144+
}
145+
146+
/**
147+
* Get the login username to be used by the controller.
148+
*
149+
* @return string
150+
*/
151+
public function username()
152+
{
153+
return 'email';
154+
}
155+
156+
/**
157+
* Log the user out of the application.
158+
*
159+
* @param \Illuminate\Http\Request $request
160+
* @return \Illuminate\Http\Response
161+
*/
162+
public function logout(Request $request)
163+
{
164+
$this->guard()->logout();
165+
166+
$request->session()->invalidate();
167+
168+
$request->session()->regenerateToken();
169+
170+
if ($response = $this->loggedOut($request)) {
171+
return $response;
172+
}
173+
174+
return $request->wantsJson()
175+
? new Response('', 204)
176+
: redirect('/');
177+
}
178+
179+
/**
180+
* The user has logged out of the application.
181+
*
182+
* @param \Illuminate\Http\Request $request
183+
* @return mixed
184+
*/
185+
protected function loggedOut(Request $request)
186+
{
187+
//
188+
}
189+
190+
/**
191+
* Get the guard to be used during authentication.
192+
*
193+
* @return \Illuminate\Contracts\Auth\StatefulGuard
194+
*/
195+
protected function guard()
196+
{
197+
return Auth::guard();
198+
}
199+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace Backpack\CRUD\app\Library\Auth;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Http\Response;
7+
8+
trait ConfirmsPasswords
9+
{
10+
use RedirectsUsers;
11+
12+
/**
13+
* Display the password confirmation view.
14+
*
15+
* @return \Illuminate\Http\Response
16+
*/
17+
public function showConfirmForm()
18+
{
19+
return view('auth.passwords.confirm');
20+
}
21+
22+
/**
23+
* Confirm the given user's password.
24+
*
25+
* @param \Illuminate\Http\Request $request
26+
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
27+
*/
28+
public function confirm(Request $request)
29+
{
30+
$request->validate($this->rules(), $this->validationErrorMessages());
31+
32+
$this->resetPasswordConfirmationTimeout($request);
33+
34+
return $request->wantsJson()
35+
? new Response('', 204)
36+
: redirect()->intended($this->redirectPath());
37+
}
38+
39+
/**
40+
* Reset the password confirmation timeout.
41+
*
42+
* @param \Illuminate\Http\Request $request
43+
* @return void
44+
*/
45+
protected function resetPasswordConfirmationTimeout(Request $request)
46+
{
47+
$request->session()->put('auth.password_confirmed_at', time());
48+
}
49+
50+
/**
51+
* Get the password confirmation validation rules.
52+
*
53+
* @return array
54+
*/
55+
protected function rules()
56+
{
57+
return [
58+
'password' => 'required|password',
59+
];
60+
}
61+
62+
/**
63+
* Get the password confirmation validation error messages.
64+
*
65+
* @return array
66+
*/
67+
protected function validationErrorMessages()
68+
{
69+
return [];
70+
}
71+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Backpack\CRUD\app\Library\Auth;
4+
5+
trait RedirectsUsers
6+
{
7+
/**
8+
* Get the post register / login redirect path.
9+
*
10+
* @return string
11+
*/
12+
public function redirectPath()
13+
{
14+
if (method_exists($this, 'redirectTo')) {
15+
return $this->redirectTo();
16+
}
17+
18+
return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
19+
}
20+
}

0 commit comments

Comments
 (0)