You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.3 KiB
52 lines
1.3 KiB
<?php |
|
|
|
namespace App\Http\Controllers\API\v1\AuthApi; |
|
|
|
use App\Models\User; |
|
use Illuminate\Http\Request; |
|
use App\Http\Controllers\Controller; |
|
use Illuminate\Support\Facades\Validator; |
|
|
|
class RegisterApiController extends Controller |
|
{ |
|
/** |
|
* Handle the incoming request. |
|
* |
|
* @param \Illuminate\Http\Request $request |
|
* @return \Illuminate\Http\Response |
|
*/ |
|
public function __invoke(Request $request) |
|
{ |
|
//set validation |
|
$validator = Validator::make($request->all(), [ |
|
'name' => 'required', |
|
'email' => 'required|email|unique:users', |
|
'password' => 'required|min:8|confirmed' |
|
]); |
|
|
|
//if validation fails |
|
if ($validator->fails()) { |
|
return response()->json($validator->errors(), 422); |
|
} |
|
|
|
//create user |
|
$user = User::create([ |
|
'name' => $request->name, |
|
'email' => $request->email, |
|
'password' => bcrypt($request->password) |
|
]); |
|
|
|
//return response JSON user is created |
|
if ($user) { |
|
return response()->json([ |
|
'success' => true, |
|
'user' => $user, |
|
], 201); |
|
} |
|
|
|
//return JSON process insert failed |
|
return response()->json([ |
|
'success' => false, |
|
], 409); |
|
} |
|
}
|
|
|