133 lines
4.3 KiB
PHP
133 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Auth;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
|
|
//Internal Library
|
|
use App\Library\Login\LoginHelper;
|
|
|
|
class LoginController extends Controller
|
|
{
|
|
/**
|
|
* Where to redirect users after login.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $redirectto = '/dashboard';
|
|
|
|
/**
|
|
* Create a new controller instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct() {
|
|
$this->middleware('guest')->except(['logout',
|
|
'handleProviderCallback',
|
|
'redirectToProvider']);
|
|
}
|
|
|
|
/**
|
|
* Logout function
|
|
*
|
|
* @return void
|
|
*/
|
|
public function logout() {
|
|
Auth::logout();
|
|
return redirect('/');
|
|
}
|
|
|
|
/**
|
|
* Redirect to the provider's website
|
|
*
|
|
* @return Socialite
|
|
* @return character_owner_hash
|
|
* @return character_name
|
|
* @return character_id
|
|
* @return token
|
|
* @return refreshToken
|
|
* @return expiresIn
|
|
* @return user (Holds jwt)
|
|
*/
|
|
public function redirectToProvider() {
|
|
//The default scope is public data for everyone due to OAuth2 Tokens
|
|
//Add esi-mail.send_mail.v1 to send mails more efficiently
|
|
$scopes = ['publicData', 'esi-mail.send_mail.v1'];
|
|
|
|
//Collect any other scopes from the database.
|
|
//If we are logged in we are linking another character to this one.
|
|
//Attempt to use the same scopes for this character as the original.
|
|
if(Auth::check()) {
|
|
$extrascopes = EsiScope::where([
|
|
'character_id' => auth()->user()->getId();
|
|
])->get(['scope']);
|
|
|
|
//Pop each scope onto the array of scopes
|
|
foreach($extraScopes as $extra) {
|
|
array_push($scopes, $extra->scope);
|
|
}
|
|
|
|
/**
|
|
* Place the scopes in the session.
|
|
* Place the original character id in the session.
|
|
*/
|
|
session()->put('scopes', $scopes);
|
|
session()->put('orgCharacter', auth()->user()->getId());
|
|
}
|
|
|
|
return Socialite::driver('eveonline')->scopes($scopes)->redirect();
|
|
}
|
|
|
|
/**
|
|
* Get token from callback
|
|
* Redirect to the dashboard if logging in successfully.
|
|
*
|
|
* @return redirect()
|
|
*/
|
|
public function handleProviderCallback(Socialite $social) {
|
|
//Get the sso user from the socialite driver
|
|
$ssoUser = $social->driver('eveonline')->user();
|
|
|
|
$scpSession = session()->pull('scopes');
|
|
|
|
//If the user was already logged in, let's do some checks to see if we are adding
|
|
//additional scopes to the user's account
|
|
if(Auth::check()) {
|
|
//If we are logged in already and the session contains the original characters, then we are creating an alt
|
|
//for the original character
|
|
if(session()->has('orgCharacter')) {
|
|
$orgCharacter = session()->pull('orgCharacter');
|
|
|
|
if(LoginHelper::createAlt($ssoUser, $orgCharacter)) {
|
|
return redirect()->to('/profile')->with('success', 'Alt registered.');
|
|
} else {
|
|
return redirect()->to('/profile')->with('error', 'Unable to register alt or it was previously registered.');
|
|
}
|
|
} else {
|
|
if(sizeof($ssoUser->scopes) > 1) {
|
|
$tokenCount = EsiToken::where([
|
|
'character_id' => $ssoUser->id,
|
|
])->count();
|
|
if($tokenCount > 0) {
|
|
LoginHelper::UpdateEsiToken($ssoUser);
|
|
} else {
|
|
LoginHelper::SaveEsiToken($ssoUser);
|
|
}
|
|
LoginHelper::SetScopes($ssoUser->scopes, $ssoUser->id);
|
|
return redirect()->to('/dashboard')->with('success', 'Successfully updated ESI scopes.');
|
|
}
|
|
}
|
|
} else {
|
|
//If the user wasn't logged in, then create a new user
|
|
$user = LoginHelper::createOrGetUser($ssoUser);
|
|
//Login in the new user
|
|
auth()->login($user, true);
|
|
//Redirect back to the dashboard
|
|
return redirect()->to('/dashboard')->with('success', 'Successfully Logged In.');
|
|
}
|
|
}
|
|
|
|
|
|
}
|