CustomAuthenticationSuccessHandler.java

package com.ctrlbuy.webshop.config;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

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

@Component
public class CustomAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request,
                                        HttpServletResponse response,
                                        Authentication authentication) throws IOException {

        // Kolla om användaren har admin-rättigheter
        boolean isAdmin = authentication.getAuthorities()
                .contains(new SimpleGrantedAuthority("ROLE_ADMIN"));

        // Kolla om användaren ursprungligen försökte nå admin-området
        String redirectUrl = request.getParameter("redirect");
        if (redirectUrl != null && redirectUrl.startsWith("/admin") && isAdmin) {
            response.sendRedirect(redirectUrl);
            return;
        }

        // Standard omdirigering baserat på roll
        if (isAdmin) {
            response.sendRedirect("/admin/dashboard");
        } else {
            response.sendRedirect("/");
        }
    }
}