Skip to content

Commit

Permalink
Added custom registration logic to account for promoter codes
Browse files Browse the repository at this point in the history
  • Loading branch information
Naapperas committed Aug 4, 2024
1 parent e329f52 commit 95f40ad
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 3 deletions.
6 changes: 4 additions & 2 deletions .env
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
APP_NAME=ENEI
APP_KEY=base64:O8e0ShbVWB3OAAFGbSUweWr1JM3xutu1isYLKLk4mqU=
APP_DEBUG=true
APP_URL=http://localhost
APP_URL=http://localhost:8000
APP_ENV=local
APP_PORT=8000

LOCALE=pt
FAKER_LOCALE=pt_PT
Expand All @@ -15,7 +17,7 @@ DB_USERNAME=postgres

SCOUT_QUEUE=true
SCOUT_DRIVER=meilisearch
MEILISEARCH_HOST=http://meilisearch:3331
MEILISEARCH_HOST=http://meilisearch:7700
MEILISEARCH_KEY=my-master-key

MEILISEARCH_NO_ANALYTICS=false
Expand Down
7 changes: 7 additions & 0 deletions app/Actions/Fortify/CreateNewUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,15 @@ public function create(array $input): User
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => $this->passwordRules(),
'terms' => Jetstream::hasTermsAndPrivacyPolicyFeature() ? ['accepted', 'required'] : '',
'promoter' => ['optional', 'string'],
])->validate();

$promoter = $input['promoter'];

if (! is_null($promoter)) {
// TODO: Trigger promoter point attribution algorithm
}

$data = [
'name' => $input['name'],
'email' => $input['email'],
Expand Down
66 changes: 66 additions & 0 deletions app/Http/Controllers/RegisteredUserController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Auth\Events\Registered;
use Illuminate\Contracts\Auth\StatefulGuard;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Str;
use Laravel\Fortify\Contracts\CreatesNewUsers;
use Laravel\Fortify\Contracts\RegisterResponse;
use Laravel\Fortify\Contracts\RegisterViewResponse;
use Laravel\Fortify\Fortify;

class RegisteredUserController extends Controller
{
/**
* The guard implementation.
*
* @var \Illuminate\Contracts\Auth\StatefulGuard
*/
protected $guard;

/**
* Create a new controller instance.
*
* @return void
*/
public function __construct(StatefulGuard $guard)
{
$this->guard = $guard;
}

/**
* Show the registration view.
*/
public function create(Request $request): RegisterViewResponse
{
$promoter = null;
if ($request->is('register/promoter/*')) {
$promoter = $request->route('promoter');
dd($promoter);
}

return app(RegisterViewResponse::class);
}

/**
* Create a new registered user.
*/
public function store(Request $request,
CreatesNewUsers $creator): RegisterResponse
{
if (config('fortify.lowercase_usernames')) {
$request->merge([
Fortify::username() => Str::lower($request->{Fortify::username()}),
]);
}

event(new Registered($user = $creator->create($request->all())));

$this->guard->login($user);

return app(RegisterResponse::class);
}
}
16 changes: 16 additions & 0 deletions resources/js/Pages/Auth/Register.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@ import TextInput from "@/Components/TextInput.vue";
import CardLayout from "../../Layouts/CardLayout.vue";
import route from "ziggy-js";
interface Props {
promoter?: string;
}
defineProps<Props>();
const form = useForm({
name: "",
email: "",
password: "",
password_confirmation: "",
promoter: "",
terms: false,
});
Expand Down Expand Up @@ -65,6 +72,15 @@ const submit = () => {
:error-message="form.errors.password_confirmation"
/>

<TextInput
id="promoter"
v-model="form.promoter"
label="Código de promotor"
type="text"
required
:error-message="form.errors.promoter"
/>

<label
v-if="$page.props.jetstream.hasTermsAndPrivacyPolicyFeature"
class="flex items-center gap-2 self-stretch"
Expand Down
6 changes: 5 additions & 1 deletion routes/fortify.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use App\Http\Controllers\RegisteredUserController;
use Illuminate\Support\Facades\Route;
use Laravel\Fortify\Features;
use Laravel\Fortify\Http\Controllers\AuthenticatedSessionController;
Expand All @@ -13,7 +14,6 @@
use Laravel\Fortify\Http\Controllers\PasswordResetLinkController;
use Laravel\Fortify\Http\Controllers\ProfileInformationController;
use Laravel\Fortify\Http\Controllers\RecoveryCodeController;
use Laravel\Fortify\Http\Controllers\RegisteredUserController;
use Laravel\Fortify\Http\Controllers\TwoFactorAuthenticatedSessionController;
use Laravel\Fortify\Http\Controllers\TwoFactorAuthenticationController;
use Laravel\Fortify\Http\Controllers\TwoFactorQrCodeController;
Expand Down Expand Up @@ -71,6 +71,10 @@
Route::get(RoutePath::for('register', '/register'), [RegisteredUserController::class, 'create'])
->middleware(['guest:'.config('fortify.guard')])
->name('register');

Route::get(RoutePath::for('register_with_promoter', '/register/promoter/{promoter}'), [RegisteredUserController::class, 'create'])
->middleware(['guest:'.config('fortify.guard')])
->name('register_with_promoter');
}

Route::post(RoutePath::for('register', '/register'), [RegisteredUserController::class, 'store'])
Expand Down

0 comments on commit 95f40ad

Please sign in to comment.