|
|
@@ -0,0 +1,228 @@
|
|
|
+using Common;
|
|
|
+using Common.Extension;
|
|
|
+using Common.Http.Apis.Request;
|
|
|
+using Kitty_Fall.Website.Helpers;
|
|
|
+using Microsoft.AspNetCore.Mvc;
|
|
|
+using Newtonsoft.Json.Linq;
|
|
|
+
|
|
|
+namespace Kitty_Fall.Website.Controllers
|
|
|
+{
|
|
|
+ public class LoginController : Controller
|
|
|
+ {
|
|
|
+ private const string TestServiceName = "Kitty_Fall";
|
|
|
+ private const string PendingOtpMsisdnKey = "testLoginOtpMsisdn";
|
|
|
+ private const string PendingOtpTransactionKey = "testLoginOtpTransactionId";
|
|
|
+ private readonly IConfiguration _configuration;
|
|
|
+ private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(LoginController));
|
|
|
+
|
|
|
+ public LoginController(IConfiguration configuration)
|
|
|
+ {
|
|
|
+ _configuration = configuration;
|
|
|
+ }
|
|
|
+
|
|
|
+ [HttpGet]
|
|
|
+ public IActionResult Index(string? returnUrl = null)
|
|
|
+ {
|
|
|
+ var currentMsisdn = HttpContext.Session.GetComplexData<string>("msisdn");
|
|
|
+ if (!string.IsNullOrWhiteSpace(currentMsisdn))
|
|
|
+ {
|
|
|
+ return RedirectToAction("Index", "Home");
|
|
|
+ }
|
|
|
+
|
|
|
+ ViewBag.ReturnUrl = NormalizeReturnUrl(returnUrl);
|
|
|
+ ViewBag.CurrentMsisdn = currentMsisdn;
|
|
|
+ ViewBag.PendingOtpMsisdn = HttpContext.Session.GetComplexData<string>(PendingOtpMsisdnKey);
|
|
|
+ ViewBag.PendingOtpTransactionId = HttpContext.Session.GetComplexData<string>(PendingOtpTransactionKey);
|
|
|
+ return View();
|
|
|
+ }
|
|
|
+
|
|
|
+ [HttpPost]
|
|
|
+ public async Task<IActionResult> SendOtpAction(string msisdn)
|
|
|
+ {
|
|
|
+ if (string.IsNullOrWhiteSpace(msisdn))
|
|
|
+ {
|
|
|
+ return Json(new { errorCode = -1, message = "Please enter MSISDN." });
|
|
|
+ }
|
|
|
+
|
|
|
+ var baseUrl = GetParameter("url");
|
|
|
+ if (string.IsNullOrWhiteSpace(baseUrl))
|
|
|
+ {
|
|
|
+ return Json(new { errorCode = -1, message = "Missing API base URL." });
|
|
|
+ }
|
|
|
+
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var language = GetCurrentLanguage();
|
|
|
+ var res = await DotnetLib.Rest.RestHandler.SendPostAsync(
|
|
|
+ log,
|
|
|
+ baseUrl + UrlConstant.UserSendOtp,
|
|
|
+ new UserSendOtpReq
|
|
|
+ {
|
|
|
+ msisdn = msisdn.Trim(),
|
|
|
+ serviceName = TestServiceName,
|
|
|
+ language = language
|
|
|
+ },
|
|
|
+ language
|
|
|
+ );
|
|
|
+
|
|
|
+ if (res?.data == null)
|
|
|
+ {
|
|
|
+ return Json(new { errorCode = -1, message = "Send OTP API returned empty response." });
|
|
|
+ }
|
|
|
+
|
|
|
+ var jObject = JObject.Parse(res.data);
|
|
|
+ var errorCode = ReadErrorCode(jObject);
|
|
|
+ var message = jObject["message"]?.ToString() ?? jObject["msg"]?.ToString() ?? "";
|
|
|
+ if (errorCode != CommonErrorCode.SUCCESS)
|
|
|
+ {
|
|
|
+ return Json(new { errorCode, message = string.IsNullOrWhiteSpace(message) ? "Send OTP failed." : message });
|
|
|
+ }
|
|
|
+
|
|
|
+ var data = jObject["data"];
|
|
|
+ var normalizedMsisdn = data?["msisdn"]?.ToString() ?? msisdn.Trim();
|
|
|
+ var transactionId = data?["transactionId"]?.ToString();
|
|
|
+ if (string.IsNullOrWhiteSpace(transactionId))
|
|
|
+ {
|
|
|
+ return Json(new { errorCode = -1, message = "Send OTP succeeded but transactionId is empty." });
|
|
|
+ }
|
|
|
+
|
|
|
+ SessionExtension.SetComplexData(HttpContext.Session, PendingOtpMsisdnKey, normalizedMsisdn.ToString());
|
|
|
+ SessionExtension.SetComplexData(HttpContext.Session, PendingOtpTransactionKey, transactionId);
|
|
|
+
|
|
|
+ return Json(new
|
|
|
+ {
|
|
|
+ errorCode,
|
|
|
+ message = string.IsNullOrWhiteSpace(message) ? "Enter OTP from message." : message,
|
|
|
+ msisdn = normalizedMsisdn,
|
|
|
+ transactionId
|
|
|
+ });
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ log.Error("Test send OTP error: ", ex);
|
|
|
+ return Json(new { errorCode = -1, message = "Send OTP failed. Please check API service." });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ [HttpPost]
|
|
|
+ public async Task<IActionResult> ConfirmOtpAction(string msisdn, string transactionId, string otp, string? returnUrl = null)
|
|
|
+ {
|
|
|
+ if (string.IsNullOrWhiteSpace(msisdn)
|
|
|
+ || string.IsNullOrWhiteSpace(transactionId)
|
|
|
+ || string.IsNullOrWhiteSpace(otp))
|
|
|
+ {
|
|
|
+ return Json(new { errorCode = -1, message = "Please enter OTP." });
|
|
|
+ }
|
|
|
+
|
|
|
+ var baseUrl = GetParameter("url");
|
|
|
+ if (string.IsNullOrWhiteSpace(baseUrl))
|
|
|
+ {
|
|
|
+ return Json(new { errorCode = -1, message = "Missing API base URL." });
|
|
|
+ }
|
|
|
+
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var language = GetCurrentLanguage();
|
|
|
+ var res = await DotnetLib.Rest.RestHandler.SendPostAsync(
|
|
|
+ log,
|
|
|
+ baseUrl + UrlConstant.UserOtpConfirm,
|
|
|
+ new UserSendOtpReq
|
|
|
+ {
|
|
|
+ msisdn = msisdn.Trim(),
|
|
|
+ serviceName = TestServiceName,
|
|
|
+ language = language,
|
|
|
+ transactionId = transactionId.Trim(),
|
|
|
+ otp = otp.Trim()
|
|
|
+ },
|
|
|
+ language
|
|
|
+ );
|
|
|
+
|
|
|
+ if (res?.data == null)
|
|
|
+ {
|
|
|
+ return Json(new { errorCode = -1, message = "Confirm OTP API returned empty response." });
|
|
|
+ }
|
|
|
+
|
|
|
+ var jObject = JObject.Parse(res.data);
|
|
|
+ var errorCode = ReadErrorCode(jObject);
|
|
|
+ var message = jObject["message"]?.ToString() ?? jObject["msg"]?.ToString() ?? "";
|
|
|
+ if (errorCode != CommonErrorCode.SUCCESS)
|
|
|
+ {
|
|
|
+ return Json(new { errorCode, message = string.IsNullOrWhiteSpace(message) ? "Confirm OTP failed." : message });
|
|
|
+ }
|
|
|
+
|
|
|
+ var loggedMsisdn = jObject["data"]?["accountUser"]?["msisdn"]?.ToString()
|
|
|
+ ?? jObject["data"]?["accountUser"]?["Msisdn"]?.ToString()
|
|
|
+ ?? msisdn.Trim();
|
|
|
+
|
|
|
+ LoginSessionHelper.SaveLoginSession(HttpContext, loggedMsisdn, jObject);
|
|
|
+ ClearPendingOtp();
|
|
|
+ await PackagesCacheHelper.LoadAndCacheAsync(log, HttpContext.Session, baseUrl, language);
|
|
|
+
|
|
|
+ return Json(new
|
|
|
+ {
|
|
|
+ errorCode,
|
|
|
+ message = string.IsNullOrWhiteSpace(message) ? "Success" : message,
|
|
|
+ redirectUrl = Url.Action("Index", "Home") ?? "/"
|
|
|
+ });
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ log.Error("Test confirm OTP error: ", ex);
|
|
|
+ return Json(new { errorCode = -1, message = "Confirm OTP failed. Please check API service and OTP." });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ [HttpPost]
|
|
|
+ [ValidateAntiForgeryToken]
|
|
|
+ public IActionResult Logout()
|
|
|
+ {
|
|
|
+ LoginSessionHelper.ClearLoginSession(HttpContext);
|
|
|
+ ClearPendingOtp();
|
|
|
+ return RedirectToAction("Index", "Login");
|
|
|
+ }
|
|
|
+
|
|
|
+ [HttpPost]
|
|
|
+ public IActionResult LogoutBackAction()
|
|
|
+ {
|
|
|
+ LoginSessionHelper.ClearLoginSession(HttpContext);
|
|
|
+ ClearPendingOtp();
|
|
|
+ return Json(new { errorCode = CommonErrorCode.SUCCESS, redirectUrl = "mytel://back" });
|
|
|
+ }
|
|
|
+
|
|
|
+ private void ClearPendingOtp()
|
|
|
+ {
|
|
|
+ HttpContext.Session.Remove(PendingOtpMsisdnKey);
|
|
|
+ HttpContext.Session.Remove(PendingOtpTransactionKey);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static int ReadErrorCode(JObject jObject)
|
|
|
+ {
|
|
|
+ var value = jObject["errorCode"]?.ToString();
|
|
|
+ return int.TryParse(value, out var errorCode) ? errorCode : -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ private string GetParameter(string key)
|
|
|
+ {
|
|
|
+ return _configuration.GetSection(key).Value
|
|
|
+ ?? _configuration.GetSection("Url").Value
|
|
|
+ ?? "";
|
|
|
+ }
|
|
|
+
|
|
|
+ private string GetCurrentLanguage()
|
|
|
+ {
|
|
|
+ var sessionLang = HttpContext.Session.GetComplexData<string>("culture");
|
|
|
+ if (!string.IsNullOrWhiteSpace(sessionLang)) return sessionLang;
|
|
|
+ return System.Globalization.CultureInfo.CurrentUICulture.Name.StartsWith("vi", StringComparison.OrdinalIgnoreCase) ? "vi" : "en";
|
|
|
+ }
|
|
|
+
|
|
|
+ private string NormalizeReturnUrl(string? returnUrl)
|
|
|
+ {
|
|
|
+ if (!string.IsNullOrWhiteSpace(returnUrl) && Url.IsLocalUrl(returnUrl))
|
|
|
+ {
|
|
|
+ return returnUrl;
|
|
|
+ }
|
|
|
+
|
|
|
+ return Url.Action("Index", "Home") ?? "/";
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|