As mentioned in this link : https://trailblazers.salesforce.com/answers?id=9063A000000piHnQAI you can have login flows that execute whenever a user logs in and for a way to execute apex code you can use the implementation mentioned in this link: https://salesforce.stackexchange.com/questions/9926/execute-an-action-run-a-trigger-every-time-a-user-logs-in-to-salesforce
Let me know if there are any questions and in case if this comes in handy can you please choose this as the best answer so that it can be used by others in the future.
As mentioned in this link : https://trailblazers.salesforce.com/answers?id=9063A000000piHnQAI you can have login flows that execute whenever a user logs in and for a way to execute apex code you can use the implementation mentioned in this link: https://salesforce.stackexchange.com/questions/9926/execute-an-action-run-a-trigger-every-time-a-user-logs-in-to-salesforce
Let me know if there are any questions and in case if this comes in handy can you please choose this as the best answer so that it can be used by others in the future.
Regards,
Anutej