add api context and logout

This commit is contained in:
dirgantarasiahaan
2023-05-25 15:39:36 +07:00
parent f0dfef725a
commit c4700af832
8 changed files with 377 additions and 1 deletions

View File

@ -0,0 +1,44 @@
package com.iconplus.smartproc.service.authentication;
import com.iconplus.smartproc.exception.BusinessException;
import com.iconplus.smartproc.helper.context.ApiContext;
import com.iconplus.smartproc.helper.model.EmptyRequest;
import com.iconplus.smartproc.helper.model.EmptyResponse;
import com.iconplus.smartproc.helper.service.BaseService;
import com.iconplus.smartproc.model.entity.TokenManagement;
import com.iconplus.smartproc.repository.TokenManagementRepository;
import com.iconplus.smartproc.repository.UsersRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class LogoutService implements BaseService<EmptyRequest, EmptyResponse> {
private ApiContext apiContext;
private TokenManagementRepository tokenManagementRepository;
private LogoutService(ApiContext apiContext,
TokenManagementRepository tokenManagementRepository) {
this.apiContext = apiContext;
this.tokenManagementRepository = tokenManagementRepository;
}
@Override
public EmptyResponse execute(EmptyRequest input) {
String accessToken = apiContext.getAuthorization();
TokenManagement tokenManagement = getTokenManagement(accessToken);
tokenManagement.setIsDelete(true);
tokenManagementRepository.save(tokenManagement);
return new EmptyResponse();
}
private TokenManagement getTokenManagement(String accessToken) {
var tokenManagement = tokenManagementRepository.findByAccessTokenAndIsDeleteFalse(accessToken);
if (tokenManagement.isEmpty()) {
throw new BusinessException("err", "err", "err");
}
return tokenManagement.get();
}
}