mirror of
https://github.com/Bubka/2FAuth.git
synced 2025-01-11 16:58:58 +01:00
Refactore Register controller
This commit is contained in:
parent
b07d599fb8
commit
ecc7447980
@ -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());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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]);
|
||||
}
|
||||
}
|
@ -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');
|
||||
|
Loading…
Reference in New Issue
Block a user