Issues (36)

app/Http/Controllers/AuthController.php (1 issue)

Severity
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Http\Requests\Auth\LoginRequest;
6
use App\Http\Requests\Auth\RegistrationRequest;
7
use App\Mail\SignUpWelcomeMail;
8
use App\Models\User;
9
use Illuminate\Http\JsonResponse;
10
use Illuminate\Http\Request;
11
use Illuminate\Support\Facades\Mail;
12
13
class AuthController extends Controller
14
{
15
    public function __construct()
16
    {
17
        parent::__construct();
18
        $this->setView('auth');
19
        $this->setRoute('auth');
20
    }
21
22
    public function login()
23
    {
24
        $this->header();
25
26
        return $this->view('login');
27
    }
28
29
    public function register()
30
    {
31
        $this->header();
32
33
        return $this->view('register');
34
    }
35
36
    public function lostPassword()
37
    {
38
        $this->header();
39
40
        return $this->view('lostpass');
41
    }
42
43
    /**
44
     * Handle Login form submisison.
45
     *
46
     * @param  LoginRequest  $request
47
     * @return \Illuminate\Http\RedirectResponse
48
     */
49
    public function loginPost(LoginRequest $request)
50
    {
51
        if (! auth()->attempt($request->only(['email', 'password']), $request->remember)) {
52
            return back()->withErrors('Email or Password is incorrect');
53
        }
54
55
        $user = auth()->user();
0 ignored issues
show
The assignment to $user is dead and can be removed.
Loading history...
56
57
        if (! empty($request->redir)) {
58
            $url = rawurldecode($request->redir);
59
        } else {
60
            $url = route('user.index');
61
        }
62
63
        return redirect($url)->withSuccess('You have been logged in successfully.');
64
    }
65
66
    public function registerPost(RegistrationRequest $request)
67
    {
68
        $user = new User();
69
        $user->fill($request->validated());
70
        $user->password = bcrypt($request->password);
71
        $user->role = 'user';
72
73
        if (! $user->save()) {
74
            return back()->withErrors('Something is wrong. Please try again.');
75
        }
76
77
        auth()->login($user, true);
78
79
        // Send Welcome Mail
80
        Mail::to($user)->send(new SignUpWelcomeMail($user));
81
82
        if (! empty($request->redir)) {
83
            $url = rawurldecode($request->redir);
84
        } else {
85
            $url = route('user.index');
86
        }
87
88
        return redirect($url)->withSuccess('You have been registered successfully...');
89
    }
90
91
    public function resetPasswordPost(Request $request)
92
    {
93
        $request->validate([
94
            'password' => 'required|min:6|confirmed',
95
        ]);
96
97
        $user = User::where('mobile', $request->mobile)->first();
98
        $user->password = bcrypt($request->password);
99
100
        if ($user->save()) {
101
            return redirect()->route('auth.login')->withSuccess('Password has been updated successfully');
102
        } else {
103
            return back()->withErrors('Something is wrong here...');
104
        }
105
    }
106
107
    public function logout()
108
    {
109
        auth()->logout();
110
111
        return redirect('/auth/login')->withSuccess('You have been logged out successfully.');
112
    }
113
}
114