Laravel JWT API

Official Document:
https://jwt-auth.readthedocs.io/en/develop/quick-start/

<?php
namespace App\Http\Controllers\Api;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use Validator;

use JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Api\ParentApi;

use Carbon\Carbon;

use App\Models\User;

use App\Traits\AjaxResponse;


class UserApi extends ParentApi
{
    use AjaxResponse;

    public function __construct(){
        //parent::__construct();
        $this->middleware('auth:api', ['except' => ['test', 'login', 'register']]);
    }

    public function test(Request $req){
        logg('ok');
    }

    public function test_login(Request $req){
        logg('login ok');
    }

    public function register(Request $req){
        //echo 'Disabled'; exit();
        $user = new User();
        $user->name = $req->input('name');
        $user->email = $req->input('email');
        $user->account = $req->input('account');
        $user->password = Hash::make($req->input('password'));
        $user->save();

        return $this->success_response(true);
    }

    public function login(Request $request){
        $validator = Validator::make($request->all(), [
            'userName' => 'required',
            'password' => 'required|string|min:6',
        ]);

        if ($validator->fails()) {
            return response()->json($validator->errors(), 422);
        }

        $data = $validator->validated();
        $credential = [];
        $credential['account'] = $data['userName'];
        $credential['password'] = $data['password'];

        if (! $token = $this->guard()->attempt($credential)) {
            return $this->fail_response('Unauthorized', 0, 401);
            //return response()->json(['error' => 'Unauthorized'], 401);
        }

        return $this->respondWithToken($token);
    }

    protected function respondWithToken($token)
    {
        //1hour
        $seconds = $this->guard()->factory()->getTTL() * 60;
        $expired = Carbon::now()->addSeconds($seconds);
        //logg($expired);

        $data = [
            'token' => $token, 
            'expired_at' => $expired->toDateTimeString(), 
            'expire_seconds' => $seconds, 
            'token_type' => 'bearer', 
        ];

        return $this->success_response($data);
    }
}