package kr.co.uplus.ez.api.login;

import java.io.IOException;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;

import kr.co.uplus.ez.api.login.dto.AuthUser;
import kr.co.uplus.ez.common.data.ApiResponseCode;
import kr.co.uplus.ez.common.data.Const;

/**
 * 로그인에 성공하면 호출되는 기본 핸들러는 SavedRequestAwareAuthenticationSuccessHandler 클래스이다.
 */
public class LoginSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
	@Autowired
	private LoginService svc;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
            Authentication authentication) throws ServletException, IOException {
    	process(request, response, authentication);
    	super.onAuthenticationSuccess(request, response, authentication);
    }

    public ApiResponseCode process(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
		AuthUser user = (AuthUser) request.getAttribute(Const.KEY_LOAD_USER);
		user.setPwd(null);
		ApiResponseCode rCode = ApiResponseCode.SUCCESS;

		svc.setLoginInfo(user.getOprtrId());

		super.clearAuthenticationAttributes(request);
		return rCode;
    }
}
