feature: keep sidebar and add service layer
This commit is contained in:
@@ -2,16 +2,20 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Services\Auth\Contracts\AuthServiceInterface;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use App\Models\User;
|
||||
|
||||
class AuthController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private AuthServiceInterface $authService
|
||||
) {}
|
||||
|
||||
public function showLoginForm()
|
||||
{
|
||||
if (Auth::check()) {
|
||||
return $this->redirectBasedOnRole(Auth::user());
|
||||
return redirect($this->authService->getRedirectRouteForUser(Auth::user()));
|
||||
}
|
||||
return view('login');
|
||||
}
|
||||
@@ -19,48 +23,24 @@ class AuthController extends Controller
|
||||
public function login(Request $request)
|
||||
{
|
||||
$credentials = $request->validate([
|
||||
'mail' => 'required|email',
|
||||
'password' => 'required'
|
||||
'mail' => 'required|email|max:255',
|
||||
'password' => 'required|string|max:100',
|
||||
]);
|
||||
|
||||
if (Auth::attempt(['mail' => $credentials['mail'], 'password' => $credentials['password']])) {
|
||||
$request->session()->regenerate();
|
||||
|
||||
$user = Auth::user();
|
||||
|
||||
if ($user->status != User::STATUS_ACTIVE) {
|
||||
Auth::logout();
|
||||
return back()->withErrors([
|
||||
'mail' => 'Tài khoản của bạn đã bị khóa hoặc bạn đã nghỉ việc.',
|
||||
]);
|
||||
}
|
||||
|
||||
return $this->redirectBasedOnRole($user);
|
||||
$result = $this->authService->attemptLogin($credentials, $request);
|
||||
|
||||
if (!$result['success']) {
|
||||
return back()->withInput($request->only('mail'))->withErrors([
|
||||
$result['error_key'] => $result['error_msg'],
|
||||
]);
|
||||
}
|
||||
|
||||
return back()->withErrors([
|
||||
'mail' => 'Email hoặc mật khẩu không chính xác.',
|
||||
]);
|
||||
return redirect($this->authService->getRedirectRouteForUser($result['user']));
|
||||
}
|
||||
|
||||
public function logout(Request $request)
|
||||
{
|
||||
Auth::logout();
|
||||
$request->session()->invalidate();
|
||||
$request->session()->regenerateToken();
|
||||
$this->authService->logout($request);
|
||||
return redirect('/login');
|
||||
}
|
||||
|
||||
private function redirectBasedOnRole($user)
|
||||
{
|
||||
if ($user->first_login == User::FIRST_LOGIN_TRUE) {
|
||||
return redirect()->route('user.change_password');
|
||||
}
|
||||
|
||||
if ($user->role == User::ROLE_ADMIN) {
|
||||
return redirect()->route('admin.dashboard');
|
||||
}
|
||||
|
||||
return redirect()->route('user.dashboard');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user