0

If i am going to delete employee if employee is logged in after deleting or disable his/her account how he will automatically logout from his account

1

How employee automatically logout after account deletion happen from admin side

vimuth
  • 5,064
  • 33
  • 79
  • 116
  • Remove/invalidate their session on the server side. Unless you have a web socket/polling/etc on the client side they won’t know they’ve been logged out until they make another request. – Dave Newton Jan 28 '23 at 14:14
  • Unrelated but the image does not provide any useful information; it’s a picture of a table (with a… curious color palette). – Dave Newton Jan 28 '23 at 14:15

1 Answers1

1

To actively logout the user, you'll need to send a push message which in turn basically triggers the submit of a hidden logout form. You can do this via a web socket. A kickoff example can be found in this answer: Notify only specific user(s) through WebSockets, when something is modified in the database.

To passively logout the user, you'll need to implement a servlet filter which checks on every single request if the currently logged-in user is still valid. Here's a kickoff example assuming that you're using homegrown user authentication (which is confirmed by the screenshot as visible in your deleted answer here):

@WebFilter("/whatever_url_pattern_covering_your_restricted_pages/*")
public class YourLoginFilter extends HttpFilter {

    @Override
    public void doFilter(HttpServletRequest request, HttpServletRequest response, FilterChain chain) throws IOException, ServletException {
        HttpSession session = request.getSession(false);
        YourUser user = (session == null) ? null : (YourUser) session.getAttribute("user");
        
        if (user == null) {
            // No logged-in user found, so redirect to login page.
            response.sendRedirect(request.getContextPath() + "/login");
        } else {
            // Logged-in user found, so validate it.
            if (yourUserService.isValid(user)) {
                // User is still valid, so continue request.
                chain.doFilter(request, response);
            } else {
                // User is not valid anymore (e.g. deleted by admin), so block request.
                response.sendRedirect(request.getContextPath() + "/blocked");
            }
        }
    }
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555