1 | <?php
|
---|
2 |
|
---|
3 | namespace App\Http\Controllers\Auth;
|
---|
4 |
|
---|
5 | use App\Helpers\Alert;
|
---|
6 | use App\Http\Requests\Auth\LoginRequest;
|
---|
7 | use App\Models\User;
|
---|
8 | use App\Http\Controllers\Controller;
|
---|
9 | use App\Notifications\VerifyUser;
|
---|
10 | use App\Services\Hashid;
|
---|
11 | use Illuminate\Support\Facades\Hash;
|
---|
12 | use Illuminate\Support\Str;
|
---|
13 |
|
---|
14 | class LoginController extends Controller
|
---|
15 | {
|
---|
16 | protected $redirectTo = '/';
|
---|
17 |
|
---|
18 | public function __construct()
|
---|
19 | {
|
---|
20 | $this->middleware("guest")->except('logout');
|
---|
21 | }
|
---|
22 |
|
---|
23 | public function showLogin()
|
---|
24 | {
|
---|
25 | return view("auth.login");
|
---|
26 | }
|
---|
27 |
|
---|
28 | public function login(LoginRequest $request, Hashid $hashid)
|
---|
29 | {
|
---|
30 | $user = User::whereUsername($request->username)->first();
|
---|
31 |
|
---|
32 | if (is_null($user)) {
|
---|
33 | Alert::flash("Please check your credentials", "error");
|
---|
34 | return redirect()->route("auth.login");
|
---|
35 | }
|
---|
36 |
|
---|
37 | if (!$user->is_active) {
|
---|
38 | Alert::flash("Your account is blocked or its not confirmed yet. Please contact with your system administrator or check your email.", "error");
|
---|
39 | return redirect()->route("auth.login");
|
---|
40 | }
|
---|
41 |
|
---|
42 | if (!Hash::check($request->password, $user->password)) {
|
---|
43 | Alert::flash("Your password is incorrect", "error");
|
---|
44 | return redirect()->route("auth.login");
|
---|
45 | }
|
---|
46 |
|
---|
47 | $user->security_code = rand(10000, 99999);
|
---|
48 | if ($user->is_forgot_password) {
|
---|
49 | $user->is_forgot_password = false;
|
---|
50 | }
|
---|
51 |
|
---|
52 | //$user->security_code = 1234;
|
---|
53 | $user->verify_token = Str::uuid();
|
---|
54 | $user->is_online = true;
|
---|
55 | $user->save();
|
---|
56 |
|
---|
57 | $user->notify(new VerifyUser($user));
|
---|
58 |
|
---|
59 | return redirect()->route("verify-login.index", [
|
---|
60 | "id" => $hashid->encode($user->id),
|
---|
61 | "token" => $user->verify_token
|
---|
62 | ]);
|
---|
63 | }
|
---|
64 |
|
---|
65 | public function logout()
|
---|
66 | {
|
---|
67 |
|
---|
68 | $user = auth()->user();
|
---|
69 | $user->is_online = false;
|
---|
70 | $user->save();
|
---|
71 |
|
---|
72 | auth()->logout();
|
---|
73 |
|
---|
74 | return redirect("/auth/login");
|
---|
75 | }
|
---|
76 | }
|
---|