Home > Blockchain >  I made a login system but it is saying wrong login details and unable to login
I made a login system but it is saying wrong login details and unable to login

Time:05-30

I want to create simple login system and I made a login system, but it is saying wrong login details. I tried doing dump test is returning null. Please see dd test in comment. Unable to logout also due to this reason

My Login Controller is:

<?php

namespace App\Http\Controllers;

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

class LoginController extends Controller
{
    public function index()
    {
        return view('login');
    }

    
    public function store(Request $request)
    {
        $this->validate($request, [
            'email'   => 'required|email',
            'password'  => 'required|alphaNum|min:3'
                ]);

        $user_data = array(
            'email' => $request->get('email'),
            'password' => $request->get('password')
        );
        
        //$user = Auth::user();
        //dd($user);              I have done dump test but it is returning null
        
        if(Auth::attempt($user_data))
        {
            return redirect()->route('welcome');
        }
        else
        {
            return back()->with('error','wrong Login Details');
        }
    } 
}

My Login Blade is

          @if ($message = Session::get('error'))
                <div >
                    <button type="button"  data-dismiss="alert">×</button>
                        <strong>{{ $message }}</strong>
                </div>
          @endif
           @if (count($errors) > 0)
            <div >
                    <ul>
                            @foreach($errors->all() as $error)
                                <li>{{ $error }}</li>
                            @endforeach
                    </ul>
            </div>
           @endif
      
    <form action="{{route('login.save')}}" style="border:1px solid #ccc" method="post">
                {{ csrf_field() }}
        <div >
            <h1>Login</h1>
            <p>Please fill in this form to Login</p>
            <hr>
            <label for="email"><b>Email</b></label>
            <input type="text" placeholder="Enter Email" name="email" value="{{old('name')}}" >
                @error('email')
                  {{$message}}
                @enderror  

            <label for="psw"><b>Password</b></label>
            <input type="password" placeholder="Enter Password" name="password"  >
                @error('password')
                  {{$message}}
                @enderror  

            <label for="psw-repeat"><b>Repeat Password</b></label>
            <input type="password" placeholder="Repeat Password" name="psw-repeat" >
            <div >
                <button type="submit" >Log in</button>
            </div>
        </div>
    </form>
</body>
</html>

Web route is

Route::get('/register','App\Http\Controllers\RegisterController@index')->name('register');
Route::post('/register/save','App\Http\Controllers\RegisterController@store')->name('register.save');
Route::get('/login','App\Http\Controllers\LoginController@index')->name('login');
Route::post('/login/save','App\Http\Controllers\LoginController@store')->name('login.save');
Route::get('/welcome','App\Http\Controllers\LogoutController@store')->name('logout');

CodePudding user response:

try to debug your code

1- after register look at database. is your password encrypted ?

2- when submit form dd($request->all()) if every thing work fine

3- Auth::attempt($request->only('email', 'password'))

I hope this answer help you to debug your code.

CodePudding user response:

Try running a dd on the controller itself. Use

dd($request->all());

before

$this->validate($request, [
            'email'   => 'required|email',
            'password'  => 'required|alphaNum|min:3'
                ]);

As of right now, there is no indication that your input data is even reaching the controller. Your route may not even be correct, it is impossible to say without knowing your project structure or the names of the file. If the dd does not return those two inputs, you know that the route is not correct or a middleware is intercepting the data. Update this answer in the comments with more info after your testing is done. Also I am not sure why you are trying to dd Auth:user. Of course it will return nothing. You have not logged in yet. There IS no user for it to show. Unless you have done something like:

$users = Users::firstOrCreate($data);
                Auth::loginUsingId($users->id);
                $users->save();

You have not actually saved any user in your database or logged in. I think you need to find a better tutorial to do this from.

  • Related