<commit_before><?php

namespace TMPHP\RestApiGenerators\Helpers\Traits;

use Dingo\Api\Http\Request;
use Dingo\Api\Routing\Helpers;
use Tymon\JWTAuth\Facades\JWTAuth;

/**
 * Trait Authenticatable
 * @package TMPHP\RestApiGenerators\Helpers\Traits
 */
trait Authenticatable
{
    use ErrorFormatable, Helpers;

    protected $userModel;

    /**
     * User registration
     *
     * @param Request $request
     * @return $this
     */
    public function register(Request $request)
    {
        //validation //todo catch ValidationException here to avoid 500 status code
        $this->validate($request, [
            'name' => 'required|string',
            'email' => 'required|email|unique:users,email',
            'password' => '<PASSWORD>'
        ]);

        //create user in database
        $this->userModel->fill($request->all())->save();

        //get token
        $token = 'Bearer ' . JWTAuth::fromUser($this->userModel);

        //return token in response header
        return $this->response->noContent()->header(
            'x-app-authorization',
            $token
        );
    }
}<commit_msg>Added login and logout methods If applied, commit will add login and logout methods<commit_after><?php

namespace TMPHP\RestApiGenerators\Helpers\Traits;

use Dingo\Api\Http\Request;
use Dingo\Api\Routing\Helpers;
use Tymon\JWTAuth\Exceptions\JWTException;
use Tymon\JWTAuth\Facades\JWTAuth;

/**
 * Trait Authenticatable
 * @package TMPHP\RestApiGenerators\Helpers\Traits
 */
trait Authenticatable
{
    use ErrorFormatable, Helpers;

    protected $userModel;

    /**
     * User registration
     *
     * @param Request $request
     * @return $this
     */
    public function register(Request $request)
    {
        //validation //todo catch ValidationException here to avoid 500 status code
        $this->validate($request, [
            'name' => 'required|string',
            'email' => 'required|email|unique:users,email',
            'password' => '<PASSWORD>:6,<PASSWORD>'
        ]);

        //create user in database
        $this->userModel->fill($request->all())->save();

        //get token
        $token = 'Bearer ' . JWTAuth::fromUser($this->userModel);

        //return token in response header
        return $this->response->noContent()->header(
            'x-app-authorization',
            $token
        );
    }

    /**
     * Logging in user
     *
     * @param Request $request
     * @return $this|\Dingo\Api\Http\Response
     */
    public function login(Request $request)
    {

        $this->validate($request, [
            'email' => 'required',
            'password' => '<PASSWORD>'
        ]);

        $token = JWTAuth::attempt($request->only(['email', 'password']));

        if ($token){
            //return token in response header
            return $this->response->noContent()->header(
                'x-app-authorization',
                $token
            );
        }else{
            return 'f off';
        }
    }

    /**
     * Logging out user
     *
     * @param Request $request
     */
    public function logout(Request $request){

        //todo delete token

        //todo return response
    }
}
{'fraction_non_alphanumeric': 0.13364632237871674, 'fraction_numerical': 0.002190923317683881, 'mean_word_length': 2.1333333333333333, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 38, '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': '4654444', 'n_tokens_mistral': 942, 'n_tokens_neox': 922, 'n_words': 200}