Chris Townsend
Chris Townsend

Reputation: 2716

Laravel Google Auth using artdarek/ oauth-4-laravel

I'm using the Artdarek package to log in using google accounts and need to authorise the user for the app. I'm using Laravel 4.2

This is the code from my function

public function loginWithGoogle() {
        // get data from input
    $code = Input::get( 'code' );

    // get google service
    $googleService = OAuth::consumer( 'Google' );

    // check if code is valid

    // if code is provided get user data and sign in
    if ( !empty( $code ) ) {

        // This was a callback request from google, get the token
        $token = $googleService->requestAccessToken( $code );

        // Send a request with it
        $result = json_decode( $googleService->request( 'https://www.googleapis.com/oauth2/v1/userinfo' ), true );

        // Check to see if user already exists
        if($user = User::where('email', '=', $result['email'])->first())
        {
            $user = User::find($user['id']);
            Auth::login($user);
            // If user isn't activated redirect them
            if ($user->deactivated == 0)
            {
                return View::make('dashboard')->with('user', $user);
            }
                return Redirect::back()->withErrors(['Sorry You have not been approved', 'Speak to your manager']);
        }
        else
        {
            // Create new user waiting for approval
            $new_user = new User();
            $new_user->email = $result['email'];
            $new_user->first_name = $result['given_name'];
            $new_user->surname = $result['family_name'];
            $new_user->googleID = $result['id'];
            $new_user->deactivated = 1;
            $new_user->save();
            return Redirect::back()->withErrors(['Your account have been created. It is awaiting activation by your manager']);
        }


    }
    // if not ask for permission first
    else {
        // get googleService authorization
        $url = $googleService->getAuthorizationUri();

        // return to google login url
        return Redirect::to( (string)$url );
    }
}

When a new user grants permission for the app, I get the error 'Cannot redirect to an empty URL'

For some reason my redirectURL is empty.

Upvotes: 3

Views: 899

Answers (1)

edisonthk
edisonthk

Reputation: 1423

Take a look at document you will find out that you have to set redirect url at 2nd arguments in OAuth::consumer method.

https://github.com/artdarek/oauth-4-laravel#usage

It means, you should use consumer with 2 arguments instead of 1 arguments

$googleService = OAuth::consumer("google","https://mydirectlink");

Upvotes: 2

Related Questions