package com.spring.infra.security.handler;

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

import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutHandler;

import com.spring.infra.security.jwt.JwtTokenRule;
import com.spring.infra.security.jwt.JwtTokenService;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class SignOutHandler implements LogoutHandler {

    private final JwtTokenService jwtTokenService;

    @Override
    public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
        String refreshToken = jwtTokenService.resolveTokenFromCookie(request, JwtTokenRule.REFRESH_PREFIX);
        jwtTokenService.deleteRefreshToken(jwtTokenService.getUserPk(refreshToken));
        jwtTokenService.deleteCookie(response);
    }
    
}
