package tutorial.core.security;

import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * 
 * @author Xavier
 */
@Component
public class AuthSuccess extends SimpleUrlAuthenticationSuccessHandler {
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        /*Response sent when a user tries to log in with a correct user/password combination*/
        AccountUserDetails ac = (AccountUserDetails)authentication.getPrincipal();
        ac.getAccount().getAccountType();
        ac.getAccount().getName();
        
        response.setStatus(HttpServletResponse.SC_OK);
        response.setHeader("Name", ac.getAccount().getName());
        response.setHeader("Type", ""+ac.getAccount().getAccountType());
    }
}
