1 | <?php
|
---|
2 |
|
---|
3 | namespace App\Http\Controllers\Auth;
|
---|
4 |
|
---|
5 | use App\Http\Requests\Auth\LoginRequest;
|
---|
6 | use App\Models\User;
|
---|
7 | use App\Http\Controllers\Controller;
|
---|
8 | use Illuminate\Support\Facades\Hash;
|
---|
9 |
|
---|
10 | class LoginController extends Controller
|
---|
11 | {
|
---|
12 | protected $redirectTo = '/dashboard';
|
---|
13 |
|
---|
14 | public function __construct()
|
---|
15 | {
|
---|
16 | $this->middleware("guest")->except('logout');
|
---|
17 | }
|
---|
18 |
|
---|
19 | public function showLogin()
|
---|
20 | {
|
---|
21 | return view("auth.login");
|
---|
22 | }
|
---|
23 |
|
---|
24 | public function login(LoginRequest $request)
|
---|
25 | {
|
---|
26 | $user = User::whereUsername($request->username)->first();
|
---|
27 |
|
---|
28 | if (!$user->is_active) {
|
---|
29 | $request->session()->flash("isActiveError", "Your account is blocked or its not confirmed yet. Please contact with administrator or check your email.");
|
---|
30 | return redirect()->back();
|
---|
31 | }
|
---|
32 |
|
---|
33 | if (!Hash::check($request->password, $user->password)) {
|
---|
34 | $request->session()->flash("passwordError", "Your password is incorrect");
|
---|
35 | return redirect()->back()->withInput($request->input());
|
---|
36 | }
|
---|
37 |
|
---|
38 | if (auth()->attempt([
|
---|
39 | "username" => $request->username,
|
---|
40 | "password" => $request->password,
|
---|
41 | "is_active" => true], $request->remember)) {
|
---|
42 |
|
---|
43 | $user->is_online = true;
|
---|
44 | $user->save();
|
---|
45 |
|
---|
46 | return redirect()->intended('/dashboard');
|
---|
47 | }
|
---|
48 |
|
---|
49 | if ($user->is_forgot_password) {
|
---|
50 | $user->is_forgot_password = false;
|
---|
51 | }
|
---|
52 |
|
---|
53 | $request->session()->flash("loginError", "Error occurred while login. Please try again later.");
|
---|
54 |
|
---|
55 | return redirect()->back()->withInput($request->input());
|
---|
56 | }
|
---|
57 |
|
---|
58 | public function logout()
|
---|
59 | {
|
---|
60 |
|
---|
61 | $user = auth()->user();
|
---|
62 | $user->is_online = false;
|
---|
63 | $user->save();
|
---|
64 |
|
---|
65 | auth()->logout();
|
---|
66 |
|
---|
67 | return redirect("/auth/login");
|
---|
68 | }
|
---|
69 | }
|
---|