First step: remove the line that says “throw new …...Exception”
“throw” means “tell the system that something went wrong”.
You probably have a reason to circumvent the default login functionality, it would however be good for context/understanding if you describe how the user credentials are provided and if there is maybe another solution available for your problem.
This github repo contains the code of the old Autologin functionality. It gives an idea how your code should look like when you want to create a session and notify the requesting user about a success/failure. Please note that some of this code is already deprecated.
https://github.com/rcha103/-EOL-MxAutoLogin