-
app/helpers.php
Open in GitHubif (! function_exists('generateToken')) { function generateToken() { do { $token = mt_rand(100000, 999999); } while (Token::where('token', $token)->exists()); return (string) $token; } } // if (! function_exists('generateTempPassword')) { function generateTempPassword() { return bin2hex(random_bytes(3)); } } //
-
app/Http/Controllers/Auth/RegistrationController.php
Open in GitHubuse App\Enums\StatusEnum; use App\Enums\VerificationEnum; use App\Http\Controllers\Controller; use App\Http\Controllers\RespondsWithHttpStatusController; use App\Http\Requests\RegistrationFormRequest; use App\Models\Token; use App\Models\User; use App\Notifications\SendEmailTokenNotification; use Illuminate\Support\Facades\Hash; class RegistrationController extends RespondsWithHttpStatusController { public function __invoke(RegistrationFormRequest $request) { Token::where('email', $request->email)->where('type', VerificationEnum::Email)->delete(); $password = generateTempPassword(); $user = User::create([ 'first_name' => $request->first_name, 'last_name' => $request->last_name, 'email' => $request->email, 'gender' => $request->gender, 'telephone' => $request->telephone, 'password' => Hash::make($password), 'status' => StatusEnum::ACTIVE ]) ->assignRole($request->input('roles')); $tokenData = Token::create([ 'token' => generateToken(), 'email' => $request->email, 'type' => VerificationEnum::Email ]); $user->notify(new SendEmailTokenNotification($tokenData->token, $password)); return $this->responseOk([ 'message' => 'Registration successful, An email has been sent to the user', ]); } }