我想在我的Android应用程序中实现OTP功能。在此应用程序中,输入电子邮件ID后,检查用户是否存在。如果用户电子邮件ID不存在,用户将收到一次性密码密钥(OTP)。收到OTP后,用户将能够登录应用程序。我需要做些什么来实现我的应用程序?
答案 0 :(得分:0)
有很多方法可以解决这个问题,但我们建议使用OTP进行完整的电子邮件验证流程验证还可以提供其他一些身份验证技术,如果你想要跟随的是官方网址,可以帮助你。 Firebase Auth System
另一种可以使用自定义机制的方法。 自定义机制步骤: 在输入电子邮件后,将静态电子邮件和密码放入代码中,将OTP发送到输入的邮件,并在尝试验证比较时通过电子邮件将OTP存储在您的数据库中。此外,保持一些时间到期或其他。
希望这对你有帮助!
答案 1 :(得分:0)
试试这段代码:
添加java类
//This method would confirm the otp
private void confirmOtp() throws JSONException {
//Creating a LayoutInflater object for the dialog box
LayoutInflater li = LayoutInflater.from(this);
//Creating a view to get the dialog box
View confirmDialog = li.inflate(R.layout.dialog_confirm, null);
//Initizliaing confirm button fo dialog box and edittext of dialog box
buttonConfirm = (AppCompatButton) confirmDialog.findViewById(R.id.buttonConfirm);
editTextConfirmOtp = (EditText) confirmDialog.findViewById(R.id.editTextOtp);
//Creating an alertdialog builder
AlertDialog.Builder alert = new AlertDialog.Builder(this);
//Adding our dialog box to the view of alert dialog
alert.setView(confirmDialog);
//Creating an alert dialog
final AlertDialog alertDialog = alert.create();
//Displaying the alert dialog
alertDialog.show();
//On the click of the confirm button from alert dialog
buttonConfirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Hiding the alert dialog
alertDialog.dismiss();
//Displaying a progressbar
final ProgressDialog loading = ProgressDialog.show(MainActivity.this, "Authenticating", "Please wait while we check the entered code", false,false);
//Getting the user entered otp from edittext
final String otp = editTextConfirmOtp.getText().toString().trim();
//Creating an string request
StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.CONFIRM_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//if the server response is success
if(response.equalsIgnoreCase("success")){
//dismissing the progressbar
loading.dismiss();
//Starting a new activity
startActivity(new Intent(MainActivity.this, Success.class));
}else{
//Displaying a toast if the otp entered is wrong
Toast.makeText(MainActivity.this,"Wrong OTP Please Try Again",Toast.LENGTH_LONG).show();
try {
//Asking user to enter otp again
confirmOtp();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
alertDialog.dismiss();
Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
//Adding the parameters otp and username
params.put(Config.KEY_OTP, otp);
params.put(Config.KEY_USERNAME, username);
return params;
}
};
//Adding the request to the queue
requestQueue.add(stringRequest);
}
});
}
添加其他API类
public class Config {
//URLs to register.php and confirm.php file
public static final String REGISTER_URL = "http://simplifiedcoding.16mb.com/AndroidOTP/register.php";
public static final String CONFIRM_URL = "http://simplifiedcoding.16mb.com/AndroidOTP/confirm.php";
//Keys to send username, password, phone and otp
public static final String KEY_USERNAME = "username";
public static final String KEY_PASSWORD = "password";
public static final String KEY_PHONE = "phone";
public static final String KEY_OTP = "otp";
//JSON Tag from response from server
public static final String TAG_RESPONSE= "ErrorMessage";
}
答案 2 :(得分:0)
您可以一步一步地完成。