user login
This commit is contained in:
@@ -2,12 +2,12 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use Laravel\Socialite\Contracts\Factory as Socialite;
|
||||||
|
use Laravel\Illuminate\Two\User as SocialiteUser;
|
||||||
|
use App\User;
|
||||||
//use Laravel\Socialite\Contracts\Factory as Socialite;
|
//use Laravel\Socialite\Contracts\Factory as Socialite;
|
||||||
//use Laravel\Socialite\Two\User as SocialiteUser;
|
//use Laravel\Socialite\Two\User as SocialiteUser;
|
||||||
//use Illuminate\Foundation\Auth\AuthenticatesUsers;
|
//use Illuminate\Foundation\Auth\AuthenticatesUsers;
|
||||||
use Socialite;
|
|
||||||
//use SocialiteUser;
|
|
||||||
|
|
||||||
|
|
||||||
class AuthController extends Controller
|
class AuthController extends Controller
|
||||||
{
|
{
|
||||||
@@ -28,8 +28,46 @@ class AuthController extends Controller
|
|||||||
* @return Response
|
* @return Response
|
||||||
*/
|
*/
|
||||||
public function handleProviderCallback(Socialite $social) {
|
public function handleProviderCallback(Socialite $social) {
|
||||||
$user = $social->driver('eveonline')->user();
|
$eve_data = $social->driver('eveonline')->user();
|
||||||
Auth::login($user);
|
//Get or create the User bound to this login
|
||||||
|
$user = $this->findOrCreateUser($eve_data);
|
||||||
|
//Auth the user
|
||||||
|
auth()->login($user);
|
||||||
|
|
||||||
|
return redirect()->to('/dashboard');
|
||||||
|
|
||||||
dd($user);
|
dd($user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if a user exists in the database, else, create and
|
||||||
|
* return the user object.
|
||||||
|
*
|
||||||
|
* @param \Laravel\Socialite\Two\User $user
|
||||||
|
*/
|
||||||
|
private function findOrCreateUser(SociateUser $eve_user): User {
|
||||||
|
//check if the user already exists in the database
|
||||||
|
if($existing = User::find($eve_user->character_id)) {
|
||||||
|
//Check the owner hash and update if necessary
|
||||||
|
if($existing->character_owner_hash !== $eve_user->character_owner_hash) {
|
||||||
|
$existing->owner_has = $eve_user->character_owner_hash;
|
||||||
|
$existing->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $existing;
|
||||||
|
}
|
||||||
|
|
||||||
|
return User::forceCreate([
|
||||||
|
'id' => $eve_user->character_id,
|
||||||
|
'name' => $eve_user->name,
|
||||||
|
'owner_hash' => $eve_user->character_owner_hash,
|
||||||
|
'email' => null,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function loginUser(User $user): bool {
|
||||||
|
auth()->login($user, true);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
16
app/SSOAccount.php
Normal file
16
app/SSOAccount.php
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class SSOAccount extends Model
|
||||||
|
{
|
||||||
|
protected $fillable = [
|
||||||
|
'name', 'email', 'avatar', 'owner_hash', 'id', 'expiresIn', 'token', 'refreshToken',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function user() {
|
||||||
|
return $this->belongsTo(User::class);
|
||||||
|
}
|
||||||
|
}
|
||||||
25
app/SSOAccountService.php
Normal file
25
app/SSOAccountService.php
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App;
|
||||||
|
|
||||||
|
use Laravel\Socialite\Contracts\User as ProviderUser;
|
||||||
|
|
||||||
|
class SSOAccountService {
|
||||||
|
|
||||||
|
public function createOrGetUser(ProviderUser $providerUser) {
|
||||||
|
$account = SocialAccount::whereProvider('eveonline')->whereProviderUserId($providerUser->getId())->first();
|
||||||
|
|
||||||
|
if($account) {
|
||||||
|
return $account->user;
|
||||||
|
} else {
|
||||||
|
$account = new SSOAccount([
|
||||||
|
'provider_user_id' => $providerUser->getId(),
|
||||||
|
'provider' => 'eveonline',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$user = User::whereEmail($providerUser->getCharacterId())->first();
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,7 +16,7 @@ class User extends Authenticatable
|
|||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $fillable = [
|
protected $fillable = [
|
||||||
'name', 'email', 'password', 'character_id'
|
'name', 'email', 'owner_hash', 'character_id',
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -17,10 +17,13 @@ class CreateUsersTable extends Migration
|
|||||||
$table->increments('id');
|
$table->increments('id');
|
||||||
$table->string('name');
|
$table->string('name');
|
||||||
$table->string('character_id');
|
$table->string('character_id');
|
||||||
|
$table->string('access_token');
|
||||||
|
$table->string('refresh_token');
|
||||||
|
$table->integer('expires_in');
|
||||||
|
$table->string('owner_has');
|
||||||
$table->string('user_type')->default('Guest');
|
$table->string('user_type')->default('Guest');
|
||||||
$table->string('email')->unique();
|
$table->string('email')->unique()->nullable();
|
||||||
$table->timestamp('email_verified_at')->nullable();
|
$table->timestamp('email_verified_at')->nullable();
|
||||||
$table->string('password');
|
|
||||||
$table->rememberToken();
|
$table->rememberToken();
|
||||||
$table->timestamps();
|
$table->timestamps();
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user