Skip to content
This repository has been archived by the owner on Dec 21, 2021. It is now read-only.

Commit

Permalink
If we’re using CandyClient, set tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
alecritson committed Feb 26, 2019
1 parent 01ed451 commit a84b5a9
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/Http/Controllers/Auth/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace GetCandy\Hub\Http\Controllers\Auth;

use CandyClient;
use Illuminate\Http\Request;
use GetCandy\Hub\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

Expand Down Expand Up @@ -51,4 +53,44 @@ public function showLoginForm()
{
return view('hub::auth.login');
}

/**
* The user has been authenticated.
*
* @param \Illuminate\Http\Request $request
* @param mixed $user
* @return mixed
*/
protected function authenticated(Request $request, $user)
{
if (class_exists(CandyClient::class)) {
// They are authenticated, so lets just get them a token
// That they can use for requests.
$token = CandyClient::getUserToken($request->email, $request->password);

$this->setSessionTokens(
$request,
$token->getBody()->access_token,
$token->getBody()->refresh_token,
$token->getBody()->expires_in
);
}
}

/**
* Sets the session tokens
*
* @param string $token
* @param string $refresh
* @param int $expires
* @return void
*/
protected function setSessionTokens($request, $token, $refresh, $expires)
{
$tokenExpiry = \Carbon\Carbon::now()->addMinutes($expires / 60);
$request->session()->put('access_token', $token);
$request->session()->put('refresh_token', $refresh);
$request->session()->put('token_expires_at', $tokenExpiry);
CandyClient::setToken($token);
}
}

0 comments on commit a84b5a9

Please sign in to comment.