package es.uv.saic.config;

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

import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.savedrequest.DefaultSavedRequest;

public class AuthSuccessHandler implements AuthenticationSuccessHandler{
	
	@Override
	public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication auth)
	        throws IOException,  ServletException {
	    request.getSession(false).setMaxInactiveInterval(3600); //3600
		if(request.getSession().getAttribute("SPRING_SECURITY_SAVED_REQUEST") != null){
			DefaultSavedRequest defaultSavedRequest = (DefaultSavedRequest) request.getSession().getAttribute("SPRING_SECURITY_SAVED_REQUEST");
	    	response.sendRedirect(defaultSavedRequest.getRedirectUrl());
		}
		else{
			response.sendRedirect(request.getContextPath()+"/procedures?_new=1");
		}
	}
}
