-
Notifications
You must be signed in to change notification settings - Fork 3
4. Register
dungda-0794 edited this page Mar 16, 2023
·
11 revisions
Note: Validation & logic register inside service
- Validate attributes
- Callback use model
- Save model to database
POST /register
- Parameters
| Request Field | Field Type | Required | Field Desc |
|---|---|---|---|
| username | string | true | username for user |
| password | string | true | password for user |
| string | true | email for user |
- Responses
| Response Field | Field Type | Required | Field Desc |
|---|---|---|---|
| id | integer | true | id for user |
| username | string | true | username for user |
| string | true | email for user |
curl -X 'POST' \
'http://localhost/api/register' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"username": "user123456",
"password": "passwordRequired@123",
"email": "testuser02@local.ltd"
}'{
"id": 13,
"email": "testuser02@local.ltd",
"username": "user123456"
}Make route function the following:
# routes/api.php
Route::post('register', 'AuthController@register');Make the register function the following:
# App\Http\Controllers\AuthController
...
use SunAsterisk\Auth\Contracts\AuthJWTInterface;
...
protected AuthJWTInterface $service;
public function __construct(AuthJWTInterface $service)
{
$this->service = $service;
}
public function showRegistrationForm()
{
return view('auth.register');
}
public function register(Request $request)
{
$rules = [];
$fields = $request->only(['username', 'password', 'email']);
$result = $this->service->register($fields, $rules, function ($entity) {
return $entity->only(['id', 'email', 'username']);
});
return response()->json($result);
}Make route function the following:
# routes/web.php
Route::get('register', [App\Http\Controllers\AuthController::class, 'showRegistrationForm'])->name('register');
Route::post('register', [App\Http\Controllers\AuthController::class, 'register']);Make login function the following:
...
use SunAsterisk\Auth\Contracts\AuthSessionInterface;
...
protected AuthSessionInterface $service;
public function __construct(AuthSessionInterface $service)
{
$this->service = $service;
}
public function register(Request $request)
{
$fields = $request->only(['username', 'password', 'email']);
$fields['name'] = $fields['username'];
$this->service->register($fields, [], function ($entity) {
//
});
return redirect()->intended('home');
}If do you want after register, user will authenticated, Please set true for argument $setGuard as bellow
$this->service->register($fields, [], function ($entity) {
//
}, true);
/**
* [register]
* @param array $fields [The user's attributes for register.]
* @param array $rules [The rules for register validate.]
* @param callable|null $callback [The callback function has the entity model.]
* @return [array]
*/
public function register(array $params = [], array $rules = [], callable $callback = null): array;# SunAsterisk\Auth\Services\AuthJWTService;
public function register(array $params = [], array $rules = [], callable $callback = null): array
{
if (empty($rules)) {
$rules = [
'username' => ['required', 'string', "unique:{$table}," . $this->username()],
'password' => [
'required',
'min:6',
'regex:/^.*(?=.{3,})(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[\d\x])(?=.*[!@#$%]).*$/',
],
];
if (isset($params['email'])) {
$rules['email'] = ['required', 'string', "unique:{$table},email"];
}
}
...
}# SunAsterisk\Auth\Services\AuthJWTService;
public function register(array $params = [], array $rules = [], callable $callback = null): array
{
...
$params[$this->passwd()] = Hash::make($params[$this->passwd()]);
}# SunAsterisk\Auth\Services\AuthJWTService;
public function register(array $params = [], array $rules = [], callable $callback = null): array
{
...
$item = $this->repository->create($params);
}Method register will return an array
You can changes or validate attributes as following
# App\Http\Controllers\AuthController
public function register(Request $request)
{
$fields = $request->only(['password', 'email']);
// customer validate
$rules = [
'email' => 'required|email',
'password' => 'required',
];
$result = $this->service->register($fields, $rules);
return response()->json($result);
}