package mx.gob.jumapacelaya.api;

import com.vaadin.flow.spring.security.AuthenticationContext;
import mx.gob.jumapacelaya.views.login.LoginView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

@Component
public class SecurityService {
    private static final Logger log = LoggerFactory.getLogger(SecurityService.class);

    private final AuthenticationContext authenticationContext;

    public SecurityService(AuthenticationContext authenticationContext) {
        this.authenticationContext = authenticationContext;
    }

    public String getAuthenticatedUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        log.debug("Authentication: {}", authentication.getName());

        if (authentication != null) {
            return authentication.getName();
        }
        return null;
    }


    public void logout() {
        authenticationContext.logout();
    }

}
