Writing Secure Authentication Logic