Skip to content

Commit 773cd66

Browse files
authored
Get rid of laravel/ui (#325)
* Get rid of laravel/ui * Apply fixes from StyleCI (#326)
1 parent 5c08d92 commit 773cd66

File tree

7 files changed

+517
-15
lines changed

7 files changed

+517
-15
lines changed

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
"ext-json": "*",
2323
"doctrine/dbal": "^2.10",
2424
"illuminate/support": "^7.0|^8.0",
25-
"laravel/ui": "^2.0|^3.0",
2625
"spatie/once": "^2.2"
2726
},
2827
"require-dev": {
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
<?php
2+
3+
namespace Binaryk\LaravelRestify\Services\Concerns;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Support\Facades\Auth;
7+
use Illuminate\Validation\ValidationException;
8+
9+
trait AuthenticatesUsers
10+
{
11+
use ThrottlesLogins;
12+
13+
/**
14+
* Get the post register / login redirect path.
15+
*
16+
* @return string
17+
*/
18+
public function redirectPath()
19+
{
20+
if (method_exists($this, 'redirectTo')) {
21+
return $this->redirectTo();
22+
}
23+
24+
return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
25+
}
26+
27+
/**
28+
* Handle a login request to the application.
29+
*
30+
* @param \Illuminate\Http\Request $request
31+
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response
32+
*/
33+
public function login(Request $request)
34+
{
35+
$this->validateLogin($request);
36+
37+
// If the class is using the ThrottlesLogins trait, we can automatically throttle
38+
// the login attempts for this application. We'll key this by the username and
39+
// the IP address of the client making these requests into this application.
40+
if ($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+
protected function validateLogin(Request $request)
65+
{
66+
$request->validate([
67+
$this->username() => 'required|string',
68+
'password' => 'required|string',
69+
]);
70+
}
71+
72+
/**
73+
* Attempt to log the user into the application.
74+
*
75+
* @param \Illuminate\Http\Request $request
76+
* @return bool
77+
*/
78+
protected function attemptLogin(Request $request)
79+
{
80+
return $this->guard()->attempt(
81+
$this->credentials($request), $request->has('remember')
82+
);
83+
}
84+
85+
/**
86+
* Get the needed authorization credentials from the request.
87+
*
88+
* @param \Illuminate\Http\Request $request
89+
* @return array
90+
*/
91+
protected function credentials(Request $request)
92+
{
93+
return $request->only($this->username(), 'password');
94+
}
95+
96+
/**
97+
* Send the response after the user was authenticated.
98+
*
99+
* @param \Illuminate\Http\Request $request
100+
* @return \Illuminate\Http\Response
101+
*/
102+
protected function sendLoginResponse(Request $request)
103+
{
104+
$request->session()->regenerate();
105+
106+
$this->clearLoginAttempts($request);
107+
108+
return $this->authenticated($request, $this->guard()->user())
109+
?: redirect()->intended($this->redirectPath());
110+
}
111+
112+
/**
113+
* The user has been authenticated.
114+
*
115+
* @param \Illuminate\Http\Request $request
116+
* @param mixed $user
117+
* @return mixed
118+
*/
119+
protected function authenticated(Request $request, $user)
120+
{
121+
//
122+
}
123+
124+
/**
125+
* Get the failed login response instance.
126+
*
127+
* @param \Illuminate\Http\Request $request
128+
* @return \Illuminate\Http\RedirectResponse
129+
*/
130+
protected function sendFailedLoginResponse(Request $request)
131+
{
132+
throw ValidationException::withMessages([
133+
$this->username() => [trans('auth.failed')],
134+
]);
135+
}
136+
137+
/**
138+
* Get the login username to be used by the controller.
139+
*
140+
* @return string
141+
*/
142+
public function username()
143+
{
144+
return 'email';
145+
}
146+
147+
/**
148+
* Log the user out of the application.
149+
*
150+
* @param \Illuminate\Http\Request $request
151+
* @return \Illuminate\Http\Response
152+
*/
153+
public function logout(Request $request)
154+
{
155+
$this->guard()->logout();
156+
157+
$request->session()->invalidate();
158+
159+
return redirect('/');
160+
}
161+
162+
/**
163+
* Get the guard to be used during authentication.
164+
*
165+
* @return \Illuminate\Contracts\Auth\StatefulGuard
166+
*/
167+
protected function guard()
168+
{
169+
return Auth::guard();
170+
}
171+
}
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
<?php
2+
3+
namespace Binaryk\LaravelRestify\Services\Concerns;
4+
5+
use Illuminate\Auth\Events\PasswordReset;
6+
use Illuminate\Http\Request;
7+
use Illuminate\Support\Facades\Auth;
8+
use Illuminate\Support\Facades\Hash;
9+
use Illuminate\Support\Facades\Password;
10+
use Illuminate\Support\Str;
11+
12+
trait ResetsPasswords
13+
{
14+
/**
15+
* Get the post register / login redirect path.
16+
*
17+
* @return string
18+
*/
19+
public function redirectPath()
20+
{
21+
if (method_exists($this, 'redirectTo')) {
22+
return $this->redirectTo();
23+
}
24+
25+
return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
26+
}
27+
28+
/**
29+
* Display the password reset view for the given token.
30+
*
31+
* If no token is present, display the link request form.
32+
*
33+
* @param \Illuminate\Http\Request $request
34+
* @param string|null $token
35+
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
36+
*/
37+
public function showResetForm(Request $request, $token = null)
38+
{
39+
return view('auth.passwords.reset')->with(
40+
['token' => $token, 'email' => $request->email]
41+
);
42+
}
43+
44+
/**
45+
* Reset the given user's password.
46+
*
47+
* @param \Illuminate\Http\Request $request
48+
* @return \Illuminate\Http\RedirectResponse
49+
*/
50+
public function reset(Request $request)
51+
{
52+
$request->validate($this->rules(), $this->validationErrorMessages());
53+
54+
// Here we will attempt to reset the user's password. If it is successful we
55+
// will update the password on an actual user model and persist it to the
56+
// database. Otherwise we will parse the error and return the response.
57+
$response = $this->broker()->reset(
58+
$this->credentials($request), function ($user, $password) {
59+
$this->resetPassword($user, $password);
60+
}
61+
);
62+
63+
// If the password was successfully reset, we will redirect the user back to
64+
// the application's home authenticated view. If there is an error we can
65+
// redirect them back to where they came from with their error message.
66+
return $response == Password::PASSWORD_RESET
67+
? $this->sendResetResponse($response)
68+
: $this->sendResetFailedResponse($request, $response);
69+
}
70+
71+
/**
72+
* Get the password reset validation rules.
73+
*
74+
* @return array
75+
*/
76+
protected function rules()
77+
{
78+
return [
79+
'token' => 'required',
80+
'email' => 'required|email',
81+
'password' => 'required|confirmed|min:6',
82+
];
83+
}
84+
85+
/**
86+
* Get the password reset validation error messages.
87+
*
88+
* @return array
89+
*/
90+
protected function validationErrorMessages()
91+
{
92+
return [];
93+
}
94+
95+
/**
96+
* Get the password reset credentials from the request.
97+
*
98+
* @param \Illuminate\Http\Request $request
99+
* @return array
100+
*/
101+
protected function credentials(Request $request)
102+
{
103+
return $request->only(
104+
'email', 'password', 'password_confirmation', 'token'
105+
);
106+
}
107+
108+
/**
109+
* Reset the given user's password.
110+
*
111+
* @param \Illuminate\Contracts\Auth\CanResetPassword $user
112+
* @param string $password
113+
* @return void
114+
*/
115+
protected function resetPassword($user, $password)
116+
{
117+
$user->password = Hash::make($password);
118+
119+
$user->setRememberToken(Str::random(60));
120+
121+
$user->save();
122+
123+
event(new PasswordReset($user));
124+
125+
$this->guard()->login($user);
126+
}
127+
128+
/**
129+
* Get the response for a successful password reset.
130+
*
131+
* @param string $response
132+
* @return \Illuminate\Http\RedirectResponse
133+
*/
134+
protected function sendResetResponse($response)
135+
{
136+
return redirect($this->redirectPath())
137+
->with('status', trans($response));
138+
}
139+
140+
/**
141+
* Get the response for a failed password reset.
142+
*
143+
* @param \Illuminate\Http\Request
144+
* @param string $response
145+
* @return \Illuminate\Http\RedirectResponse
146+
*/
147+
protected function sendResetFailedResponse(Request $request, $response)
148+
{
149+
return redirect()->back()
150+
->withInput($request->only('email'))
151+
->withErrors(['email' => trans($response)]);
152+
}
153+
154+
/**
155+
* Get the broker to be used during password reset.
156+
*
157+
* @return \Illuminate\Contracts\Auth\PasswordBroker
158+
*/
159+
public function broker()
160+
{
161+
return Password::broker();
162+
}
163+
164+
/**
165+
* Get the guard to be used during password reset.
166+
*
167+
* @return \Illuminate\Contracts\Auth\StatefulGuard
168+
*/
169+
protected function guard()
170+
{
171+
return Auth::guard();
172+
}
173+
}

0 commit comments

Comments
 (0)