Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel login and register forms on the same page in Laravel 5.2

Login and Register forms on the same page

From default auth and register sistem that comes with Laravel 5.2, Just made a simple view, that could be the welcome page, and to include the login and register forms. At the moment, if any button is pressed, the validation checks and finds errors from both forms, and are highlighted all of them which are required. So what is the best solution to include them and maybe a separate validation for each?

I tried to change the default login sistem to be different from the register form input names, but i think there is a chain, due to validation and adding input values into database.

<div class="form-group{{ $errors->has('login_email') ? ' has-error' : '' }}">
    <label class="col-md-4 control-label">E-Mail Address</label>
    <div class="col-md-6">
         <input type="email" class="form-control" name="login_email" value="{{ old('login_email') }}">
         @if ($errors->has('login_email'))
              <span class="help-block">
                   <strong>{{ $errors->first('login_email') }}</strong>
              </span>
         @endif
     </div>
</div>

<div class="form-group{{ $errors->has('login_password') ? ' has-error' : '' }}">
     <label class="col-md-4 control-label">Password</label>
          <div class="col-md-6">
                <input type="password" class="form-control" name="login_password">
                @if ($errors->has('login_password'))
                     <span class="help-block">
                          <strong>{{ $errors->first('login_password') }}</strong>
                     </span>
               @endif
          </div>
      </div>

As it can be seen, just changed the email and password input names from 'email' to 'login_email' and from 'password' to 'login_password'

Any quick and efficient solutions/ideas are welcome

like image 745
Spidey Avatar asked Dec 18 '22 20:12

Spidey


2 Answers

Instead of changing input names, just Override trait function and call it from the overriden function...

With this done, we can store a session value that tell us from where the auth attempt is coming from login or register form!

    use AuthenticatesUsers, RegistersUsers {
    AuthenticatesUsers::redirectPath insteadof RegistersUsers;
    AuthenticatesUsers::getGuard insteadof RegistersUsers;
    login as traitLogin;
    register as traitRegister;
}

// Override trait function and call it from the overriden function
public function login(Request $request)
{
    //Set session as 'login'
    Session::put('last_auth_attempt', 'login');
    //The trait is not a class. You can't access its members directly.
    return $this->traitLogin($request);
}


public function register(Request $request)
{
    //Set session as 'register'
    Session::put('last_auth_attempt', 'register');
    //The trait is not a class. You can't access its members directly.
    return $this->traitRegister($request);
}

and in your View.blade file just check your errors with your Session value ...

<div class="form-group{{ $errors->has('email') && Session::get('last_auth_attempt') == 'login' ? ' has-error' : '' }}">
    <label class="col-md-4 control-label">E-Mail</label>

    <div class="col-md-6">
        <input type="email" class="form-control" name="email" value="{{ old('email') }}">

        @if ($errors->has('email') && Session::get('last_auth_attempt') == 'login')
            <span class="help-block">
                <strong>{{ $errors->first('email') }}</strong>
            </span>
        @endif
    </div>
</div>

With this you can check if button pressed is from login ou register button

like image 122
Fabiano Albernaz Avatar answered Dec 28 '22 16:12

Fabiano Albernaz


Another quick but maybe not that elegant solution would be inserting a hidden field into the login-form

<input type="hidden" name="loginform" value="1">

and then just check it with email- and password-errors in both forms.

At the login-form something like:

@if($errors->has('password') && !old('loginform'))

And at the registration-form something like:

@if($errors->has('password') && old('loginform'))

No server-side code needed. Laravel version 5.*

like image 33
loau Avatar answered Dec 28 '22 17:12

loau