Auth :: attempt在Laravel 5.8中不起作用

时间:2019-09-15 05:15:48

标签: php laravel-5.8

大家好,我对Auth :: attempt有疑问 我尝试创建自己的登录路线,并且在测试功能auth :: attempt时似乎无法正常工作 这是我的代码 我的路线web.php文件:

use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('frontend.index');
});


Auth::routes();
// Route::get('login','Auth\LoginController@showLoginForm')->name('login');
Route::get('index','Frontend\HomeController@index')->name('index');
Route::get('logout', 'Auth\LoginController@logout')->name('logoutt');
Route::post('login', 'Auth\LoginController@authenticate')->name('loginn');




Route::get('/home', 'Backend\HomeController@index')->name('home');

我的login.blade.php

<form method="POST" action="{{ route('loginn') }}">
            @csrf
            <div class="login-form">
                <div class="form-group">
                    <label for="username" class="placeholder"><b>email</b></label>
                    <input id="login" type="text" class="form-control{{ $errors->has('username') || $errors->has('email') ? ' is-invalid' : '' }}" name="email" value="{{ old('username') ?: old('email') }}" required autofocus>                
                    @if ($errors->has('username') || $errors->has('email'))
                    <span class="invalid-feedback">
                        <strong>{{ $errors->first('username') ?: $errors->first('email') }}</strong>
                    </span>
                    @endif
                </div>
                <div class="form-group">
                    <label for="password" class="placeholder"><b>password</b></label>   
                    <div class="position-relative">
                        <input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password" required autocomplete="current-password">                        
                        @error('password')
                        <span class="invalid-feedback" role="alert">
                            <strong>{{ $message }}</strong>
                        </span>
                        @enderror
                        <div class="show-password">
                            <i class="flaticon-interface"></i>
                        </div>                          
                        @if (Route::has('password.request'))
                        <a href="{{ route('password.request') }}" class="link float-right">forget password?</a>   
                        @endif
                    </div>                 
                </div>               
                <div class="form-group">
                    <hr>
                    <label><b>Or sign in with</b></label>                   
                    <div class="row">                       
                        <div class="col-6">
                            <button class="btn btn-primary">
                                <span class="btn-label">
                                    <i class="fab fa-facebook"></i>
                                </span>
                                facebook
                            </button>                                 
                        </div>                      
                        <div class="col-6">
                            <button class="btn btn-danger">
                                <span class="btn-label">
                                    <i class="fab fa-google"></i>
                                </span>
                                google
                            </button>
                        </div>

                    </div>
                    <hr>
                </div>               
                <div class="form-group form-action-d-flex mb-3">
                    <div class="custom-control custom-checkbox">
                        <input type="checkbox" class="custom-control-input" id="rememberme" {{ old('remember') ? 'checked' : '' }}>
                        <label class="custom-control-label m-0" for="rememberme">Remember Me</label>
                    </div>
                    <button type="submit" class="btn btn-primary col-md-5 float-right mt-3 mt-sm-0 fw-bold">
                        Sign In
                    </button>
                </div>
                <!--                <div class="form-action">
                    <a href="#" class="btn btn-primary btn-rounded btn-login">Sign In</a>
                </div> -->
                {{-- {{route('register')}} --}}
                <div class="login-account">
                    <span class="msg">Don't have an account yet ?</span>
                    <a href="{{route('register')}}" id="show-signup" class="link">Sign Up</a>
                </div>
            </div>
        </form>

和我的loginController.php

    <?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Http\Middleware\Authenticate;
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');
        $this->username = $this->findUsername();
    }
    public function index()
    {
        return view('frontend.index');
    }
    public function findUsername()
    {
        $login = request()->input('login');

        $fieldType = filter_var($login, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';

        request()->merge([$fieldType => $login]);

        return $fieldType;
    }

    public function authenticate(Request $request)
    {

        // $email = $request->input('login');
        // $username = $request->input('login');
        // $password = $request->input('password');
        $credentials = $request->only('email', 'password');
        // dd($credentials);
        // dd(Auth::attempt($credentials));
        if (Auth::attempt($credentials)) {
            // Authentication passed...
            return redirect()->intended('home');
        }else{
            echo '123';
        }
    }

尽管我已经尝试了许多方法,但看来auth :: try函数无法验证密码 这是我的userFactory.php,我也尝试使用hash :: make('')和bcrypt()仍然无法正常工作,它总是返回“ 123”,就像我在authenticate()函数中设置的一样。

<?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */
use App\Models\User;
use Illuminate\Support\Str;
use Faker\Generator as Faker;
use Illuminate\Support\Facades\Hash;

$factory->define(User::class, function (Faker $faker) {
    return [
        'username' => $faker->domainWord,
        'email'=>$faker->freeEmail,
        'password' => Hash::make('123456'), // password
        'role' =>$faker->numberBetween($min = 0, $max = 1),
        'status'=>$faker->numberBetween($min =0, $max=1),
        'remember_token' => Str::random(10),
    ];
});

0 个答案:

没有答案