Reputation: 842
I'm writing my first web site, and am dealing with user registration. One common problem to me like to everyone else is to detect user already exist.
I am writing the app with python, and postgres as database. I have currently come up with 2 ideas:
1) lock(mutex) u = select from db where name = input_name if u == null insert into db (name) values (input_name) else return 'user already exist' unlock(mutex)
2) try: insert into db (name) values(input) except: return 'user already exist'
The first way is to use mutex lock for clear logic, while the second way using exception to indicate user existence.
Can anyone discuss what are the pros and cons of both of the methods?
Upvotes: 0
Views: 262
Reputation: 76073
I think both will work, and both are equally bad ideas. :) My point is that implementing user authentication in python/pg has been done so many times in the past that there's hardly justification for writing it yourself. Have you had a look at Django, for example? It will take care of this for you, and much more, and let you focus your efforts on your particular application.
Upvotes: 2
Reputation: 3899
Slightly different, I usually do a select query via AJAX to determine if a username already exists, that way I can display a message on the UI explaining that the name is already taken and suggest another before the submit the registration form.
Upvotes: 0