Execute custom script on user login/logout

I have a client side script and I want to execute it when a user logs in and out. How do I achieve that?

1 Like

Question is old, but may this answer will help others facing same problem in future, btw this trick clearly mentioned in the documentation hooks section.

Link for documentation page:
https://frappeframework.com/docs/user/en/python-api/hooks#session-hooks

Isn’t this for server side script(python) and not client side? I want to execute js code.

sorry, my bad…
but dude you can’t communicate to client side from server, but there are some hacks you can do

  1. apply your logics at server side
  2. extend frappe logout function

for example:
frappe.app.logout = () => {
var me = this;
me.logged_out = true;
return frappe.call({
method: “logout”,
callback: function (r) {
if (r.exc) {
return;
}
// do your stuff here
document.location.href = “/login”;
},
});
};

I have a specific case where I need the code to be executed at client side. Extending the logout function will be modification of core. I’m on Frappe Could and has no access to core. :confused:

no dude you don’t want core access, ok np, follow these steps:

  1. make a new app
  2. make a js file inside your app and then include it in build.json
  3. in that js file simply overwrite frappe default function, and in the callback section, just manipulate the flow as you want

for example:
frappe core logout function:
image

your custom js logout function in your own app:
image

cheers!

1 Like