[7304c7f] | 1 | <?php
|
---|
| 2 |
|
---|
| 3 | namespace App\Http\Controllers\Auth;
|
---|
| 4 |
|
---|
[1c25bcf] | 5 | use App\Helpers\Alert;
|
---|
[7304c7f] | 6 | use App\Http\Requests\Auth\LoginRequest;
|
---|
| 7 | use App\Models\User;
|
---|
| 8 | use App\Http\Controllers\Controller;
|
---|
[1c25bcf] | 9 | use App\Notifications\VerifyUser;
|
---|
| 10 | use App\Services\Hashid;
|
---|
[7304c7f] | 11 | use Illuminate\Support\Facades\Hash;
|
---|
[1c25bcf] | 12 | use Illuminate\Support\Str;
|
---|
[7304c7f] | 13 |
|
---|
| 14 | class LoginController extends Controller
|
---|
| 15 | {
|
---|
[1c25bcf] | 16 | protected $redirectTo = '/';
|
---|
[7304c7f] | 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 |
|
---|
[1c25bcf] | 28 | public function login(LoginRequest $request, Hashid $hashid)
|
---|
[7304c7f] | 29 | {
|
---|
| 30 | $user = User::whereUsername($request->username)->first();
|
---|
| 31 |
|
---|
[1c25bcf] | 32 | if (is_null($user)) {
|
---|
| 33 | Alert::flash("Please check your credentials", "error");
|
---|
| 34 | return redirect()->route("auth.login");
|
---|
[7304c7f] | 35 | }
|
---|
| 36 |
|
---|
[1c25bcf] | 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");
|
---|
[7304c7f] | 40 | }
|
---|
| 41 |
|
---|
[1c25bcf] | 42 | if (!Hash::check($request->password, $user->password)) {
|
---|
| 43 | Alert::flash("Your password is incorrect", "error");
|
---|
| 44 | return redirect()->route("auth.login");
|
---|
[7304c7f] | 45 | }
|
---|
| 46 |
|
---|
[1c25bcf] | 47 | //$user->security_code = rand(10000, 99999);
|
---|
[7304c7f] | 48 | if ($user->is_forgot_password) {
|
---|
| 49 | $user->is_forgot_password = false;
|
---|
| 50 | }
|
---|
| 51 |
|
---|
[1c25bcf] | 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));
|
---|
[7304c7f] | 58 |
|
---|
[1c25bcf] | 59 | return redirect()->route("verify-login.index", [
|
---|
| 60 | "id" => $hashid->encode($user->id),
|
---|
| 61 | "token" => $user->verify_token
|
---|
| 62 | ]);
|
---|
[7304c7f] | 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 | }
|
---|