1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CodexShaper\DBM\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use CodexShaper\DBM\Facades\Manager as DBM; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use Illuminate\Support\Facades\Auth; |
8
|
|
|
use Illuminate\Support\Facades\Config; |
9
|
|
|
use Illuminate\Support\Facades\Response; |
10
|
|
|
use Illuminate\Support\Facades\Validator; |
11
|
|
|
|
12
|
|
|
class UserController extends Controller |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Show login form |
16
|
|
|
* |
17
|
|
|
* @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory |
18
|
|
|
*/ |
19
|
|
|
public function showLoginForm() |
20
|
|
|
{ |
21
|
|
|
return view('dbm::admin'); |
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) { |
|
|
|
|
47
|
|
|
$user->tokens()->delete(); |
48
|
|
|
} |
49
|
|
|
return response()->json([ |
50
|
|
|
'success' => true, |
51
|
|
|
'user' => $user, |
52
|
|
|
'token' => $user->createToken('DBM')->accessToken, |
53
|
|
|
'expiry' => $expiry, |
54
|
|
|
]); |
55
|
|
|
|
56
|
|
|
} catch (\Exception $e) { |
57
|
|
|
$this->generateError([$e->getMessage()]); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
return response()->json(["success" => false, "error" => "Unauthorised"], 401); |
61
|
|
|
|
62
|
|
|
} |
63
|
|
|
/** |
64
|
|
|
* Validate Credentials |
65
|
|
|
* |
66
|
|
|
* @param array $data |
67
|
|
|
* |
68
|
|
|
* @return \Illuminate\Http\JsonResponse|true |
69
|
|
|
*/ |
70
|
|
|
public function validation($data) |
71
|
|
|
{ |
72
|
|
|
$validator = Validator::make($data, [ |
73
|
|
|
'email' => 'required|email', |
74
|
|
|
'password' => 'required', |
75
|
|
|
|
76
|
|
|
]); |
77
|
|
|
|
78
|
|
|
if ($validator->fails()) { |
79
|
|
|
$errors = []; |
80
|
|
|
foreach ($validator->errors()->all() as $error) { |
81
|
|
|
$errors[] = $error; |
82
|
|
|
} |
83
|
|
|
return $this->generateError($errors); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
/** |
89
|
|
|
* Generate errors and return response |
90
|
|
|
* |
91
|
|
|
* @param array $errors |
92
|
|
|
* |
93
|
|
|
* @return \Illuminate\Http\JsonResponse |
94
|
|
|
*/ |
95
|
|
|
public function generateError($errors) |
96
|
|
|
{ |
97
|
|
|
return response()->json([ |
98
|
|
|
'success' => false, |
99
|
|
|
'errors' => $errors, |
100
|
|
|
], 400); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|