public function handleRegister(Request $request, UserRepository $users)
{
if (!$this->checkCaptcha($request)) {
return json(trans('auth.validation.captcha'), 1);
}
$this->validate($request, ['email' => 'required|email', 'password' => 'required|min:8|max:16', 'nickname' => 'required|nickname|max:255']);
if (!option('user_can_register')) {
return json(trans('auth.register.close'), 7);
}
// If amount of registered accounts of IP is more than allowed amounts,
// then reject the register.
if (User::where('ip', $request->ip())->count() < option('regs_per_ip')) {
// Register a new user.
// If the email is already registered,
// it will return a false value.
$user = User::register($request->input('email'), $request->input('password'), function ($user) use($request) {
$user->ip = $request->ip();
$user->score = option('user_initial_score');
$user->register_at = Utils::getTimeFormatted();
$user->last_sign_at = Utils::getTimeFormatted(time() - 86400);
$user->permission = User::NORMAL;
$user->nickname = $request->input('nickname');
});
if (!$user) {
return json(trans('auth.register.registered'), 5);
}
event(new Events\UserRegistered($user));
return json(['errno' => 0, 'msg' => trans('auth.register.success'), 'token' => $user->getToken()])->withCookie('uid', $user->uid, 60)->withCookie('token', $user->getToken(), 60);
} else {
return json(trans('auth.register.max', ['regs' => option('regs_per_ip')]), 7);
}
}