-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
migration, route, controller trait and some improvements
- Loading branch information
Showing
10 changed files
with
211 additions
and
27 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
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 @@ | ||
.idea/ |
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
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
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
36 changes: 36 additions & 0 deletions
36
database/migrations/2018_02_21_230848_add_gate_id_column_to_users_table.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,36 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class AddGateIdColumnToUsersTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::table('users', function (Blueprint $table) { | ||
$table->unsignedBigInteger('gate_id')->unique()->nullable(); | ||
}); | ||
|
||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::table('users', function (Blueprint $table) { | ||
if (DB::getDriverName() !== 'sqlite') { | ||
$table->dropUnique(['gate_id']); | ||
} | ||
$table->dropColumn('gate_id'); | ||
}); | ||
} | ||
} |
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,5 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Route; | ||
|
||
Route::get(config('gate.auth_callback_uri', 'auth/callback'), config('gate.auth_callback_controller', 'App\Http\Controllers\Auth\LoginController') . '@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
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
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,99 @@ | ||
<?php | ||
|
||
|
||
namespace Looxis\Gate; | ||
|
||
use Auth; | ||
use Redirect; | ||
use Exception; | ||
use App\Http\Controllers\Controller; | ||
use App\User; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Str; | ||
use Laravel\Socialite\Facades\Socialite; | ||
|
||
trait LoginControllerTrait | ||
{ | ||
protected $redirectAfterLogout = '/login'; | ||
|
||
public function showLoginForm() | ||
{ | ||
if (\LooxisGate::enabled() && !request()->exists('local')) { | ||
return $this->redirectToProvider(); | ||
} | ||
return view('auth.login'); | ||
} | ||
|
||
public function loggedOut(Request $request) | ||
{ | ||
if (\LooxisGate::enabled()) { | ||
return redirect($this->gate()->getLogoutUrl()); | ||
} | ||
|
||
return redirect('/'); | ||
} | ||
|
||
|
||
public function gate() | ||
{ | ||
return Socialite::driver('gate'); | ||
} | ||
|
||
|
||
/** | ||
* Redirect the user to the auth provider's authentication page. | ||
* | ||
* @return Response | ||
*/ | ||
public function redirectToProvider() | ||
{ | ||
return $this->gate()->redirect(); | ||
} | ||
|
||
/** | ||
* Obtain the user information from GitHub. | ||
* | ||
* @return Response | ||
*/ | ||
public function handleProviderCallback() | ||
{ | ||
try { | ||
$user = $this->gate()->user(); | ||
} catch (Exception $e) { | ||
return Redirect::to('auth'); | ||
} | ||
|
||
$authUser = $this->findOrCreateUser($user); | ||
|
||
Auth::login($authUser, true); | ||
|
||
return Redirect::to($this->redirectTo); | ||
} | ||
|
||
/** | ||
* Return user if exists; create and return if doesn't | ||
* | ||
* @param $gateUser | ||
* @return User | ||
*/ | ||
private function findOrCreateUser($gateUser) | ||
{ | ||
if ($authUser = User::where('gate_id', $gateUser->id)->first()) { | ||
return $authUser; | ||
} | ||
if ($authUser = User::where('email', $gateUser->email)->first()) { | ||
$authUser->update([ | ||
'gate_id' => $gateUser->id | ||
]); | ||
return $authUser; | ||
} | ||
|
||
return User::create([ | ||
'name' => $gateUser->name, | ||
'email' => $gateUser->email, | ||
'gate_id' => $gateUser->id, | ||
'api_token' => Str::random(60), | ||
'password' => '', | ||
]); | ||
} | ||
} |