Signup-login using the same form

Is it possible to use the same form containing email id and password to sign up the user if he does’nt exist in the database or log him in if is already registered ?

Never mind, I found the solution at Sign up unique username + email only isn't working