Login with Twitter in Laravel with artdarek / oauth-4-laravel

0

I do not understand how this OAth service provider is used:

I have a login created, where, being a login, I hope to enter there an email and password and a button to make the request.

My code is this:

<div class="container">
    <div class="row">
        <div class="col-md-4 col-md-offset-4">
            <div class="account-box">
                <div class="logo">
                    <img src="/tweety.png" width="80px" alt=""/>
                </div>
                <h3 align="center">Twitvel</h3>
                <form class="form-signin" action="#">
                <div class="form-group">
                    <input type="text" name="mail" id="mail" class="form-control" placeholder="Email" required autofocus />
                </div>
                <div class="form-group">
                    <input type="password" name="password" id="password" class="form-control" placeholder="Password" required />
                </div>
                <div class="btn btn-lg btn-block">
                  <a href="{{ url('auth/twitter') }}" class="btn btn-lg btn-info btn-block">
                    <strong>Autentificar con Twitter</strong>
                  </a>
                </div>
                </form>
            </div>
        </div>
    </div>
</div>

And the route to which I make the request has this code:

Route::get('/auth/twitter', function(){
$token = Input::get( 'oauth_token' );
$verify = Input::get( 'oauth_verifier' );

// get twitter service
$tw = OAuth::consumer( 'Twitter' );

// check if code is valid

// if code is provided get user data and sign in
if ( !empty( $token ) && !empty( $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 );

    $message = 'Your unique Twitter user id is: ' . $result['id'] . ' and your name is ' . $result['name'];
    echo $message. "<br/>";

    //Var_dump
    //display whole array().
    dd($result);

}

And I do not understand if it is correct to put my email and password in my input, I just do not understand how to login.

    
asked by YelloWhale 25.07.2016 в 20:54
source

1 answer

1

No, you do not need a username or password because you are an OAuth provider.

According to wikipedia :

  

OAuth allows a user of site A to share their information on site A (service provider) with site B (called consumer) without sharing their entire identity. For consumer developers, OAuth is a method of interacting with protected data and publishing it. For service provider developers, OAuth provides users with access to their data while protecting the credentials of their account.

In other words, you do not need a user or a password because the user is using his Twitter "user" (in this case) to authenticate himself on your website. It is the responsibility of your site to save the information provided (such as the token) by the service so that the user can continue logging into your site without having to request permissions or any other information each time the user connects (except compare be the correct user and that the application has the agreed permissions).

Turning to programming as such, you only need (apart from configuring the package):

A view with a "Enter" button, something similar to what you have but without the username and password:

<a href="{{ url('auth/twitter') }}" class="btn btn-lg btn-info btn-block">
  <strong>Autentificar con Twitter</strong>
</a>

A route (and a method in a controller, preferably), which communicates with Twitter to request / verify permissions.

Route::get('/auth/twitter', ......); // agrega controlador y método

In the controller (add the respective dependencies):

public function logInTwitter() {

    $reqToken = $tw->requestRequestToken();

    // get Authorization Uri sending the request token
    $url = $tw->getAuthorizationUri(array('oauth_token' => $reqToken->getRequestToken()));

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

}

Finally, you would need another route and another method in the controller to receive the Twitter response, although in the quick example of the documentation they do everything in one method:

Route::get('/auth/response/twitter', ......); // agrega controlador y método

and in the controller

public function processResponseTwitter()
    // get data from input
    $token = Input::get( 'oauth_token' );
    $verify = Input::get( 'oauth_verifier' );

    // get twitter service
    $tw = OAuth::consumer( 'Twitter' );

    // check if code is valid

    // if code is provided get user data and sign in
    if ( !empty( $token ) && !empty( $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 );

        $message = 'Your unique Twitter user id is: ' . $result['id'] . ' and your name is ' . $result['name'];
        echo $message. "<br/>";

        // Login ?
    } else {
       // Problems....
    }
    
answered by 26.07.2016 / 22:11
source