I am making an android app using laravell api. Currently i am logging in to my app using email but i want to change it with phone number. Please help me Here is my android code ..
private void login() {
try {
if (password.getText().toString().isEmpty()) {
Toast.makeText(this, getString(R.string.invalid_password), Toast.LENGTH_SHORT).show();
return;
}
if (mobile.isEmpty()) {
Toast.makeText(this, getString(R.string.invalid_email), Toast.LENGTH_SHORT).show();
return;
}
if (SharedHelper.getKey(this, "device_token").isEmpty()) {
FirebaseInstanceId.getInstance().getInstanceId().addOnCompleteListener(task -> {
if (task.isSuccessful()) {
SharedHelper.putKey(this, "device_token", task.getResult().getToken());
Log.d("FCM_TOKEN", task.getResult().getToken());
} else Log.w("PasswordActivity", "getInstanceId failed", task.getException());
});
return;
}
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(this, "device_token", "No device"));
map.put("device_id", SharedHelper.getKey(this, "device_id", "123"));
map.put("device_type", BuildConfig.DEVICE_TYPE);
showLoading();
presenter.login(map);
} catch (Exception e) {
e.printStackTrace();
}
}
And here is my login function in my api file in laravell
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::guard("web")->attempt(['mobile' => $request->username, 'password' => $request->password])) {
$user = Auth::guard("web")->user();
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++;
}
}
}
}
// $json['status'] = true;
$response->setContent(json_encode($json));
$update = User::where('mobile', $request->username)->update(['device_token' => $request->device_token, 'device_id' => $request->device_id, 'device_type' => $request->device_type]);
return $response;
}
Here is my all code i just want to make login using phone number. Please help me
Source: Laravel