artdarek / oauth-4-laravel

OAuth Service Provider for Laravel 4
684 stars 217 forks source link

How to implement GitHub login?, laravel example please! #90

Open davgeek opened 10 years ago

davgeek commented 10 years ago

How I can login using github?, y use the examples but this generet errors.

fhferreira commented 9 years ago
public function loginWithGithub()
    {
        // get data from input
        $code = Input::get( 'code' );

        $githubService = OAuth::consumer( 'Github' );

        if ( !empty( $code ) ) {

            // This was a callback request from github, get the token
            $token = $githubService->requestAccessToken( $code );
            // Send a request with it. Please note that XML is the default format.
            $result = json_decode($githubService->request('user/emails'), true);

            // Show some of the resultant data
            echo 'The first email on your github account is ' . $result[0];

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

        }
        else {            
            // get githubService authorization
            $url = $githubService->getAuthorizationUri();

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

    }
davgeek commented 9 years ago

thanks for the answer, but this generete an error

Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_ERROR) Call to a member function getAuthorizationUri() on a non-object

why this happens?

fhferreira commented 9 years ago

In my project this works ok.

You run composer update in your root aplication, and add referente into alias e and services providers in app.php

davgeek commented 9 years ago

Now work with this code


public function loginGithub(){  
            $code = Input::get( 'code' );
            $gh = OAuth::consumer( 'GitHub', 'http://programacion.tips/login/github', null);
            if ( !empty( $code ) ) {
                $token = $gh->requestAccessToken($code);
                $result = json_decode( $gh->request( '/user' ), true );
                $uid = $result['id'];
                $profile = Profile::whereUid($uid)->first();
                if (empty($profile)) {
                   $user = new User;
                   $user->name = $result['name'];
                   $user->email = $result['email'];
                   $imagenPerfil = $result['avatar_url'];
                   $user->photo = $imagenPerfil;
                   $user->save();
                   $profile = new Profile();
                   $profile->uid = $uid;
                   $profile->username = strtolower($result['login']);
                   $profile = $user->profiles()->save($profile);
                }
                $profile->save();
                $user = $profile->user;

                Auth::login($user);
                Session::put('u_id', Auth::user()->id);
                return Redirect::to('/')->with('message', 'Logeado con Github');
            }
            else {         
                $url = $gh->getAuthorizationUri();
                return Redirect::to( (string) $url );
            }
    }