Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jcoder02k's avatar

Custom Registraion with condition/logic

How can i create a login form that verify email and showing email field only? Once email is enter and if it's exist but no password it will show button that have "Verify you r email". Once verified it will redirect to profile page to create the password. Another condition if email exist and that have email has a password it will show the password field after showing email and password field it will show the ""Sign in" button. Last condition if email is enter and don't exist it will redirect to Registration for new user. Any idea how to do this condition?

0 likes
7 replies
LaryAI's avatar
Level 58

To achieve the custom registration flow you've described, you'll need to create a multi-step form that conditionally displays fields based on the user's input and the state of the user's data in your database. Here's a high-level solution using Laravel, which is a common framework discussed on Laracasts:

  1. Create a route for the initial form that only asks for the email address.
  2. Create a controller method to handle the form submission and check the user's email against the database.
  3. Depending on the result, conditionally display the next step in the registration process.

Here's an example of how you might implement this in Laravel:

Routes (web.php):

Route::get('/register', 'AuthController@showRegistrationForm')->name('register');
Route::post('/register', 'AuthController@register');
Route::get('/verify-email', 'AuthController@showVerifyEmailForm')->name('verify-email');
Route::post('/verify-email', 'AuthController@verifyEmail');
Route::get('/create-password/{user}', 'AuthController@showCreatePasswordForm')->name('create-password');
Route::post('/create-password', 'AuthController@createPassword');

AuthController:

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Auth;

class AuthController extends Controller
{
    public function showRegistrationForm()
    {
        return view('auth.register');
    }

    public function register(Request $request)
    {
        $request->validate(['email' => 'required|email']);

        $user = User::where('email', $request->email)->first();

        if ($user) {
            if ($user->password) {
                // User exists and has a password, show password field
                return view('auth.login', ['email' => $request->email]);
            } else {
                // User exists but has no password, show verify email button
                return redirect()->route('verify-email')->with('email', $request->email);
            }
        } else {
            // No user found, redirect to registration
            return redirect()->route('register');
        }
    }

    public function showVerifyEmailForm()
    {
        return view('auth.verify-email', ['email' => session('email')]);
    }

    public function verifyEmail(Request $request)
    {
        // Implement your email verification logic here
        // After verification, redirect to create password form
        $user = User::where('email', session('email'))->firstOrFail();
        return redirect()->route('create-password', ['user' => $user]);
    }

    public function showCreatePasswordForm(User $user)
    {
        return view('auth.create-password', ['user' => $user]);
    }

    public function createPassword(Request $request)
    {
        $request->validate([
            'password' => 'required|confirmed|min:6',
        ]);

        $user = User::findOrFail($request->user_id);
        $user->password = Hash::make($request->password);
        $user->save();

        Auth::login($user);

        return redirect('/profile');
    }
}

Views:

  • auth.register should contain a form that asks for the email.
  • auth.login should contain a form that asks for the email (pre-filled and read-only) and password.
  • auth.verify-email should contain a button to trigger email verification.
  • auth.create-password should contain a form that allows the user to create a password.

Remember to replace the placeholders with actual views and implement the email verification logic according to your application's requirements. This is a simplified example to guide you through the process. You'll need to flesh out the details based on your specific needs and implement proper security measures, such as CSRF protection and rate limiting.

jcoder02k's avatar

@LaryAI how exactly i will implement this?

Views:

auth.register should contain a form that asks for the email.
auth.login should contain a form that asks for the email (pre-filled and read-only) and password.
auth.verify-email should contain a button to trigger email verification.
auth.create-password should contain a form that allows the user to create a password.

Remember to replace the placeholders with actual views and implement the email verification logic according to your application's requirements. This is a simplified example to guide you through the process. You'll need to flesh out the details based on your specific needs and implement proper security measures, such as CSRF protection and rate limiting.

aurawindsurfing's avatar

@jcoder02k I have build many apps and I never had to do this. I get what you try to do but it is quite complex thing to do.

If you however want to try to achive it in filament it looks to me like like a wizard form: https://filamentphp.com/docs/3.x/forms/layout/wizard#overview

If not then think of it as a separate forms on a blade page. So you have you first route where you take your email. Then you process that email in your controller Then you have another page that displays confirmation and so on.

When you figure that out then you could possibly move to Livewire Page components and have more interactivity within the forms. But to do that you first need to understand the whole process step by step in traditional route -> view -> controller flow

Then if you really feel like it you can build your own registration in filament which is blade + livewire.

Having said all of the above I would still go with traditional registration as it simply works and its is 100% accessible for users.

Hope it helps!

jcoder02k's avatar

@aurawindsurfing thank you for the info. I don't want to use a multi step form that show different steps basically i just want to display email field first then user validate it. If exists but no password yet need to show the Verify email button so user can verify it and redirect to Profile page and create password. Next if user enter email and exist with password it will just show the Sign button. Last if email don't exist it will redirect to Registration form.

martinbean's avatar

@jcoder02k So maybe attempt something first, and then ask a question when you actually get stuck? Because if you haven’t tried anything already, then you’re essentially just asking us to give you the code, or step-by-step instructions.

Please or to participate in this conversation.