ADD logout functionality to SecurityConfig and Topbar

This commit is contained in:
2025-06-24 20:23:56 +02:00
parent bf071d06f9
commit 071f3c420c
2 changed files with 28 additions and 5 deletions

View File

@@ -13,6 +13,7 @@ type props = {
const Topbar = ({sidebarToggled, setSidebarToggled, user, className}: props) => { const Topbar = ({sidebarToggled, setSidebarToggled, user, className}: props) => {
const apiUrl = import.meta.env.VITE_API_URL; const apiUrl = import.meta.env.VITE_API_URL;
const loginUrl = `${apiUrl}/oauth2/authorization/google`; const loginUrl = `${apiUrl}/oauth2/authorization/google`;
const logoutUrl = `${apiUrl}/api/v1/auth/logout`;
return ( return (
<div className={clsx(className, "flex justify-between")}> <div className={clsx(className, "flex justify-between")}>
@@ -20,10 +21,24 @@ const Topbar = ({sidebarToggled, setSidebarToggled, user, className}: props) =>
{sidebarToggled ? <Menu size={24}/> : <X size={24}/>} {sidebarToggled ? <Menu size={24}/> : <X size={24}/>}
</MenuButton> </MenuButton>
<MenuButton className={"w-40 text-gray-600"} { user ? (
onClick={() => globalThis.location.href = loginUrl}> <div>
{ user ? user.name : "Login" } <MenuButton className={"w-40 text-gray-600"}>
</MenuButton> {user.name}
</MenuButton>
<MenuButton className={"w-20 text-gray-600"}
onClick={() => globalThis.location.href = logoutUrl}>
Logout
</MenuButton>
</div>
) :
(
<MenuButton className={"w-20 text-gray-600"}
onClick={() => globalThis.location.href = loginUrl}>
Login
</MenuButton>
)};
</div> </div>
) )
} }

View File

@@ -9,6 +9,7 @@ import org.springframework.security.config.annotation.web.configuration.EnableWe
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.web.SecurityFilterChain; import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler; import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
@Configuration @Configuration
@EnableWebSecurity @EnableWebSecurity
@@ -41,7 +42,9 @@ public class SecurityConfig {
.successHandler(successHandler())) .successHandler(successHandler()))
.logout(logout -> logout .logout(logout -> logout
.logoutUrl("/api/v1/auth/logout") .logoutUrl("/api/v1/auth/logout")
.logoutSuccessUrl(frontendUrl) .logoutSuccessHandler(logoutSuccessHandler())
.invalidateHttpSession(true)
.deleteCookies("JSESSIONID")
); );
return http.build(); return http.build();
@@ -52,4 +55,9 @@ public class SecurityConfig {
return (request, response, authentication) -> response.sendRedirect(frontendUrl); return (request, response, authentication) -> response.sendRedirect(frontendUrl);
} }
@Bean
public LogoutSuccessHandler logoutSuccessHandler() {
return (request, response, authentication) -> response.sendRedirect(frontendUrl);
}
} }