简体   繁体   中英

Laravel 5.7 Custom Login Not Working - New Installation

I have a new installation of laravel 5.7 I have created the database and users table with dummy info manually and connected the database with the application.

Can anyone please point out why the following code isn't working.

login.blade.php

        <div class="form box">
           <h1 align="center">Login - MobileInfo</h1>
           <br />

           @if(isset(Auth::user()->username))
                <script>window.location="main/success"</script>
           @endif

           @if ($message = Session::get('error'))
           <div class="alert alert-danger alert-block">
               <button type="button" class="close" data-dismiss="alert">x</button>
               <strong>{{ $message }}</strong>
           </div>
           @endif

           @if (count($errors) > 0)
           <div class="alert alert-danger">
               <ul>
                   @foreach($errors->all() as $error)
                   <li>{{ $error }}</li>
                   @endforeach
               </ul>
           </div>

           @endif
            <form method="post" action="{{ url('login/checklogin') }}">
                {{ csrf_field()}}
                <div class="form-group">
                    <label>Username</label>
                    <input type="text" name="username" class="form-control" />
                </div>
                <div class="form-group">
                    <label>Password</label>
                    <input type="password" name="password" class="form-control" />
                </div>
                 <div class="form-group">
                    <input type="submit" name="login" class="btn btn-primary" value="Login" />
                </div>
            </form>

        </div>
    </div>

Login Controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Validator;
use Auth;

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

function checklogin(Request $request)
{
    $this->validate($request, [
        'username' => 'required',
        'password' => 'required'
    ]);

    $user_data = array(
        'username' => $request->get('username'),
        'password' => $request->get('password')
    );

    if(Auth::attempt($user_data))
    {
        return redirect('login/success');
    }
    else
    {
        return back()->with('error', 'Wrong Login Details');
    }
}

function success()
{
    return view('dashboard');
}

function logout()
{
    Auth::logout();
    return redirect('login');
}
}

web.php

Route::get('/', 'LoginController@index');
Route::post('login/checklogin', 'LoginController@checklogin');
Route::get('login/success', 'LoginController@success');
Route::get('login/logout', 'LoginController@logout');

The error I see after adding any of the correct combinations of username/password is the else block of Auth::attempt($user_data), that is "Wrong Login Details".

By default, Laravel auth provide authentification with email, so to use another field to login, you may define it in LoginController.

public function username()
{
    return 'username';
}

Documentation

Laravel store passwords using its hashing algorithm and then validate/compare with same hashing algorithm.

The problem is, you stored literal passwords in database, that's why laravel is unable to validate those passwords. Laravel is expecting hashed password from database.

Literal password won't work. So ...

#1

Best solution is, make a signup form as well (using laravel Auth) and insert demo data through that signup form. Laravel will store passwords after hashing it, and eventually successfully validating those passwords.

#2

Another(complex/tedious) solution is, find out laravel's hashing algorithm and hash passwords with a separate php script. Then store hashed passwords in database directly. You don't need signup form in this case because you are doing it manually.

Update for solution # 2

Use below code to hash your literal passwords in laravel 5.x.

$hashedpassword = bcrypt('plaintextpassword');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM