This repository has been archived by the owner on Jan 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/Socialite/app/Http/Controllers/Auth/SocialiteAuthController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters