简体   繁体   English

Laravel 5.7 登录没有重定向或错误

[英]Laravel 5.7 Login with no Redirect or Errors

I'm new to Laravel so I started following lessons on Laracast then the guy who gave the lessons went to the authentication of a user and guessed etc. The register works fine and it goes into the databases with hashed passwords and it keeps that user that just registered login (and can do all the stuff a login user is supposed to be doing on the website).我是 Laravel 的新手,所以我开始学习 Laracast 的课程,然后上课的那个人去验证用户并猜测等。注册工作正常,它使用散列密码进入数据库,并让该用户保持刚刚注册登录(并且可以做登录用户应该在网站上做的所有事情)。 When I go to my login form and try to log in it doesn't redirect and give errors.当我转到我的登录表单并尝试登录时,它不会重定向并给出错误。 It just reloads even when user/password credentials are not correct.即使用户/密码凭据不正确,它也会重新加载。

It's needed to register how it saves the user into the database.需要注册它如何将用户保存到数据库中。

$user = User::create([
    'name' => request('name'),
    'email' => request('email'),
    'password' => bcrypt(request('password'))
]);

login form...登录表格...

<form method="POST" action="/login">
    {{ csrf_field() }}
    <div class="form-group">
        <label for="Email">Email:</label>
        <input type="email" class="form-control" id="Email" name="Email" required>
    </div>

    <div class="form-group">
        <label for="password">Password:</label>
        <input type="password" class="form-control" id="password" name="password" required>
    </div>

    <div class="form-group">
        <button type="submit" class="btn btn-primary">Sign in</button>
    </div>
    @include ('layouts.errors')
</form>

web.php for routing用于路由的 web.php

 Route::get('/', 'PostsController@index')->name('home');
 Route::get('/posts/create', 'PostsController@create');
 Route::post('/posts', 'PostsController@store');
 Route::get('/posts/{post}', 'PostsController@show');

 Route::post('/posts/{post}/comments', 'CommentsController@store');

 Route::get('/register', 'RegistrationController@create');
 Route::post('/register', 'RegistrationController@store');
 Route::get('/login', 'SessionsController@create')->name('login');
 Route::post('/login', 'SessionsController@store');
 Route::get('/logout', 'SessionsController@destroy');

The controller that handles the login处理登录的控制器

<?php

class SessionsController extends Controller
{
    public function __construct()
    {
        $this->middleware('guest');
    }

    public function create()
    {
        return view('sessions.create');
    }

    public function store()
    {
        // Attempt to authenticate the user.
        if (!auth()->attempt(request(['email', 'password']))) {
            return back();
        }
        // Redirect them to the homepage
        return redirect()->home();
    }

    public function destroy()
    {
        auth()->logout();
        return redirect()->home();
    }
}

Errors.php错误.php

<div class="form-group">
  <div class="alert alert-errors">
        <ul>
            @foreach($errors->all() as $error)
                <li> {{ $error }} </li>
            @endforeach
        </ul>
    </div>
</div>

Have a look at your form.看看你的表格。 The names of the inputs are the one that are needed in the controller to access them.输入的名称是控制器中访问它们所需的名称。

In the form you declared Email and password .在您声明的表单中Emailpassword

 ...
<input type="email" class="form-control" id="Email" name="Email" required>
....
<input type="password" class="form-control" id="password" name="password" required>
...

In the Controller you try to access email and password .在 Controller 中,您尝试访问emailpassword

if (! auth()->attempt(request(['email', 'password']))) {

This might be the reason why the login fails and you just got redirected to the Page, because the executed code is这可能是登录失败并且您刚刚被重定向到页面的原因,因为执行的代码是

return back(); 

So change your form to所以改变你的形式

     ....

    <div class="form-group">

        <label for="Email">Email:</label>

        <input type="email" class="form-control" id="email" name="email" required>

    </div>
    .....

Let me know if this is working.让我知道这是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM