Issues (124)

src/Http/Controllers/UserController.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace CodexShaper\DBM\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Facades\Auth;
7
use Illuminate\Support\Facades\Config;
8
use Illuminate\Support\Facades\Response;
9
use Illuminate\Support\Facades\Validator;
10
11
class UserController extends Controller
12
{
13
    /**
14
     * Show login form.
15
     *
16
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
17
     */
18
    public function showLoginForm()
19
    {
20
        return view('dbm::admin');
21
    }
22
23
    /**
24
     * Login User for API.
25
     *
26
     * @return \Illuminate\Http\JsonResponse
27
     */
28
    public function login(Request $request)
29
    {
30
        if ($request->ajax()) {
31
            try {
32
                if (($response = $this->validation($request->data)) !== true) {
33
                    return $response;
34
                }
35
                $credentials = [
36
                    'email' => $request->data['email'],
37
                    'password' => $request->data['password'],
38
                ];
39
40
                if (! Auth::attempt($credentials)) {
41
                    return $this->generateError(["Email and password combination doesn't match"]);
42
                }
43
44
                $user = Auth::user();
45
                $expiry = Config::get('dbm.auth.token.expiry');
46
                if (count($user->tokens) > 0) {
0 ignored issues
show
Accessing tokens on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
47
                    $user->tokens()->delete();
48
                }
49
50
                return response()->json([
51
                    'success' => true,
52
                    'user' => $user,
53
                    'token' => $user->createToken('DBM')->accessToken,
54
                    'expiry' => $expiry,
55
                ]);
56
            } catch (\Exception $e) {
57
                $this->generateError([$e->getMessage()]);
58
            }
59
        }
60
61
        return response()->json(['success' => false, 'error' => 'Unauthorised'], 401);
62
    }
63
64
    /**
65
     * Validate Credentials.
66
     *
67
     * @param array $data
68
     *
69
     * @return \Illuminate\Http\JsonResponse|true
70
     */
71
    public function validation($data)
72
    {
73
        $validator = Validator::make($data, [
74
            'email' => 'required|email',
75
            'password' => 'required',
76
77
        ]);
78
79
        if ($validator->fails()) {
80
            $errors = [];
81
            foreach ($validator->errors()->all() as $error) {
82
                $errors[] = $error;
83
            }
84
85
            return $this->generateError($errors);
86
        }
87
88
        return true;
89
    }
90
91
    /**
92
     * Generate errors and return response.
93
     *
94
     * @param array $errors
95
     *
96
     * @return \Illuminate\Http\JsonResponse
97
     */
98
    public function generateError($errors)
99
    {
100
        return response()->json([
101
            'success' => false,
102
            'errors' => $errors,
103
        ], 400);
104
    }
105
}
106