This is possible with the help Custom Authentication Extensions, there are different events are available to configure, where you can call your custom logic in the workflow Below are the events supported
Diagram from MSDN on the events workflow
Check this link from Microsoft here : https://learn.microsoft.com/en-us/entra/identity-platform/custom-extension-overview