duanguzhong5776 2015-12-14 10:36
浏览 45

Auth:attempt()不工作laravel 5

My Auth::attempt() function is not working it always redirects me to the logging page even if the email and the password are correct.

Here is my controller:

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');
    }
}

And here is my signing in form:

@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条回答 默认 最新

  • dongpi9164 2015-12-14 10:41
    关注
    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');
                }
            }
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大