<commit_before><?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\User;
use App;
use Request;
use View;
use Redirect;
use Session;
use Config;
use Response;
use Auth;
use DB;
use EtuUTT;

/**
 * OAuth authentication with the etu.utt.fr website.
 *
 * @author  <NAME> <<EMAIL>>
 * @license MIT
 */
class OAuthController extends Controller
{
    /**
     * Return a JSON object with the link to auth with Etu UTT
     *
     * @return Response
     */
    public function getRedirectLink()
    {
        $id = Config::get('services.etuutt.mobile_client.id');
        return Response::json([
            'redirectUri' => Config::get('services.etuutt.baseuri.public').'/api/oauth/authorize?client_id=' . $id . '&scopes=private_user_account&response_type=code&state=xyz'
        ]);
    }

    public function discordCallback()
    {
        // authorization code required to continue
        if (!Request::has('access_token')) {
            return Response::json(["message" => "missing parameter : access_token"], 401);
        }

        $client = new \GuzzleHttp\Client([
            'base_uri' => Config::get('services.etuutt.baseuri.api'),
            'auth' => [
                Config::get('services.etuutt.mobile_client.id'),
                Config::get('services.etuutt.mobile_client.secret')
            ]
        ]);

        try {
            $response = $client->get('/api/private/user/account?access_token=' .Request::input('access_token'));
        } catch (\GuzzleHttp\Exception\GuzzleException $e) {
            return Response::json(["message" => "failed to fetch your account data"], 500);
        }

        $json = json_decode($response->getBody()->getContents(), true)['data'];

        $user = EtuUTT::updateOrCreateUser($json, Request::input('access_token'), Request::input('access_token'));
        $user->admin = 100;

        // generate auth token for this student
        $createdToken = $user->createToken("discord");
        $passport_access_token = $createdToken->accessToken;
        $passport_expires_at = $createdToken->token->expires_at->getTimestamp() * 1000;

        return Response::json([
            "access_token" => $passport_access_token,
            "expires_at" => $passport_expires_at,
        ]);
    }

<commit_msg>fix: add user save in discord callback<commit_after><?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\User;
use App;
use Request;
use View;
use Redirect;
use Session;
use Config;
use Response;
use Auth;
use DB;
use EtuUTT;

/**
 * OAuth authentication with the etu.utt.fr website.
 *
 * @author  <NAME> <<EMAIL>>
 * @license MIT
 */
class OAuthController extends Controller
{
    /**
     * Return a JSON object with the link to auth with Etu UTT
     *
     * @return Response
     */
    public function getRedirectLink()
    {
        $id = Config::get('services.etuutt.mobile_client.id');
        return Response::json([
            'redirectUri' => Config::get('services.etuutt.baseuri.public').'/api/oauth/authorize?client_id=' . $id . '&scopes=private_user_account&response_type=code&state=xyz'
        ]);
    }

    public function discordCallback()
    {
        // authorization code required to continue
        if (!Request::has('access_token')) {
            return Response::json(["message" => "missing parameter : access_token"], 401);
        }

        $client = new \GuzzleHttp\Client([
            'base_uri' => Config::get('services.etuutt.baseuri.api'),
            'auth' => [
                Config::get('services.etuutt.mobile_client.id'),
                Config::get('services.etuutt.mobile_client.secret')
            ]
        ]);

        try {
            $response = $client->get('/api/private/user/account?access_token=' .Request::input('access_token'));
        } catch (\GuzzleHttp\Exception\GuzzleException $e) {
            return Response::json(["message" => "failed to fetch your account data"], 500);
        }

        $json = json_decode($response->getBody()->getContents(), true)['data'];

        $user = EtuUTT::updateOrCreateUser($json, Request::input('access_token'), Request::input('access_token'));
        $user->admin = 100;
        $user->save();

        // generate auth token for this student
        $createdToken = $user->createToken("discord");
        $passport_access_token = $createdToken->accessToken;
        $passport_expires_at = $createdToken->token->expires_at->getTimestamp() * 1000;

        return Response::json([
            "access_token" => $passport_access_token,
            "expires_at" => $passport_expires_at,
        ]);
    }


{'fraction_non_alphanumeric': 0.14491197939029626, 'fraction_numerical': 0.0055817947617003, 'mean_word_length': 2.778588807785888, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 42, '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': '6071628', 'n_tokens_mistral': 1412, 'n_tokens_neox': 1357, 'n_words': 277}