Auth:attempt()不工作laravel 5

时间:2015-12-14 10:36:21

标签: php laravel

我的Auth::attempt()功能无效,即使电子邮件和密码正确,也始终将我重定向到日志页面。

这是我的控制器:     

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Requests\UserRequest;
use App\Http\Controllers\Controller;
use App\User;
use Auth;

class UsersController extends Controller
{
    public function getNewaccount(){
        return view('users.newaccount');
    }

    public function postCreate(Request $request){
        $user=new User($request->all());
        $user->save();
        return redirect('users/signin')->with('message','Thank you for registering please sign in');
    }

    public function getSignin(){
        return view('users.signin');
    }

    public function postSignin(Request $request){
        if(Auth::attempt(array('email'=>$request->input('email'),'password'=>$request->input('password')))){
            return redirect('/')->with('message','Thanks for signing');
        }else{
            return redirect('users/signin')->with('message','Email/password are wrong');
        }
    }

    public function getSignout(){
        Auth::logout();
        return redirect('users/signin')->with('message','You have successfully logged out');
    }
}

这是我的签名形式:

@extends('layouts.main')

@section('content')

    <section id="signin-form">
        <h1>I have an account</h1>
        {!! Form::open(array('action'=>'UsersController@postSignin')) !!}
        <p>
            {!! Html::image('img/email.gif', 'Email Address') !!}
            {!! Form::text('email') !!}
        </p>
        <p>
            {!! Html::image('img/password.gif', 'Password') !!}
            {!! Form::password('password') !!}
        </p>

        {!! Form::submit('Sign In', array('class'=>'secondary-cart-btn')) !!}
        {!! Form::close() !!}
    </section><!-- end signin-form -->
    <section id="signup">
        <h2>I'm a new customer</h2>
        <h3>You can create an account in just a few simple steps.<br>
            Click below to begin.</h3>
        <a href='users/newaccount' class='default-btn'>Create New Account</a>

    </section><!--- end signup -->

@stop

1 个答案:

答案 0 :(得分:0)

public function postSignin(Request $request)
    {
 //if you require validation otherwise you can skip this step
        $vLogin = validator::make($request->all(), [
            'email' => 'required|email',
            'password' => 'required'
        ]);

        if ($vLogin->fails()) {
            return redirect()->back()
                ->withErrors($vLogin->errors())
                ->withInput(Input::except('password'));
        } else {
            /*
             *getting the email and password that user has typed in form
             */
            $loginData = array(
                'email' => Input::get('email'),
                'password' => Input::get('password')
            );

            /*
             * Checking against the record in database whether the email and password is valid
             * Or the record exists in the database
             */
            if (Auth::validate($loginData)) {
                if (Auth::attempt($loginData)) {
                    //return wherever you like
                    return Redirect::intended('dashboard');
                }
            } else {
                // if any error send back with message.
                Session::flash('error', 'Invalid Email/Password Combination');
                return Redirect::to('login');
            }
        }
    }