Last active
December 11, 2023 07:17
-
-
Save aldhinya/03ad09a32673603f217dda4c1120d515 to your computer and use it in GitHub Desktop.
Manual Auth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
return [ | |
'defaults' => [ | |
'guard' => 'web', | |
'passwords' => 'users', | |
], | |
'guards' => [ | |
'admin' => [ | |
'driver' => 'session', | |
'provider' => 'admin', | |
], | |
'web' => [ | |
'driver' => 'session', | |
'provider' => 'users', | |
], | |
], | |
'providers' => [ | |
'admin' => [ | |
'driver' => 'eloquent', | |
'model' => App\Models\Users::class, | |
], | |
'users' => [ | |
'driver' => 'database', | |
'table' => 'users', | |
], | |
], | |
'passwords' => [ | |
'users' => [ | |
'provider' => 'users', | |
'table' => 'password_resets', | |
'expire' => 60, | |
'throttle' => 60, | |
], | |
], | |
'password_timeout' => 10800, | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use App\Models\Users; | |
use Illuminate\Http\Request; | |
use Illuminate\Support\Facades\Auth; | |
class AuthController extends Controller | |
{ | |
public function login() | |
{ | |
if(Auth::guard('admin')->check()){ | |
return redirect(route('admin.dashboard'))->with(['jenis' => 'success', 'pesan' => 'Anda Sudah Login, Selamat Datang Kembali.']); | |
} | |
return view('login'); | |
} | |
public function login_proses(Request $request) | |
{ | |
$request->validate([ | |
'username' => 'required', | |
'password' => 'required', | |
]); | |
$cekUser = Users::where('username', $request->username)->first(); | |
if($cekUser) { | |
if ($cekUser->status!=1) { | |
return redirect()->back()->with(['jenis' => 'danger', 'pesan' => 'Gagal Login, akun anda tidak aktif.']) | |
->withInput(); | |
} | |
if (Auth::guard('admin')->attempt(['username' => $request->username, 'password' => $request->password])) { | |
return redirect(route('admin.dashboard'))->with(['jenis' => 'success', 'pesan' => 'Berhasil Login, Selamat Datang Kembali.']); | |
} else { | |
return redirect()->back()->with(['jenis' => 'danger', 'pesan' => 'Username atau Password salah']) | |
->withInput(); | |
} | |
} | |
return redirect()->route('login')->with(['jenis' => 'danger', 'pesan' => 'Gagal Login.']); | |
} | |
public function logout() | |
{ | |
Auth::guard('admin')->logout(); | |
return redirect()->route('login'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected $routeMiddleware = [ | |
'authlogin' => Middleware\AuthLogin::class, | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Middleware; | |
use Closure; | |
use Illuminate\Http\Request; | |
use Illuminate\Support\Facades\Auth; | |
use Illuminate\Support\Facades\Route; | |
class AuthLogin | |
{ | |
public function handle($request, Closure $next, ...$roles) | |
{ | |
foreach ($roles as $r) { | |
if (Auth::guard($r)->user()) { | |
return $next($request); | |
} | |
} | |
return redirect()->route('login')->with(['jenis' => 'danger', 'pesan' => 'Anda belum login']); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment