这样实现用手机验证码登录(不用 ‘password’ 字段)
问题:通过下面的代码验证了短信验证码之后,通过Auth::login()
登录用户。 这样是否正确?
if (!hash_equals($session_data['code'], $request->phone_verification_code)) {
// 返回401
throw new AuthenticationException('验证码错误');
}else{
$user = User::Where('mobile_phone',$request->mobile_phone)->first();
Auth::login($user);
}
说明:如果用 Auth::attempt()
需要password
字段。最后只能想到上面的这个方法。
{
$credentials = $request->only('mobile_phone', 'password');
if (Auth::attempt($credentials)) {
// Authentication passed...
return redirect()->intended('dashboard');
}
谢谢各位
guard('web')
的是这样。Passport OAuth
也是差不多User::find(1)->createToken('phone')
guard('api')
直接改token
觉得不够场景,就自己定义一个
guard
,继承底层的的guard
(大部分情况都是够的)关于方法,这是写
guard
的人的事情。