source: app/Http/Controllers/Auth/LoginController.php@ 05e57e2

develop
Last change on this file since 05e57e2 was 7304c7f, checked in by beratkjufliju <kufliju@…>, 3 years ago

added user authentication, create & forgot password methods and blades

  • Property mode set to 100644
File size: 1.8 KB
RevLine 
[7304c7f]1<?php
2
3namespace App\Http\Controllers\Auth;
4
5use App\Http\Requests\Auth\LoginRequest;
6use App\Models\User;
7use App\Http\Controllers\Controller;
8use Illuminate\Support\Facades\Hash;
9
10class 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 your system 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", "An 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}
Note: See TracBrowser for help on using the repository browser.