Php 如何使用laravell api在android应用程序中将登录从电子邮件更改为手机号码?
是我的Android代码登录请求从这里转到指定的URL并收回响应,但当我尝试使用手机号码而不是电子邮件登录时,响应总是错误的(用户凭据不正确)Php 如何使用laravell api在android应用程序中将登录从电子邮件更改为手机号码?,php,android,laravel,authentication,mvvm,Php,Android,Laravel,Authentication,Mvvm,是我的Android代码登录请求从这里转到指定的URL并收回响应,但当我尝试使用手机号码而不是电子邮件登录时,响应总是错误的(用户凭据不正确) HashMap<String, Object> map = new HashMap<>(); map.put("grant_type", "password"); map.put("username", mobile);
HashMap<String, Object> map = new HashMap<>();
map.put("grant_type", "password");
map.put("username", mobile);
map.put("password", password.getText().toString());
map.put("client_secret", BuildConfig.CLIENT_SECRET);
map.put("client_id", BuildConfig.CLIENT_ID);
map.put("device_token", SharedHelper.getKey(PasswordActivity.this, "device_token", "No device"));
map.put("device_id", SharedHelper.getKey(PasswordActivity.this, "device_id", "123"));
map.put("device_type", BuildConfig.DEVICE_TYPE);
laravel的
LoginController
使用Lightning\Foundation\Auth\AuthenticatesUsers
特性,该特性具有用户名
方法,您可以在控制器中重写它,并将其返回值从电子邮件更改为您想要使用的任何内容。我是否也应该调用AuthenticateUsers中的登录函数,还是只在LoginController中重写username()函数?
public function login(Request $request)
{
$tokenRequest = $request->create('/oauth/token', 'POST', $request->all());
$request->request->add([
"client_id" => $request->client_id,
"client_secret" => $request->client_secret,
"grant_type" => $request->grant_type,
"code" => '*',
]);
$response = Route::dispatch($tokenRequest);
$json = (array)json_decode($response->getContent());
if (!empty($json['error'])) {
$json['error'] = $json['message'];
}
if (empty($json['error'])) {
if (Auth::attempt(['mobile' => $request->username, 'password' => $request->password])) {
$user = Auth::user()->id;
if ($user) {
$accessTokens = DB::table('oauth_access_tokens')->where('user_id', $user->id)->orderBy('created_at', 'desc')->get();
$t = 1;
foreach ($accessTokens as $accessToken) {
if ($t != 1) {
DB::table('oauth_refresh_tokens')->where('access_token_id', $accessToken->id)->delete();
DB::table('oauth_access_tokens')->where('id', $accessToken->id)->delete();
}
$t++;
}
}
}
}
$update = User::where('mobile', $request->username)->update(['device_token' => $request->device_token, 'device_id' => $request->device_id, 'device_type' => $request->device_type]);
$response->setContent(json_encode($json));
// return response()->json($json);
return $response;
}