<commit_before><?php

namespace Laravolt\Auth;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
use Laravolt\Auth\Mail\ActivationMail;

trait Activation
{
    public function register(Request $request)
    {
        $this->validator($request->all())->validate();

        $user = $this->create($request->all());
        $token = $this->createToken($user);

        Mail::to($user)->send(new ActivationMail($token));

        return redirect()->back()->withSuccess(trans('auth::auth.registration_success'));
    }

    public function activate($token)
    {
        $token = DB::table('users_activation')->whereToken($token)->pluck('user_id');

        if ($token->isEmpty()) {
            abort(404);
        }

        $userId = $token->first();

        $user = app(config('auth.providers.users.model'))->findOrFail($userId);
        $user->status = config('laravolt.auth.activation.status_after');
        $user->save();

        DB::table('users_activation')->whereToken($token)->delete();

        return redirect()->route('auth::login')->withSuccess(trans('auth::auth.activation_success'));
    }

    protected function createToken(Model $user)
    {
        $token = md5(uniqid(rand(), true));
        DB::table('users_activation')->insert([<commit_msg>trigger Registered event on user register<commit_after><?php

namespace Laravolt\Auth;

use Carbon\Carbon;
use Illuminate\Auth\Events\Registered;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
use Laravolt\Auth\Mail\ActivationMail;

trait Activation
{
    public function register(Request $request)
    {
        $this->validator($request->all())->validate();

        event(new Registered($user = $this->create($request->all())));
        $token = $this->createToken($user);

        Mail::to($user)->send(new ActivationMail($token));

        return redirect()->back()->withSuccess(trans('auth::auth.registration_success'));
    }

    public function activate($token)
    {
        $token = DB::table('users_activation')->whereToken($token)->pluck('user_id');

        if ($token->isEmpty()) {
            abort(404);
        }

        $userId = $token->first();

        $user = app(config('auth.providers.users.model'))->findOrFail($userId);
        $user->status = config('laravolt.auth.activation.status_after');
        $user->save();

        DB::table('users_activation')->whereToken($token)->delete();

        return redirect()->route('auth::login')->withSuccess(trans('auth::auth.activation_success'));
    }

    protected function createToken(Model $user)
    {
        $token = md5(uniqid(rand(), true));
        DB::table('users_activation')->insert([
{'fraction_non_alphanumeric': 0.17489421720733428, 'fraction_numerical': 0.0028208744710860366, 'mean_word_length': 3.6356209150326797, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 45, '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': '4898670', 'n_tokens_mistral': 836, 'n_tokens_neox': 823, 'n_words': 119}