Home > front end >  Login form is not directing to dashboard page after user logs in
Login form is not directing to dashboard page after user logs in

Time:06-15

I am trying to redirect logged in users to the main dashboard view, that I've titled as "xwelcome.blade.php" temporarily.

I've attached my routes:

Route::get('xwelcome', [CustomAuthController::class, 'xwelcome']); 

Route::get('login', [CustomAuthController::class, 'index'])->name('login');

Route::post('custom-login', [CustomAuthController::class, 'customLogin'])->name('login.custom'); 

Route::get('registration', [CustomAuthController::class, 'registration'])->name('register-user');

Route::post('custom-registration', [CustomAuthController::class, 'customRegistration'])->name('register.custom'); 

Route::get('logout', [CustomAuthController::class, 'logOut'])->name('logout');

This is my CustomAuthController:

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Hash;
use Session;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
class CustomAuthController extends Controller

{
    public function index()
    {
        return view('auth.login');
    }  
      
    public function customLogin(Request $request)
    {
        $request->validate([
            'email' => 'required',
            'password' => 'required',
        ]);
   
        $credentials = $request->only('email', 'password');
        if (Auth::attempt($credentials)) {
            return redirect()->intended('xwelcome')
                        ->withSuccess('Signed in');
        }
  
        return redirect("login")->withSuccess('Login details are not valid');
    }

    public function registration()
    {
        return view('auth.registration');
    }
      
    public function customRegistration(Request $request)
    {  
        $request->validate([
            'name' => 'required',
            'email' => 'required|email|unique:users',
            'password' => 'required|min:6',
        ]);
           
        $data = $request->all();
        $check = $this->create($data);
         
        return redirect("xwelcome")->withSuccess('You have signed-in');
    }

    public function create(array $data)
    {
      return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => Hash::make($data['password'])
      ]);
    }    
    
    public function dashboard()
    {
        if(Auth::check()){
            return view('xwelcome');
        }
  
        return redirect("login")->withSuccess('You are not allowed to access');
    }
    
    public function logOut() {
        Session::flush();
        Auth::logout();
  
        return Redirect('login');
    }
}

Login button:

<form role="form" method="post" action="{{ route('login.custom') }}">
                        @csrf
                      <div >
                      <div >
                        <label >Email address</label>
                        <div >
                          <div >
                            <span ><i ></i></span>
                          </div>
                          <input type="email"  id="input-email" placeholder="[email protected]">
                        </div>
                      </div>
                      <div >
                        <div >
                          <div>
                            <label >Password</label>
                          </div>
                          <div >
                            <a href="#!" >Lost password?</a>
                          </div>
                        </div>
                        <div >
                          <div >
                            <span ><i ></i></span>
                          </div>
                          <input type="password"  id="input-password" placeholder="Password">
                          <div >
                            <span >
                              <a href="#" data-toggle="password-text" data-target="#input-password">
                                <i ></i>
                              </a>
                            </span>
                          </div>
                        </div>
                      </div>
                        <button type="submit" >
                         <span ><i ></i></span>
                        Sign In
                        </button></div>
                    </form>

After I fill in my test account details, and press the button, I see this in my address bar

https://example.com/login?_token=0GddRaD453thhRFL7KWgEQ5DnZ3q7B4rL5gaajuM

Any help would be appreciated. Cheers

CodePudding user response:

I can't see the input fields in your form!.

Have you tried to display the errors after validation in the form?.

I think that you are redirected back to :

https://example.com/login?_token=0GddRaD453thhRFL7KWgEQ5DnZ3q7B4rL5gaajuM

because you are missing the input:email and input:password, what your are seeing is only the csrf token that laravel generates.

CodePudding user response:

i Think issue is on button . try this out

<form role="form" method="post" action="{{ route('login.custom') }}">
                        @csrf
                      <div >
<button type="submit" >
                        
          <span ><i ></i></span>
                        Sign In
                        </button></div>
                    </form>
  • Related