Hey @maverjk, I wasn’t trying to insinuate that you needed something very complicated, and certainly not a full-blown app, just that you could harvest a few design patterns from my code. I could have been much more specific.
The bottom line is that existing Frappe function don’t make the custom redirect on logout very easy, and the mechanisms are distinct between the frontend (portal pages) and the backend (app pages). For the app pages, you need to override the frappe.app.logout
JavaScript function. The original function is as follows:
logout() {
var me = this;
me.logged_out = true;
return frappe.call({
method: "logout",
callback: function (r) {
if (r.exc) {
return;
}
me.redirect_to_login();
},
});
}
The main issue here is that the logout call performed in the background, and the subsequent redirect_to_login
is hard-coded, and can’t be intercepted. The following works as an override, though if anyone has a more direct way to override this function, that would be great:
document.addEventListener('DOMContentLoaded', () => {
// Wait for frappe to be initialized
const onFrappeApplication = () => {
if (window.frappe?.app) {
// Override the logout function
frappe.app.logout() = () => {
var me = this;
me.logged_out = true;
return frappe.call({
method: "logout",
callback: function (r) {
if (r.exc) {
return;
}
// Redirect to the custom URL
window.location.href = "/custom-url";
},
});
};
} else {
// Check again in a moment if frappe isn't ready
setTimeout(onFrappeApplication, 100);
}
};
onFrappeApplication();
});
As I mentioned, the portal/web pages work completely differently. I managed to override the frontend menu with the following hook:
website_context = {
"post_login": [
{"label": "My Account", "url": "/me"},
{"label": "Log out", "url": "/?cmd=jwt_auth.auth.web_logout"} # Custom logout URL
]
}
This is pretty different from the above, as it relies on a custom api endpoint:
@frappe.whitelist()
def web_logout():
auth = SessionJWTAuth()
frappe.local.login_manager.logout()
if auth.settings.enabled:
location = auth.get_logout_url()
else:
location = "/login"
frappe.local.response["type"] = "redirect"
frappe.local.response["location"] = location
Again, if anyone has any improvements to suggest on the above, that would be most welcome.