简体   繁体   中英

Laravel 5.4 Authentication

Im trying to create login form and its controller but when i try to login it doesnt work can any one help me i'm very new in laravel.

here is my form

<form action="/login" method="post">
        {{ csrf_field() }}

        <div class="form-group has-feedback">
            <input type="email" name="email" class="form-control" placeholder="Email">
            <span class="glyphicon glyphicon-envelope form-control-feedback"></span>
        </div>
        <div class="form-group has-feedback">
            <input type="password" name="password" class="form-control" placeholder="Password">
            <span class="glyphicon glyphicon-lock form-control-feedback"></span>
        </div>
        <div class="row">
            <div class="col-xs-7">
                <div class="checkbox">
                    <label>
                        <input type="checkbox"> Remember Me
                    </label>
                </div>
            </div>
            <!-- /.col -->
            <div class="col-xs-5">
                <button type="submit" class="btn btn-primary btn-raised btn-block ">Sign In</button>
            </div>
            <!-- /.col -->
        </div>
    </form>

and here is my route

Route::get('/login', 'loginController@create');
Route::post('/login', 'loginController@store');

and my loginController is

class loginController extends Controller
{
    public function __construct(){
        $this->middleware('guest', ['except' => 'destroy']);
    }

   public function create(){

       return view('pages.admin.login');
   }


    public function store(){

       if(! auth()->attempt(request(['email', 'password']))){
            return back()->withErrors([
                'message' => 'Please check your credentials'
            ]);
        }

        return redirect('/home');
    }


}

My user modal is

    class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'fname','oname','lname', 'email', 'phone','password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

Please where i'm i doing wrong on these, when i entered email and password as my credential it just refresh and back to login page

auth()->attempt()需要一个数组,您将request()的返回值作为单个参数发送,是吗?

这是您的try函数应为的样子:

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

Your function should be like this with Request facade.

  public function store(Request $request){

   if(! auth()->attempt($request->only(['email', 'password']))){
        return back()->withErrors([
            'message' => 'Please check your credentials'
        ]);
    }

    return redirect('/home');
    }

Add these on top of LoginController

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

and modify your store method

public function store(Request $request){

    $email = $request->email;
    $password = $request->password;

    if (! Auth::attempt(['email' => $email, 'password' => $password])) {
        // Authentication Failed...
        return back()->withErrors([
            'message' => 'Please check your credentials'
        ]);
    }

    return redirect('/home');
}

Also remove password from $hidden in your User Model.

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'remember_token',
];

Hope it's helpful.

You can use php artisan make:auth and laravel will produce everything needed for a login including the controller and then you can go into your resource and edit to make it look how you imagined.

This is what my login controller looks like after using php artisan make:auth

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

class LoginController extends Controller
{


    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
}

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