80 lines
2.0 KiB
PHP
80 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests;
|
|
use App\Http\Requests\LoginRequest;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
/**
|
|
* Class Login
|
|
*
|
|
* @package App\Http\Controllers\Admin
|
|
*
|
|
* @todo use trait ThrottlesLogins for disabling brutal-force password cracking
|
|
*/
|
|
class Login extends Controller
|
|
{
|
|
/**
|
|
* Display login form
|
|
*
|
|
* This method displays login form, or in case that user is logged in, redirects him to admin dashboard panel.
|
|
*
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
|
|
*/
|
|
public function getLoginForm()
|
|
{
|
|
if (Auth::check()) {
|
|
return redirect()->route('admin.dashboard');
|
|
}
|
|
|
|
return view('admin.loginForm.form');
|
|
}
|
|
|
|
/**
|
|
* Login handler
|
|
*
|
|
* @param LoginRequest $request
|
|
*
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function postLogin(LoginRequest $request)
|
|
{
|
|
$email = $request->email;
|
|
|
|
// try to find user
|
|
$user = User::where('email', $email)
|
|
->where('role', '!=', User::ROLE_VISITOR)
|
|
->where('status', User::STATE_ACTIVE)
|
|
->first();
|
|
|
|
if (!$user) {
|
|
return $this->loginFailed();
|
|
}
|
|
|
|
// try to log in user
|
|
if (Hash::check($request->password, $user->password)) {
|
|
Auth::login($user);
|
|
|
|
return redirect()->route('admin.dashboard');
|
|
}
|
|
|
|
// if (Auth::attempt($request->only('email', 'password'))) {
|
|
// return redirect()->route('admin.dashboard');
|
|
// }
|
|
|
|
// login invalid, redirect and inform the user
|
|
return $this->loginFailed();
|
|
}
|
|
|
|
private function loginFailed()
|
|
{
|
|
return redirect()
|
|
->route('login')
|
|
->with('error', trans('auth.failed'));
|
|
}
|
|
}
|