<commit_before><?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Classes\EsiConnection;
use Socialite;
use Illuminate\Support\Facades\Auth;
use App\User;
use App\Whitelist;

class AuthController extends Controller
{
    /**
     * Redirect the user to the EVE Online SSO page and ask for permission to search corporation assets.
     *
     * @return Response
     */
    public function redirectToProvider()
    {
        return Socialite::driver('eveonline')->scopes([
//            'esi-industry.read_corporation_mining.v1',
            'esi-wallet.read_corporation_wallets.v1',
            'esi-mail.send_mail.v1',
            'esi-universe.read_structures.v1',
            'esi-corporations.read_structures.v1',
        ])->redirect();
    }

    /**
     * Obtain the user information from EVE Online.
     *
     * @return Response
     */
    public function handleProviderCallback()
    {
        $esi = new EsiConnection;
        
        $user = Socialite::driver('eveonline')->user();
        $authUser = $this->findOrCreateUser($user);

        // Check if the user is whitelisted to access the app.
        Whitelist::where('eve_id', $authUser->eve_id)->firstOrFail();

        // Check if the user is a member of the correct alliance.
        $character = $esi->esi->invoke('get', '/characters/{character_id}/', [
            'character_id' => $authUser->eve_id,
        ]);
        $corporation = $esi->esi->invoke('get', '/corporations/{corporation_id}/', [
            'corporation_id' => $character->corporation_id,
        ]);

        // If an alliance is set, it must match the stored environment variable.
        if ($corporation->alliance_id && $corporation->alliance_id == env('EVE_ALLIANCE_ID', NULL))
        {
            Auth::login($authUser, true);
        }

        return redirect('/');
    }

    /**
     * Return user if exists; create and return if doesn't
     *
     * @param $user
     * @return User
     */
     private function findOrCreateUser($user)
     {
         if ($authUser = User::where('eve_id', $user->id)->first())
         {
             $authUser->token = $user->token;
             $authUser->refresh_token = $user->refreshToken;
             $authUser->save();
             return $authUser;
         }
 
         return User::create([
             'eve_id' => $user->id,
             'name' => $user->name,
             'avatar' => $user->avatar,
             'token' => $user->token,
             'refresh_token' => $user->refreshToken,
         ]);
     }
    
}<commit_msg>Change the order that things happen in the auth process.<commit_after><?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Classes\EsiConnection;
use Socialite;
use Illuminate\Support\Facades\Auth;
use App\User;
use App\Whitelist;

class AuthController extends Controller
{
    /**
     * Redirect the user to the EVE Online SSO page and ask for permission to search corporation assets.
     *
     * @return Response
     */
    public function redirectToProvider()
    {
        return Socialite::driver('eveonline')->scopes([
//            'esi-industry.read_corporation_mining.v1',
            'esi-wallet.read_corporation_wallets.v1',
            'esi-mail.send_mail.v1',
            'esi-universe.read_structures.v1',
            'esi-corporations.read_structures.v1',
        ])->redirect();
    }

    /**
     * Obtain the user information from EVE Online.
     *
     * @return Response
     */
    public function handleProviderCallback()
    {
        
        // Find or create the user.
        $user = Socialite::driver('eveonline')->user();
        $authUser = $this->findOrCreateUser($user);

        // Check if the user is whitelisted to access the app.
        Whitelist::where('eve_id', $authUser->eve_id)->firstOrFail();

        $esi = new EsiConnection;

        // Check if the user is a member of the correct alliance.
        $character = $esi->esi->invoke('get', '/characters/{character_id}/', [
            'character_id' => $authUser->eve_id,
        ]);
        $corporation = $esi->esi->invoke('get', '/corporations/{corporation_id}/', [
            'corporation_id' => $character->corporation_id,
        ]);

        // If an alliance is set, it must match the stored environment variable.
        if ($corporation->alliance_id && $corporation->alliance_id == env('EVE_ALLIANCE_ID', NULL))
        {
            Auth::login($authUser, true);
        }

        return redirect('/');
    }

    /**
     * Return user if exists; create and return if doesn't
     *
     * @param $user
     * @return User
     */
     private function findOrCreateUser($user)
     {
         if ($authUser = User::where('eve_id', $user->id)->first())
         {
             $authUser->token = $user->token;
             $authUser->refresh_token = $user->refreshToken;
             $authUser->save();
             return $authUser;
         }
 
         return User::create([
             'eve_id' => $user->id,
             'name' => $user->name,
             'avatar' => $user->avatar,
             'token' => $user->token,
             'refresh_token' => $user->refreshToken,
         ]);
     }
    
}
{'fraction_non_alphanumeric': 0.13750954927425516, 'fraction_numerical': 0.0019098548510313217, 'mean_word_length': 2.108011869436202, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5821073', 'n_tokens_mistral': 1544, 'n_tokens_neox': 1507, 'n_words': 336}