Carwyn Stephen
Carwyn Stephen

Reputation: 134

Laravel Redirect::to() not working

I am creating a twitter log in feature for my project, the oauth step where the user has granted permission for my app to use their data returns the user to the /twitter-auth route, this route in turn initiates this method:

public function auth() {
    /* Oauth token */ 
    $token = Input::get('oauth_token');

    /* Verifier token */
    $verifier = Input::get('oauth_verifier');

    /* Request access token */
    $accessToken = Twitter::oAuthAccessToken($token, $verifier);


    /* Set the session variables from the acccess token above */
    Session::set('user_id', $accessToken['user_id']);
    Session::set('username', $accessToken['screen_name']);
    Session::set('oauth_token', $accessToken['oauth_token']);
    Session::set('oauth_token_secret', $accessToken['oauth_token_secret']);

    /* Determine if the user already exists in the database, if he/she does, then
    only update the user, otherwise, store a new user. Also pass an instance of the 
    accessToken as flash data in both instances. */
    if( User::where('twitter_id', $accessToken['user_id'])->first() == null )
    {

        $newUser = array(
            'username' => $accessToken['screen_name'],
            'oauth_token' => $accessToken['oauth_token'],
            'oauth_token_secret' => $accessToken['oauth_token_secret'],
            'twitter_id' => $accessToken['user_id']
        );

        User::create( $newUser );
        return Redirect::to('/');

    }

    else
    {
        $userToUpdate = User::where('twitter_id', Session::get('user_id'))->first();

        $userToUpdate->username = $accessToken['screen_name'];
        $userToUpdate->oauth_token = $accessToken['oauth_token'];
        $userToUpdate->oauth_token_secret = $accessToken['oauth_token_secret'];
        $userToUpdate->twitter_id = $accessToken['user_id'];

        $userToUpdate->save();
        return Redirect::to('/');

    }


}

The user is saved/updated as necessary, but the user is not redirected to the home page. This happens with the redirect code both inside and outside of the IF statement. I was wondering if anyone could give me any clues as to why the redirect isn't working?

Upvotes: 0

Views: 7494

Answers (3)

Haisum Usman
Haisum Usman

Reputation: 516

Returning a Redirect to execute it is only possible from routes, controller actions and filters. Otherwise you have to call send()

Redirect::to('login')->send();

Upvotes: 2

Mahmoud Zalt
Mahmoud Zalt

Reputation: 31130

You are missing a return

your function in this case auth() is returning the Redirect object but is the function calling your auth() function is returning the result back to the controller?

Upvotes: 3

Paul Bele
Paul Bele

Reputation: 1534

Please make sure that in your controller, you return the Redirect class that is from auth() function. Just tested your code and works :

let's say you have a UserController :

routes.php

Route::get('twitter-auth',array('as'=>'twitter-auth', 'uses'=>'UserController@twitterAuth'));

UserController the user model class is just passed by dependency injection, to test this part also.

<?php

class UserController extends BaseController {

    public function __construct(User $u){
        $this->user = $u;
    }


    public function twitterAuth(){

        return $this->user->auth();



    }

}

User model : I had to modify the code a little to fit my setup also

public function auth(){

    /* Oauth token */ 
    $token = Input::get('oauth_token');

    /* Verifier token */
    $verifier = Input::get('oauth_verifier');

    /* Request access token */
    //$accessToken = Twitter::oAuthAccessToken($token, $verifier);
    //emulate the request of access Token
    $accessToken = [
        'user_id'=>'11',
        'screen_name'=>'fewfewfew',
        'oauth_token'=>'12312321',
        'oauth_token_secret'=>'12312232323'
    ];

    /* Set the session variables from the acccess token above */
    Session::set('user_id', $accessToken['user_id']);
    Session::set('username', $accessToken['screen_name']);
    Session::set('oauth_token', $accessToken['oauth_token']);
    Session::set('oauth_token_secret', $accessToken['oauth_token_secret']);


    /* Determine if the user already exists in the database, if he/she does, then
    only update the user, otherwise, store a new user. Also pass an instance of the 
    accessToken as flash data in both instances. */
    if( User::where('twitter_id', $accessToken['user_id'])->first() == null )
    {

        $newUser = array(
            'username' => $accessToken['screen_name'],
            'oauth_token' => $accessToken['oauth_token'],
            'oauth_token_secret' => $accessToken['oauth_token_secret'],
            'twitter_id' => $accessToken['user_id']
        );

        User::create( $newUser );
        return Redirect::to('/');

    }

    else
    {
        $userToUpdate = User::where('twitter_id', Session::get('user_id'))->first();

        $userToUpdate->username = $accessToken['screen_name'];
        $userToUpdate->oauth_token = $accessToken['oauth_token'];
        $userToUpdate->oauth_token_secret = $accessToken['oauth_token_secret'];
        $userToUpdate->twitter_id = $accessToken['user_id'];

        $userToUpdate->save();
        return Redirect::to('/');

    }


}

Let me know if this is what you wanted

Upvotes: 2

Related Questions