简体   繁体   English

Laravel 5 使用用户名而不是电子邮件登录

[英]Laravel 5 login with username instead of email

How can I make login with username, instead of email?如何使用用户名而不是电子邮件登录? 电子邮件

It's my login.blade.php这是我的 login.blade.php

@extends('layouts.main')
@section('errors')
@if (count($errors) > 0)
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif
@section('content')
<form method="POST" action="/auth/login">
    {!! csrf_field() !!}

    <div>
        Email
        <input type="email" name="email" value="{{ old('email') }}">
    </div>

    <div>
        Password
        <input type="password" name="password" id="password">
    </div>

    <div>
        <input type="checkbox" name="remember"> Remember Me
    </div>

    <div>
        <button type="submit">Login</button>
    </div>
</form>
</body>
@stop

And the second quetsion: How to make password reset?第二个问题:如何重置密码? Thanks in advance ;)提前致谢 ;)

You change email to username in you form您在表单中将电子邮件更改为用户名

<form method="POST" action="/auth/login">
    {!! csrf_field() !!}

    <div>
        Email
        <input type="text" name="username" value="{{ old('username') }}">
    </div>

    <div>
        Password
        <input type="password" name="password" id="password">
    </div>

    <div>
        <input type="checkbox" name="remember"> Remember Me
    </div>

    <div>
        <button type="submit">Login</button>
    </div>
</form>

and in you AuthenticatesAndRegisterUsers.php you change email to username also.在您 AuthenticatesAndRegisterUsers.php 中,您也将电子邮件更改为用户名。

public function postLogin(Request $request)
    {
        $this->validate($request, [
            'username' => 'required', 'password' => 'required',
        ]);

        $credentials = $request->only('username', 'password');

        if ($this->auth->attempt($credentials, $request->has('remember')))
        {
            return redirect()->intended($this->redirectPath());
        }

        return redirect($this->loginPath())
                    ->withInput($request->only('username', 'remember'))
                    ->withErrors([
                        'username' => $this->getFailedLoginMessage(),
                    ]);
    }

By default, Laravel uses the email field for authentication .默认情况下, Laravel 使用 email 字段进行身份验证 If you would like to customize this, you may define a username method on your LoginController:如果你想自定义这个,你可以在你的 LoginController 上定义一个 username 方法:

public function username()
{
    return 'username';
}

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

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