-
Notifications
You must be signed in to change notification settings - Fork 0
/
AuthController.php
88 lines (65 loc) · 2.3 KB
/
AuthController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Auth;
use App\User;
class ApiAuthCR extends Controller
{
public function login(Request $req)
{
$req->validate([
'email' => 'required|email',
'password' => 'required|string'
]);
$req->request->add([
'grant_type' => 'password',
'client_id' => env('PASSPORT_GRANT_ID'),
'client_secret' => env('PASSPORT_GRANT_TOKEN'),
'username' => $req->email,
'password' => $req->password,
'scope' => '*',
]);
$tokenRequest = Request::create('/oauth/token', 'POST');
$response = Route::dispatch($tokenRequest);
$code = $response->getStatusCode();
if ($code === 200) {
// Auth::attempt($req->all());
return response()->json($response->getBody(), 200);
//
} //
else if ($code === 400) $error = "Invalid request. Please enter a email or password!";
else if ($code === 401) $error = "Your credentials are incorrect!";
else $error = "Something went wrong";
// if ($error) return redirect()->back()->withErrors(['error' => $error]);
if ($error) return response()->json(['error' => $error], 400);
}
public function register(Request $req)
{
$req->validate([
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed'
]);
$user = User::create([
'name' => $req->name,
'email' => $req->email,
'password' => bcrypt($req->password)
]);
if ($user) return response()->json(['message' => 'success'], 200);
else return response()->json(['message' => 'success'], 400);
// Auth::attempt([
// 'email' => $req->email,
// 'password' => $req->password
// ]);
return redirect('/');
}
public function logout()
{
// Auth::logout();
auth()->user()->tokens->each(function ($token) {
$token->delete();
});
return response()->json(['message' => 'success'], 200);
}
}