1
votes

First i created the Admin model in which i implemented it using Authenticable Admin.php

<?php

    namespace App;

    use Illuminate\Database\Eloquent\Model;

    use Illuminate\Auth\Authenticatable;

    class Admin extends Model implements \Illuminate\Contracts\Auth\Authenticatable
    {
        //
        use Authenticatable;
    }

then i created the migration :

    use Illuminate\Support\Facades\Schema;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;

    class CreateAdminsTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::create('admins', function (Blueprint $table) {
                $table->increments('id');
                $table->timestamps();
                $table->rememberToken();
                $table->string('email');
                $table->string('password');
            });
        }

        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::drop('admins');
        }
    }

Replaced the Users model in config/auth.php with Admin Model :

'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Admin::class,
        ],

AdminController.php

<?php

namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
use Auth;

class AdminController extends Controller{

    public function getIndex()
    {
        $posts= Post::orderBy('created_at','desc')->take(3)->get();
        return view('backend.index')->with(['posts'=>$posts]);
    }

    public function getLogin(){
        return view('backend.login');
    }

    public function postLogin(Request $request){
        $this->validate($request,[
            'email'=>'required|email',
            'password'=>'required'
        ]);

        dd(Auth::attempt(['email'=>$request['email'],'password'=>$request['password']]));

        if(!Auth::attempt(['email'=>$request['email'],'password'=>$request['password']])){
            return redirect()->back()->with(['fail'=>'Could Not Log You In']);
        }

        return redirect()->route('admin.index');
    }

    public function getLogout(){
        Auth::logout();
        return redirect()->route('blog.index');
    }

}

web.php

Route::get('/admin/login',[
    'uses'=>'AdminController@getLogin',
    'as'=>'admin.login'
]);

Route::post('/admin/login',[
    'uses'=>'AdminController@postLogin',
    'as'=>'admin.login'
]);

login.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Admin Area</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="{{URL::to('css/bootstrap.min.css')}}">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="{{URL::to('js/bootstrap.min.js')}}"></script>
    @yield('styles')
</head>
<body>
<div class="container">
    <div class="container-fluid">
        @include('includes.info-box')
        <form method="post" action="{{route('admin.login')}}">
            <div class="form-group">
                <label for="email">Email: </label>
                <input type="email" class="form-control" id="email" name="email"
                       {{$errors->has('email')?'class=alert alert-danger':'' }}
                       value="{{Request::old('email')}}">
            </div>
            <div class="form-group">
                <label for="password">Password: </label>
                <input type="password" class="form-control" id="password" name="password"
                       {{$errors->has('password')?'class=alert alert-danger':'' }}
                       >
            </div>
            <div class="form-group">
                <button type="submit" class="btn btn-default">Login</button>
                <input type="hidden" name="_token" value="{{Session::token()}}">
            </div>
        </form>
    </div>
</div>
</body>
</html>

so when i tried to login and dd the Auth Attempt it is returning false i have a user with email [email protected] and password test : enter image description here

created the user using seeder :

<?php

use Illuminate\Database\Seeder;

class AdminTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        //
        $admin = new \App\Admin;
        $admin->email='[email protected]';
        $admin->password='[email protected]';
        $admin->save();
    }
}
1
did you encrypt the password using bcrypt?Wreigh
no because the password is simple textNihal Saxena
Auth attempt bcrypts the entered password, so if you registered the user and did not used bcrypt to encrypt the password, it will fail. try $user->password = bcrypt('test');Wreigh
where should i use itNihal Saxena
when registering your user, or you can change your user's password in tinker: $u = App\Admin::first(); $u->password = bcrypt('test'); $u->save()Wreigh

1 Answers

7
votes

So for your seeder.

$admin = new \App\Admin;
$admin->email='[email protected]';
$admin->password='[email protected]';
$admin->save();

the password should be hashed using bcrypt.

$admin->password = bcrypt('[email protected]');

but as you've said, you're trying 'test' as the password, so it should be:

$admin->password = bcrypt('test');

that should work.

the reason is, Auth::attempt hashes the entered password using bcrypt. So if the stored password in your database is not bcrypt-ed, Auth::attempt will fail to match the two passwords.

For example you put test as the password, plain text, if you try to login to laravel using Auth::attempt using password = 'test', string 'test' will be compared against the bcrpyt version of 'test'.