<?php

namespace Modules\User\Http\Controllers\Api\Auth;

use App\Api\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Jason\Api\Api;
use Modules\User\Events\UserLoginSuccess;
use Modules\User\Http\Requests\RegisterRequest;
use Modules\User\Models\User;

class RegisterController extends Controller
{

    /**
     * Notes   : 注册
     *
     * @Date   : 2021/3/14 1:24 下午
     * @Author : <Jason.C>
     * @param  RegisterRequest  $request
     * @return JsonResponse
     */
    public function index(RegisterRequest $request): JsonResponse
    {
        $username = $request->username;
        $password = $request->password;

        $user = User::create([
            'username' => $username,
            'password' => $password,
        ]);

        $token = Api::login($user);

        event(new UserLoginSuccess($user, $request, '用户注册'));

        return $this->success([
            'access_token' => $token,
            'token_type'   => 'Bearer',
        ]);
    }

}
