feature: keep sidebar and add service layer
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Auth;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Services\Auth\Contracts\AuthServiceInterface;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class AuthService implements AuthServiceInterface
|
||||
{
|
||||
public function attemptLogin(array $credentials, Request $request): array
|
||||
{
|
||||
if (!Auth::attempt(['mail' => $credentials['mail'], 'password' => $credentials['password']])) {
|
||||
return ['success' => false, 'error_key' => 'mail', 'error_msg' => __('auth.failed')];
|
||||
}
|
||||
|
||||
$request->session()->regenerate();
|
||||
|
||||
$user = Auth::user();
|
||||
|
||||
if ($user->status != User::STATUS_ACTIVE) {
|
||||
Auth::logout();
|
||||
return ['success' => false, 'error_key' => 'mail', 'error_msg' => __('auth.inactive')];
|
||||
}
|
||||
|
||||
return ['success' => true, 'user' => $user];
|
||||
}
|
||||
|
||||
public function logout(Request $request): void
|
||||
{
|
||||
Auth::logout();
|
||||
$request->session()->invalidate();
|
||||
$request->session()->regenerateToken();
|
||||
}
|
||||
|
||||
public function getRedirectRouteForUser(User $user): string
|
||||
{
|
||||
if ($user->first_login == User::FIRST_LOGIN_TRUE) {
|
||||
return route('user.change_password');
|
||||
}
|
||||
|
||||
if ($user->role == User::ROLE_ADMIN) {
|
||||
return route('admin.dashboard');
|
||||
}
|
||||
|
||||
return route('user.dashboard');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Auth\Contracts;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
interface AuthServiceInterface
|
||||
{
|
||||
public function attemptLogin(array $credentials, Request $request): array;
|
||||
|
||||
public function logout(Request $request): void;
|
||||
|
||||
public function getRedirectRouteForUser(User $user): string;
|
||||
}
|
||||
Reference in New Issue
Block a user