refactor and make redirect back to previous page after sign-in/up more robust.