From e7ff6eaf7ffd713d102f596b4e5d906504160ff8 Mon Sep 17 00:00:00 2001
From: Jack <jack.su@pollex.com.tw>
Date: 星期二, 23 十一月 2021 18:34:23 +0800
Subject: [PATCH] [ADD] 客戶註冊的API

---
 pamapi/src/main/java/com/pollex/pam/security/provider/OtpAuthenticationProvider.java |   29 ++++++++++++++++++++++++-----
 1 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/pamapi/src/main/java/com/pollex/pam/security/provider/OtpAuthenticationProvider.java b/pamapi/src/main/java/com/pollex/pam/security/provider/OtpAuthenticationProvider.java
index 6fadbba..43138ce 100644
--- a/pamapi/src/main/java/com/pollex/pam/security/provider/OtpAuthenticationProvider.java
+++ b/pamapi/src/main/java/com/pollex/pam/security/provider/OtpAuthenticationProvider.java
@@ -2,9 +2,13 @@
 
 import com.pollex.pam.config.ApplicationProperties;
 import com.pollex.pam.domain.Customer;
+import com.pollex.pam.domain.OtpTmp;
 import com.pollex.pam.enums.CustomerDetailEnum;
+import com.pollex.pam.enums.OtpLoginTypeEnum;
+import com.pollex.pam.enums.OtpTmpStatusEnum;
 import com.pollex.pam.repository.CustomerRepository;
 import com.pollex.pam.security.token.OtpAuthenticationToken;
+import com.pollex.pam.service.OtpTmpService;
 import com.pollex.pam.service.OtpWebService;
 import com.pollex.pam.service.dto.OtpResponseDTO;
 import com.pollex.pam.web.rest.vm.OtpAccount;
@@ -38,6 +42,9 @@
 
     @Autowired
     CustomerRepository customerRepository;
+    
+    @Autowired
+    OtpTmpService otpTmpService;
 
     public Authentication authenticate(OtpAuthenticationToken otpAuthenticationToken) throws AuthenticationException {
         OtpAccount otpAccount = otpAuthenticationToken.getPrincipal();
@@ -46,13 +53,15 @@
         String otpCode = otpAuthenticationToken.getCredentials();
 
         if(applicationProperty.isMockLogin()){
-            return getCustomerToken(account, otpCode);
+        	setVerrifiedOtpTmp(account, indexKey);
+            return getCustomerToken(account, otpCode, indexKey);
         }
 
         try {
             OtpResponseDTO otpResponseDTO = otpWebService.verifyOTP(indexKey, otpCode);
             if(otpResponseDTO.isSuccess()) {
-                return getCustomerToken(account, otpCode);
+            	setVerrifiedOtpTmp(account, indexKey);
+                return getCustomerToken(account, otpCode, indexKey);
             }
         } catch (Exception e) {
             log.error("Exception: ", e);
@@ -62,9 +71,18 @@
         throw new AuthenticationCredentialsNotFoundException("");
     }
 
-    private UsernamePasswordAuthenticationToken getCustomerToken(String account, String otpCode) {
-        // todo �摮�DB��撅祆迤撣貊鞊∴����畾essage����垢��脰�酉���
-        Customer customer = customerRepository.findOneByEmailEqualsOrPhoneEquals(account, account).orElseThrow(() -> new UsernameNotFoundException("this customer is not in db, account = " + account));
+    private void setVerrifiedOtpTmp(String account, String indexKey) {
+    	OtpTmp otpTmp = otpTmpService.findByAccountAndIndexKey(account, indexKey);
+    	otpTmp.setStatus(OtpTmpStatusEnum.VERRIFIED);
+    	otpTmpService.save(otpTmp);
+	}
+
+	private UsernamePasswordAuthenticationToken getCustomerToken(String account
+    		, String otpCode, String indexKey) {
+    	
+    	
+    	// todo �摮�DB��撅祆迤撣貊鞊∴����畾essage����垢��脰�酉���
+        Customer customer = customerRepository.findOneByEmailEqualsOrPhoneEquals(account, account).orElseThrow(() -> new UsernameNotFoundException("this customer is not in register, account = " + account));
 
         List<GrantedAuthority> grantedAuths = Arrays.asList(new SimpleGrantedAuthority("ROLE_USER"));
         UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(account, otpCode, grantedAuths);
@@ -73,6 +91,7 @@
         details.put(CustomerDetailEnum.ID.getValue(), customer.getId().toString());
         details.put(CustomerDetailEnum.NAME.getValue(), customer.getName());
         details.put(CustomerDetailEnum.ACCOUNT.getValue(), account);
+//        details.put(CustomerDetailEnum.CONTACT_TYPE.getValue(), customer.getContactType());
         authenticationToken.setDetails(details);
 
         return authenticationToken;

--
Gitblit v1.8.0