在Laravel中更改用户登录方法

f0brbegy  于 2023-04-07  发布在  其他
关注(0)|答案(5)|浏览(154)

我正在编辑一个使用模块的laravel项目。我想将登录字段从email更改为移动的。在登录模块中的用户登录控制器中,有以下代码:

$loggedIn = $this->auth->login(
    [
        'email' => $request->email,
        'password' => $request->password,
    ],
    (bool) $request->get('remember_me', false)
);

我想用户登录与移动的,所以我改变了代码:

$loggedIn = $this->auth->login (
    [
        'mobile' => $request->email,
        'password' => $request->password,
    ],
    (bool) $request->get('remember_me', false)
);

但当我使用这个修改后的代码,它并没有改变这意味着用户仍然可以登录通过输入电子邮件!没有移动的!

iq0todco

iq0todco1#

试试这样……

来源链接

if (Auth::attempt(['email' => $email, 'password' => $password], $remember)) {
        // The user is being remembered...
    }
vu8f3i0k

vu8f3i0k2#

LoginController中添加以下行:

/**
     * Get the login username to be used by the controller.
     *
     * @return string
     */
    public function username()
    {
        return 'mobile';
    }
holgip5t

holgip5t3#

public function __construct()
    {
        $this->middleware('guest')->except('logout');
        $this->mobile = $this->findMobile();
    }
    public function findMobile()
    {
        $login = request()->login;
        $fieldType = filter_var($login, FILTER_VALIDATE_EMAIL) ? 'email' : 'mobile';
        request()->merge([$fieldType => $login]);
        return $fieldType;
    }
    public function mobile()
    {
        return $this->mobile;
    }
    public function logout(Request $request) {
        Auth::logout();
        return redirect('/login');
    }

尝试使用此代码,您可以使用电子邮件或移动的登录

yftpprvb

yftpprvb4#

您可以使用自定义登录

$user = User::where('mobile', $request->email)->first();

if (Hash::check($request->password,$user->password)) {
    auth()->login($user, (bool) $request->get('remember_me', false));  // it will login that user 
}

参考链接https://laravel.com/docs/8.x/authentication#other-authentication-methods

注意:这样你就有更多的控制权

brgchamk

brgchamk5#

$customerInfo = array(“email”=〉$email,“password”=〉$password);

// $credentials = $request->only('email', 'password');
    if (Auth::attempt($customerInfo)) {
        return redirect()->intended('home')
                    ->withSuccess('You have Successfully loggedin');
    }

相关问题