skip to Main Content

enter image description here

Login is failed, the credencials email and password, is correct, but laravel dont acces to dashboard

The password reset is worng to, the email is correct and i try chance of credentials on myphpadmin but dont entry.

this is my code

<?php

namespace AppHttpControllersAuth;

use AppHttpControllersController;
use AppHttpRequestsAuthLoginRequest;
use AppProvidersRouteServiceProvider;
use IlluminateHttpRequest;
use IlluminateSupportFacadesAuth;

class AuthenticatedSessionController extends Controller
{
    /**
     * Display the login view.
     *
     * @return IlluminateViewView
     */
    public function create()
    {
        return view('auth.login');
    }

    /**
     * Handle an incoming authentication request.
     *
     * @param  AppHttpRequestsAuthLoginRequest  $request
     * @return IlluminateHttpRedirectResponse
     */
    public function store(LoginRequest $request)
    {
        $request->authenticate();

        $request->session()->regenerate();

        return redirect()->intended(RouteServiceProvider::HOME);
    }

    /**
     * Destroy an authenticated session.
     *
     * @param  IlluminateHttpRequest  $request
     * @return IlluminateHttpRedirectResponse
     */
    public function destroy(Request $request)
    {
        Auth::guard('web')->logout();

        $request->session()->invalidate();

        $request->session()->regenerateToken();

        return redirect('/');
    }
}

2

Answers


  1. If you’re mass assignment while attempting authentication make sure the form requested keys must same as Model’s $fillable keys.
    then try this:

         /**
         * Handle an incoming authentication request.
         *
         * @param  AppHttpRequestsAuthLoginRequest  $request
         * @return IlluminateHttpRedirectResponse
         */
        public function store(LoginRequest $request)
        {
            $credentials = $request->validated();
    
            if (Auth::attempt($credentials)) {
               return redirect()->intended(RouteServiceProvider::HOME);
            }
            return back()->with('message', 'The given credentials are not matched');
        }
    

    if you’re working with multi guards, try this:

        public function store(LoginRequest $request)
        {
            $credentials = $request->validated();
    
            if (Auth::guard('web')->attempt($credentials)) {
               return redirect()->intended(RouteServiceProvider::HOME);
            }
            return back()->with('message', 'The given credentials are not matched');
        }
    
    Login or Signup to reply.
  2. I have tested this example code from your and my implementation

    <?php
        
        namespace AppHttpControllersAuth;
        
        use AppHttpControllersController;
        use AppHttpRequestsAuthLoginRequest;
        use AppProvidersRouteServiceProvider;
        use IlluminateHttpRequest;
        use IlluminateSupportFacadesAuth;
        use IlluminateHttpRequest;
        use Hash;
        use Session;
        use AppModelsUser;
        
        class AuthenticatedSessionController extends Controller
        {
            /**
             * Display the login view.
             *
             * @return IlluminateViewView
             */
            public function create()
            {
                return view('auth.login');
            }
        
            /**
             * Handle an incoming authentication request.
             *
             * @param  AppHttpRequestsAuthLoginRequest  $request
             * @return IlluminateHttpRedirectResponse
             */
            public function store(LoginRequest $request)
            {
              $request->validate([
                'email' => 'required',
                'password' => 'required',
              ]);
        
            $credentials = $request->only('email', 'password');
            if (Auth::attempt($credentials)) {
                return redirect()->intended('dashboard')
                            ->withSuccess('Signed in');
            }
       
            return redirect("login")->withSuccess('Login details are not valid');
            }
        
            /**
             * Destroy an authenticated session.
             *
             * @param  IlluminateHttpRequest  $request
             * @return IlluminateHttpRedirectResponse
             */
            public function destroy(Request $request)
            {
               Session::flush();
               Auth::logout();
               return redirect('/');
            }
            /**
             * Registration page.
             *
             */
            public function registration()
            {
              return view('auth.registration');
            }
           
             /**
             * Store new user request handler
             *
             */
            public function storeUser(Request $request)
            {  
             $request->validate([
                'name' => 'required',
                'email' => 'required|email|unique:users',
                'password' => 'required|min:6',
             ]);
                
             $data = $request->all();
             $check = $this->create($data);
              
             return redirect("dashboard")->withSuccess('have signed-in');
            }
             /**
             * Store new user
             *
             */
            public function create(array $data)
            {
               return User::create([
                'name' => $data['name'],
                'email' => $data['email'],
                'password' => Hash::make($data['password'])
               ]);
            }    
         
            /**
             * Dashboard for user
             *
             */
            public function dash()
             {
               if(Auth::check()){
                return view('dashboard');
                }
       
               return redirect("login")->withSuccess('are not allowed to access');
             }
        }
    

    Please check from your project and check flash messages if password was wrong check password hashing correctly

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search