Skip to content

Commit 2ae4739

Browse files
committed
Add auth.
1 parent 5d2d53e commit 2ae4739

File tree

4 files changed

+81
-21
lines changed

4 files changed

+81
-21
lines changed

app/Http/Controllers/Auth/LoginController.php

+65-18
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,15 @@
22

33
namespace App\Http\Controllers\Auth;
44

5+
use Cookie;
6+
use Illuminate\Http\Request;
57
use App\Http\Controllers\Controller;
68
use Illuminate\Foundation\Auth\AuthenticatesUsers;
79

810
class LoginController extends Controller
911
{
10-
/*
11-
|--------------------------------------------------------------------------
12-
| Login Controller
13-
|--------------------------------------------------------------------------
14-
|
15-
| This controller handles authenticating users for the application and
16-
| redirecting them to your home screen. The controller uses a trait
17-
| to conveniently provide its functionality to your applications.
18-
|
19-
*/
20-
2112
use AuthenticatesUsers;
2213

23-
/**
24-
* Where to redirect users after login.
25-
*
26-
* @var string
27-
*/
28-
protected $redirectTo = '/home';
29-
3014
/**
3115
* Create a new controller instance.
3216
*
@@ -36,4 +20,67 @@ public function __construct()
3620
{
3721
$this->middleware('guest')->except('logout');
3822
}
23+
24+
/**
25+
* Attempt to log the user into the application.
26+
*
27+
* @param \Illuminate\Http\Request $request
28+
* @return bool
29+
*/
30+
protected function attemptLogin(Request $request)
31+
{
32+
$token = $this->guard()->attempt($this->credentials($request));
33+
34+
if ($token) {
35+
$this->guard()->setToken($token);
36+
37+
return true;
38+
}
39+
40+
return false;
41+
}
42+
43+
/**
44+
* Get the needed authorization credentials from the request.
45+
*
46+
* @param \Illuminate\Http\Request $request
47+
* @return array
48+
*/
49+
protected function credentials(Request $request)
50+
{
51+
return array_merge(
52+
$request->only($this->username(), 'password'),
53+
['role' => 'admin']
54+
);
55+
}
56+
57+
/**
58+
* Send the response after the user was authenticated.
59+
*
60+
* @param \Illuminate\Http\Request $request
61+
* @return \Illuminate\Http\Response
62+
*/
63+
protected function sendLoginResponse(Request $request)
64+
{
65+
$this->clearLoginAttempts($request);
66+
67+
$token = (string) $this->guard()->getToken();
68+
$expiration = $this->guard()->getPayload()->get('exp');
69+
70+
return [
71+
'token' => $token,
72+
'user' => $this->guard()->user(),
73+
];
74+
}
75+
76+
/**
77+
* Log the user out of the application.
78+
*
79+
* @param \Illuminate\Http\Request $request
80+
* @return \Illuminate\Http\Response
81+
*/
82+
public function logout(Request $request)
83+
{
84+
return $this->guard()->logout();
85+
}
3986
}

app/Http/Middleware/RedirectIfAuthenticated.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class RedirectIfAuthenticated
1818
public function handle($request, Closure $next, $guard = null)
1919
{
2020
if (Auth::guard($guard)->check()) {
21-
return redirect('/home');
21+
// return response()->json(['error' => 'Already authenticated.'], 400);
2222
}
2323

2424
return $next($request);

config/auth.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515

1616
'defaults' => [
17-
'guard' => 'web',
17+
'guard' => 'api',
1818
'passwords' => 'users',
1919
],
2020

@@ -42,7 +42,7 @@
4242
],
4343

4444
'api' => [
45-
'driver' => 'token',
45+
'driver' => 'jwt',
4646
'provider' => 'users',
4747
],
4848
],

routes/api.php

+13
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,19 @@
1212
| is assigned the "api" middleware group. Enjoy building your API!
1313
|
1414
*/
15+
// Routes for only the guest user...
16+
Route::group(['namespace' => 'Auth', 'prefix' => 'auth'], function () {
17+
Route::post('login', 'LoginController@login');
18+
19+
Route::group(['middleware' => 'auth:api'], function () {
20+
// Logout
21+
Route::post('/logout', 'LoginController@logout');
22+
// Get User
23+
Route::get('/user', function (Request $request) {
24+
return ['user' => $request->user()];
25+
});
26+
});
27+
});
1528

1629
// Posts
1730
Route::group(['prefix' => 'posts'], function () {

0 commit comments

Comments
 (0)