No Laravel
has helpers
redirection that works perfectly within public methods. But in the class I had to split in two public methods and one private so as not to be repeating the authentication and registration codes that is practically the same, it only changes the interface.
In this way I put the whole package code oriceon / oauth-5-laravel within the private method. But the redirect stops working.
What can it be?
Follow the code:
public function register(Request $request)
{
$this->register=true;
$this->requestOAuth($request);
}
public function authenticate(Request $request)
{
$this->requestOAuth($request);
}
private method
private function requestOAuth(Request $request)
{
try {
// !!! Force via Curl !!!
$this->oauth->setHttpClient('CurlClient');
// get data from request
$token = $request->get('oauth_token');
$verify = $request->get('oauth_verifier');
// get twitter service
$tw = $this->oauth->consumer('Twitter');
// check if code is valid
// if code is provided get user data and sign in
if ( ! is_null($token) && ! is_null($verify))
{
// This was a callback request from twitter, get the token
$token = $tw->requestAccessToken($token, $verify);
// Send a request with it
$result = json_decode($tw->request('account/verify_credentials.json'), true);
if($this->repository instanceof OAuthInterface) {
if ($this->register === true) {
$data = $this->repository->register($result);
} else {
$data = $this->repository->authenticate($result);
}
//Create Sessions, Cookies Etc...
$this->storage($data);
}
return redirect()->route('account.login')->with('message_success_login', true);
}
// if not ask for permission first
else
{
// get request token
$reqToken = $tw->requestRequestToken();
// get Authorization Uri sending the request token
$url = $tw->getAuthorizationUri(['oauth_token' => $reqToken->getRequestToken()]);
if (!empty($request->input('denied'))) {
throw new \Exception( Config::get('constants.OAUTH_DENIED') );
}
// return to twitter login url
return redirect((string)$url);
}
} catch (\Exception $e) {
return redirect()->route('account.login')->with('message_error', $e->getMessage());
}
}