package com.style24.front.support.security.handler;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.SimpleUrlLogoutSuccessHandler;

import com.style24.front.biz.service.TsfLoginService;
import com.style24.front.support.env.TsfConstants;
import com.style24.front.support.security.session.TsfSession;

import lombok.extern.slf4j.Slf4j;

import com.gagaframework.web.util.GagaCookieUtil;

/**
 * 로그아웃 성공 시 호출되는 Handler
 * 
 * @author gagamel
 * @since 2020. 9. 11
 */
@Slf4j
public class TsfLogoutSuccessHandler extends SimpleUrlLogoutSuccessHandler {

	@Autowired
	private TsfLoginService loginService;

	@Override
	public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
		TsfSession.invalidate(request);

		// RememberMe 쿠키가 있으면
		String ckRememberMe = GagaCookieUtil.getCookie(request, TsfConstants.CK_PREFIX + "_remember-me");

		if (StringUtils.isNotBlank(ckRememberMe)) {
			// 로그인유지토큰 삭제
			loginService.deletePersistentToken(ckRememberMe);

			// RememberMe 쿠키 삭제
			GagaCookieUtil.deleteCookie(response, TsfConstants.CK_PREFIX + "_remember-me");
		}

		response.sendRedirect("/");
	}

}
