Redirect via private method in Laravel

Asked

Viewed 182 times

0

In the Laravel has the helpers redirect that works perfectly within public methods. However in the class I had to divide into two public and one private methods to avoid repeating the authentication and registration codes that is practically the same, only changes the interface.

This way I put all the code of the package oriceon/oauth-5-Arable inside the private method. But the redirect stops working.

Whatever?

Follows 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());

    }

}

1 answer

1


Is a erro conceptual where methods should return (return):

public function register(Request $request)
{
    $this->register=true;
    return $this->requestOAuth($request);
}


public function authenticate(Request $request)
{
    return $this->requestOAuth($request);
}

the two methods are now in the correct format with return.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.