@Pierre Cordier @Mr_Antivius Thank you guys for your answer, it helped me figure out the problem and allowed me to mess with JWT, but in the end did not work out a solution for me.
To allow only authenticated users access to domain.com/dashboard
, I had to implement a hybrid session and OAuth authentication system. I decided to go with Sentinel (instead of Laravel from the auth system) because it has a user permission system that I need elsewhere in my application. I use this library for an OAuth server.
Here is what I do in the controller:
POST domain.com/auth/authenticate
:
public function processLogin(Request $request) { $credentials = [ 'email' => $request->input('username'), 'password' => $request->input('password'), ]; try { $sentinel = Sentinel::authenticate($credentials); } catch (\Cartalyst\Sentinel\Checkpoints\ThrottlingException $e) { $response = ['error' => [$e->getMessage()]]; $httpStatus = 429; return response()->json($response, $httpStatus); } catch (\Cartalyst\Sentinel\Checkpoints\NotActivatedException $e) { $response = ['error' => [$e->getMessage()]]; $httpStatus = 401; return response()->json($response, $httpStatus); } if ($sentinel) //user credentials correct { //get oauth token $oauthToken = Authorizer::issueAccessToken(); $response = ['success' => true, 'user' => ['id' => $sentinel->id, 'email' => $sentinel->email]] + $oauthToken; $httpStatus = 200; } else { $response = ['success' => false, 'error' => ['Incorrect credentials']]; $httpStatus = 401; } return response()->json($response, $httpStatus); }
Here is the method that the OAuth library executes to authenticate the user:
public function verifyAuth($email, $password) { $credentials = [ 'email' => $email, 'password' => $password, ]; if ($user = Sentinel::stateless($credentials)) { return $user->id; } else { return false; } }
This will create a response like this:
{ "success": true, "user": { "id": 1, "email": " email@domain.tld " }, "access_token": "6a204bd89f3c8348afd5c77c717a097a", "token_type": "Bearer", "expires_in": 28800, "refresh_token": "092a8e1a7025f700af39e38a638e199b" }
Hope this helps someone out there.
Side note: I am sending a POST
request to domain.com/auth/authenticate
instead of api.domain.com/auth/authenticate
because I could not get domain.com/dashboard
to recognize the cookie sender if I sent it to api.domain.com
. I tried changing the domain
in config/session.php
to .domain.com
, but still nothing. Maybe I'm doing something wrong?