dhulipudi / webextest

test
0 stars 0 forks source link

logout #10

Open dhulipudi opened 5 months ago

dhulipudi commented 5 months ago

// LogoutServlet.java import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.SlingHttpServletResponse; import org.apache.sling.api.servlets.SlingAllMethodsServlet; import org.osgi.service.component.annotations.Component;

import javax.servlet.Servlet; import javax.servlet.ServletException; import java.io.IOException;

@Component(service = Servlet.class, property = { "sling.servlet.methods=POST", "sling.servlet.paths=/bin/logout" }) public class LogoutServlet extends SlingAllMethodsServlet {

@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
    request.getSession().invalidate();
    response.setStatus(SlingHttpServletResponse.SC_OK);
}

}

==========

// logout.js function logout() { fetch('/bin/logout', { method: 'POST', }).then(response => { if (response.ok) { window.location.href = '/content/your-website/login.html'; } else { console.error('Logout failed'); } }); }