AngularJS OTP Verification With Third Party Authentication

AngularJS OTP Verification With Third Party User Authentication.

Kindly see the following steps.

Step1

https://msg91.com/ just follow this url create signup your account and get API and authentication key.

Step2

Just get API URL and call your MVC page Controller.

For example, the API gets SendAPI, VerifyAPI. These both are called in your MVC Controller page.

Step3

Create one cshtml page and refer the below code to set time out and validate OTP input and submit button also.

  1. //Design code  
  2. <form data-toggle="md-validator">  
  3.     <div id="Checkemail" class="md-form-group md-label-floating"> <input class="md-form-control" type="text" name="email" maximum="10" ng-model="Mobilenumber"> <label class="md-control-label">Enter your MobileNumber</label> </div>  
  4.     <div class="toast-message">  
  5.         <p style="text-align:center;color:red;"></p>  
  6.     </div>  
  7.     <div class="md-form-group"> <button type="submit" ng-keyup="$event.keyCode == 13" class=" btn btn-primary btn-block" ng-click="Submit()">Submit</button> </div>  
  8. </form>  

Step 4

Go to Controller.js and Service.js page and follow the below step. Use Page loading time ng-init() function to get your phone number from database and send, and we have to assign GenerateOTP function and get your OTP.

  1. //Controller.js code  
  2. $scope.Submit = function() {  
  3.     var mobile = $scope.Mobilenumber;  
  4.     Service.GenerateOTP(mobile).then(function(p1) {  
  5.         var Messageshow = JSON.parse(p1.data);  
  6.         if (Messageshow == "OTP_SENT_SUCCESSFULLY") {  
  7.             $scope.countDown = 60;  
  8.             var timer = setInterval(function() {  
  9.                 $scope.countDown--;  
  10.                 if ($scope.countDown >= 0) {  
  11.                     $scope.$apply();  
  12.                 }  
  13.                 if ($scope.countDown == 0) {  
  14.                     $("#countid").hide();  
  15.                 }  
  16.             }, 1000);  
  17.         } else if (Messageshow == "OTP_EXPRID") {  
  18.             $("#countidresult").show();  
  19.             alert() p1.data;  
  20.         }  
  21.     });  
  22. }  

Here I mention the sucess and failure message and OTP verify time limit. I will set 60 seconds for here.

Step 5

Like country code only pass to Service page to MVC Controller page. See the below.

  1. //Service.js Code  
  2. this.GenerateOTP = function(mobile) {  
  3.     var request = $http({  
  4.         method: "Get",  
  5.         url: "/SMS/GenerateOTP?mobile=" + mobile,  
  6.         dataType: "json"  
  7.     });  
  8.     return request;  
  9. }  

 

Step6

GoTo Controller folder right click add → new item ->select controller.cs file set name OTPController.

Send mobile number following this code.

  1. [HttpGet]  
  2. public JsonResult GenerateOTP(string mobile) {  
  3.     string CountryCode = "xx";  
  4.     string appKey = "xxxxxxxxxxxxxxx";  
  5.     var client = new RestClient("sms otp api url ");  
  6.     var request = new RestRequest(Method.POST);  
  7.     request.AddHeader("cache-control""no-cache");  
  8.     request.AddHeader("application-key", appKey);  
  9.     request.AddParameter("undefined""{\n \"countryCode\": " + countryCode + ",\n \"mobileNumber\": " + mobile + ",\n \"getGeneratedOTP\": true\n}", ParameterType.RequestBody);  
  10.     IRestResponse response = client.Execute(request);  
  11.     var newResource = JsonConvert.DeserializeObject < OTPVerification > (response.Content);  
  12.     if (newResource.response.code == "OTP_SENT_SUCCESSFULLY") {  
  13.         return Json(newResource.response.code, JsonRequestBehavior.AllowGet);  
  14.     } else if (newResource.response.code == "OTP_EXPRID") {  
  15.         return Json(newResource.response.code, JsonRequestBehavior.AllowGet);  
  16.     } else {  
  17.         return Json(newResource.status, JsonRequestBehavior.AllowGet);  
  18.     }  
  19. }  

Reponce Code status.

We post data on this URL as delivery report. We get HTTP response code 200 ok it's successfully been returned. In case the other code was returned it means 500 for internal error.504 for gateway timeout. So we can try after some period of time. If you have success it means show this message OTP_SENT_SUCCESSFULLY .

Step7

The set time period to start in 60 seconds.

For Example

You didn't get OTP please click and resend via call to pickup call and verify code .

Step8

If the code was sent to OTPverifyCode interact Verification API.

If we enter the wrong code it means show error invalid otp:refer below image



Just follow Controller.js code.

  1. $scope.VerifyOTP = function() {  
  2.     var VerificationCode = $scope.VerificationCode;  
  3.     Service.OTPverifyCode(VerificationCode).then(function(p1) {  
  4.         var Messageshow = JSON.parse(p1.data);  
  5.         if (Messageshow == "NUMBER_VERIFIED_SUCCESSFULLY") {  
  6.             $scope.countDown = 0;  
  7.             $("#countidresult").hide();  
  8.             $("#countid").hide();  
  9.             alert(p1.data);  
  10.             RedirectSports(-1);  
  11.             // $window.location.href = "/Account/Login";  
  12.         } else if (Messageshow == "OTP_INVALID") {  
  13.             $("#countidresult").show();  
  14.             alert(p1.data);  
  15.         }  
  16.     });  
  17. }  

Step 9

Service.js code

  1. this.OTPverifyCode = function(verificationCode) {  
  2.     var request = $http({  
  3.         method: "Post",  
  4.         url: "/SMS/OTPverifyCode?verificationCode=" + verificationCode,  
  5.         dataType: "json"  
  6.     });  
  7.     return request;  
  8. }  

Step10

  1. [HttpPost]  
  2. public JsonResult OTPverifyCode(string verificationCode) {  
  3.     string countryCode = "xx";  
  4.     string appKey = "xxxxxxxxxxxxxxx";  
  5.     var client = new RestClient("sms otp api url");  
  6.     var request = new RestRequest(Method.POST);  
  7.     request.AddHeader("cache-control""no-cache");  
  8.     request.AddHeader("application-key", appKey);  
  9.     request.AddParameter("undefined""{\n \"countryCode\": " + countryCode + ",\n \"mobileNumber\": " + mobileNumber + ",\n \"oneTimePassword\": " + verificationCode + "\n}", ParameterType.RequestBody);  
  10.     IRestResponse response = client.Execute(request);  
  11.     var newResource = JsonConvert.DeserializeObject < OTPVerification > (response.Content);  
  12.     if (newResource.status == "success" && newResource.response.code == "NUMBER_VERIFIED_SUCCESSFULLY") {  
  13.         return Json(newResource.response.code, JsonRequestBehavior.AllowGet);  
  14.     } else if (newResource.response.code == "OTP_INVALID") {  
  15.         return Json(newResource.response.code, JsonRequestBehavior.AllowGet);  
  16.     } else {  
  17.         return Json(newResource.status, JsonRequestBehavior.AllowGet);  
  18.     }  
  19. }  

 

`

Finally, the process of creating an AngularJS OTP verification with third party authentication has been successfully completed.


Similar Articles