Skip to content
This repository has been archived by the owner on Jan 10, 2020. It is now read-only.

Commit

Permalink
New Socialite integration
Browse files Browse the repository at this point in the history
  • Loading branch information
mlantz committed Apr 11, 2016
1 parent 54cfc65 commit ea2c339
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 0 deletions.
102 changes: 102 additions & 0 deletions src/Console/Socialite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace Yab\Laracogs\Console;

use Artisan;
use Illuminate\Support\Str;
use Illuminate\Support\Schema;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\Config;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;

class Socialite extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'laracogs:socialite';

protected $files;

/**
* The console command description.
*
* @var string
*/
protected $description = 'Laracogs will add a Socialite auth to your app';

/**
* Execute the console command.
*
* @return mixed
*/
public function fire()
{
if (! file_exists(base_path('resources/views/team/create.blade.php'))) {
$this->line("\n\nPlease perform the starter command:\n");
$this->info("\n\nphp artisan laracogs:starter\n");
$this->line("\n\nThen one you're able to run the unit tests successfully re-run this command, to bootstrap your app :)\n");
} else {
$fileSystem = new Filesystem;

$files = $fileSystem->allFiles(__DIR__.'/../Socialite');
$this->line("\n");
foreach ($files as $file) {
$this->line(str_replace(__DIR__.'/../Socialite/', '', $file));
}

$this->info("\n\nThese files will be published\n");

$result = $this->confirm("Are you sure you want to overwrite any files of the same name?");

if ($result) {
foreach ($files as $file) {
$newFileName = str_replace(__DIR__.'/../Socialite/', '', $file);
$this->line("Copying ".$newFileName."...");
if (is_dir($file)) {
$fileSystem->copyDirectory($file, base_path($newFileName));
} else {
@mkdir(base_path(str_replace(basename($newFileName), '', $newFileName)), 0755, true);
$fileSystem->copy($file, base_path($newFileName));
}
}
}

$this->info("\n\n You will need to run: composer require laravel/socialite");
$this->info("\n\n Then follow the directions regarding billing on: https://laravel.com/docs/");
$this->info("\n\n Please review the setup details for socialite including your provider details.");
$this->info("\n\n You will want to add things like:");
$this->line("\n This to the provdiers in the app config: ");
$this->comment("\n 'providers' => [");
$this->comment("\n\t // Other service providers...");
$this->comment("\n\t Laravel\Socialite\SocialiteServiceProvider::class,");
$this->comment("\n ],");
$this->line("\n Also you will need to add your providers to your services: (example) ");
$this->comment("\n'github' => [");
$this->comment("\n\t'client_id' => 'your-github-app-id',");
$this->comment("\n\t'client_secret' => 'your-github-app-secret',");
$this->comment("\n\t'redirect' => 'http://domain/auth/github/callback',");
$this->comment("\n\t'scopes' => ['user:email'],");
$this->comment("\n],");
$this->line("\n This to the aliases in the app config: ");
$this->comment("\n 'Socialite' => Laravel\Socialite\Facades\Socialite::class,");
$this->line("\n Add this line to (app/Providers/RouteServiceProvider.php):");
$this->comment("\n require app_path('Http/social-routes.php');");
$this->info("Finished setting up a basic socialite structure");
}
}

/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return [];
}
}
1 change: 1 addition & 0 deletions src/LaracogsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public function register()
$this->commands([
\Yab\Laracogs\Console\Api::class,
\Yab\Laracogs\Console\Billing::class,
\Yab\Laracogs\Console\Socialite::class,
\Yab\Laracogs\Console\Bootstrap::class,
\Yab\Laracogs\Console\Docs::class,
\Yab\Laracogs\Console\Crud::class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace App\Http\Controllers\Auth;

use DB;
use URL;
use Auth;
use Config;
use Socialite;
use App\Services\UserService;
use App\Repositories\User\User;
use App\Http\Controllers\Controller;

class SocialiteAuthController extends Controller
{

protected $service;

public function __construct(UserService $userService)
{
$this->service = $userService;
}

/**
* Redirect the user to the GitHub authentication page.
*
* @return Response
*/
public function redirectToProvider($provider)
{
return Socialite::driver($provider)->scopes(Config::get('services.'.$provider.'.scopes'))->redirect();
}

/**
* Obtain the user information from GitHub.
*
* @return Response
*/
public function handleProviderCallback($provider)
{
$user = Socialite::driver($provider)->user();
$localUser = $this->service->findByEmail($user->getEmail());

// If we cannot find user by email locally
if (! $localUser) {
$data = [
'name' => $user->getName(),
'email' => $user->getEmail(),
'password' => md5(rand(222222, 999999))
];

$localUser = $this->createUser($data);
}

if (Auth::login($localUser)) {
return redirect('dashboard');
}

return redirect('/login')->with('error', 'Unable to login.');
}

/**
* Create a user
*
* @param array $data
*
* @return User
*/
public function createUser($data)
{
return DB::transaction(function() use ($data) {
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);

return $this->service->create($user, $data['password']);
});
}
}
12 changes: 12 additions & 0 deletions src/Socialite/app/Http/socialite-routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

/*
|--------------------------------------------------------------------------
| Social Routes
|--------------------------------------------------------------------------
*/

Route::group(['middleware' => 'web'], function() {
Route::get('auth/{provider}', 'Auth\SocialiteAuthController@redirectToProvider');
Route::get('auth/{provider}/callback', 'Auth\SocialiteAuthController@handleProviderCallback');
});
5 changes: 5 additions & 0 deletions src/Starter/app/Services/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public function search($input)
return $this->userRepo->search($input, env('paginate', 25));
}

public function findByEmail($email)
{
return $this->userRepo->findByEmail($email);
}

/*
|--------------------------------------------------------------------------
| Setters
Expand Down

0 comments on commit ea2c339

Please sign in to comment.