Refactore Register controller

This commit is contained in:
Bubka 2020-03-04 21:49:45 +01:00
parent b07d599fb8
commit ecc7447980
3 changed files with 42 additions and 75 deletions

View File

@ -3,8 +3,11 @@
namespace App\Http\Controllers\Auth;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Auth\Events\Registered;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
@ -22,22 +25,46 @@ class RegisterController extends Controller
*/
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* @var string
* check if a user exists
* @param Request $request [description]
* @return json
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
public function checkUser()
{
$this->middleware('guest');
$count = DB::table('users')->count();
return response()->json(['userCount' => $count], 200);
}
/**
* Handle a registration request for the application.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function register(Request $request)
{
// check if a user already exists
if( DB::table('users')->count() > 0 ) {
return response()->json(['message' => __('errors.already_one_user_registered')], 400);
}
$this->validator($request->all())->validate();
event(new Registered($user = $this->create($request->all())));
//$this->guard()->login($user);
$success['token'] = $user->createToken('MyApp')->accessToken;
$success['name'] = $user->name;
return response()->json(['message' => $success]);
// return $this->registered($request, $user)
// ?: redirect($this->redirectPath());
}
/**

View File

@ -1,59 +0,0 @@
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
class UserController extends Controller
{
/**
* check if a user exists
* @param Request $request [description]
* @return json
*/
public function checkUser()
{
$count = DB::table('users')->count();
return response()->json(['userCount' => $count], 200);
}
/**
* register new user
* @param Request $request [description]
* @return json
*/
public function register(Request $request)
{
// check if a user already exists
$count = DB::table('users')->count();
if( $count > 0 ) {
return response()->json(['message' => __('errors.already_one_user_registered')], 400);
}
$this->validate($request, [
'name' => 'required',
'email' => 'required|email',
'password' => 'required|confirmed|min:8',
]);
$input = $request->all();
$input['password'] = bcrypt($input['password']);
$user = User::create($input);
$success['token'] = $user->createToken('MyApp')->accessToken;
$success['name'] = $user->name;
return response()->json(['message' => $success]);
}
}

View File

@ -16,9 +16,8 @@
Route::group(['middleware' => 'guest:api'], function () {
Route::post('login', 'Auth\LoginController@login');
Route::post('checkuser', 'UserController@checkUser');
Route::post('register', 'UserController@register');
Route::post('checkuser', 'Auth\RegisterController@checkUser');
Route::post('register', 'Auth\RegisterController@register');
Route::post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail');
Route::post('password/reset', 'Auth\ResetPasswordController@reset');