PHP code example of pasadinhas / oauth-4-laravel

1. Go to this page and download the library: Download pasadinhas/oauth-4-laravel library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pasadinhas / oauth-4-laravel example snippets


'providers' => array(
	// ...
	
	'LaravelOAuth\OAuthServiceProvider'
)

'aliases' => array(
	// ...
	
	'OAuth' => 'LaravelOAuth\Facade\OAuth',
)

return array( 
	
	/*
	|--------------------------------------------------------------------------
	| oAuth Config
	|--------------------------------------------------------------------------
	*/

	/**
	 * Storage
	 */
	'storage' => 'Session', 

	/**
	 * Consumers
	 */
	'consumers' => array(

		// Add your consumers here. Example:

		/**
		 * Facebook
		 */
		'Facebook' => array(
		    'client_id'     => '',
		    'client_secret' => '',
		    'scope'         => array(),
		),		

	)

);

$fb = OAuth::consumer('Facebook');

$fb = OAuth::consumer('Facebook','http://url.to.redirect.to');

'Facebook' => array(
    'client_id'     => 'Your Facebook client ID',
    'client_secret' => 'Your Facebook Client Secret',
    'scope'         => array('email','read_friendlists','user_online_presence'),
),	

/**
 * Login user with facebook
 *
 * @return void
 */

public function loginWithFacebook() {
	
	// get data from input
	$code = Input::get('code');
	
	// get fb service
	$fb = OAuth::consumer('Facebook');
	
	// check if code is valid
	
	// if code is provided get user data and sign in
	if (!empty($code)) {
		
		// This was a callback request from facebook, get the token
		$token = $fb->requestAccessToken($code);
		
		// Send a request with it
		$result = json_decode($fb->request('/me'), true);
		
		$message = 'Your unique facebook user id is: ' . $result['id'] . ' and your name is ' . $result['name'];
		echo $message. "<br/>";
		
		//Var_dump
		//display whole array().
		echo "<pre>";
		dd($result);
	
	}
	// if not ask for permission first
	else {
		// get fb authorization
		$url = $fb->getAuthorizationUri();
		
		// return to facebook login url
		 return Redirect::to((string) $url);
	}

}

'Google' => array(
    'client_id'     => 'Your Google client ID',
    'client_secret' => 'Your Google Client Secret',
    'scope'         => array('userinfo_email', 'userinfo_profile'),
),	

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 );
		
		$message = 'Your unique Google user id is: ' . $result['id'] . ' and your name is ' . $result['name'];
		echo $message. "<br/>";
		
		//Var_dump
		//display whole array().
		dd($result);
	        
	}
	// if not ask for permission first
	else {
		// get googleService authorization
		$url = $googleService->getAuthorizationUri();
		
		// return to facebook login url
		return Redirect::to( (string)$url );
	}
}

'Linkedin' => array(
    'client_id'     => 'Your Linkedin API ID',
    'client_secret' => 'Your Linkedin API Secret',
),	


 public function loginWithLinkedin() {

        // get data from input
        $code = Input::get( 'code' );

        $linkedinService = OAuth::consumer( 'Linkedin' );


        if ( !empty( $code ) ) {

            // This was a callback request from linkedin, get the token
            $token = $linkedinService->requestAccessToken( $code );
            // Send a request with it. Please note that XML is the default format.
            $result = json_decode($linkedinService->request('/people/~?format=json'), true);

            // Show some of the resultant data
            echo 'Your linkedin first name is ' . $result['firstName'] . ' and your last name is ' . $result['lastName'];


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

        }// if not ask for permission first
        else {
            // get linkedinService authorization
            $url = $linkedinService->getAuthorizationUri(array('state'=>'DCEEFWF45453sdffef424'));

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


    }


'FenixEdu' => array(
    'client_id'         => 'Your FenixEdu Client ID',
    'client_secret'     => 'Your FenixEdu Client Secret',
    'redirect_url'      => 'Your FenixEdu redirect URL',
    'automatic_refresh' => true, // if you want to use the refresh token automaticly
),	

/**
 * Login user with FenixEdu
 *
 * @return void
 */

public function loginWithFenixEdu() {
	
	// get data from input
	$code = Input::get('code');
	
	// get FenixEdu service
	$fenix = OAuth::consumer('FenixEdu');
	
	// check if code is valid
	
	// if code is provided get user data and sign in
	if (!empty($code)) {
		
		// This was a callback request from facebook, get the token
		$token = $fenix->requestAccessToken($code);
		
		// Send a request with it
		$result = json_decode($fenix->request('/person'), true);
		
		$message = 'Your FenixEdu user is: ' . $result['username'] . ' and your name is ' . $result['name'];
		echo $message. "<br/>";
		
		//Var_dump
		//display whole array().
		echo "<pre>";
		dd($result);
	
	}
	// if not ask for permission first
	else {
		// get FenixEdu authorization
		$url = $fenix->getAuthorizationUri();
		
		// return to FenixEdu login url
		 return Redirect::to((string) $url);
	}

}
app/config/app.php
app/config/app.php

$ php artisan config:publish pasadinhas/oauth-4-laravel