R
Ryan Stewart
I'm wondering how you out there who work with web applications handle
user login. The common practice where I am has always been to stick a
User object in the session:
session.setAttribute("currentUser", user);
I'm becoming less and less satisfied with that method. It seems very
cluttered and not very object oriented. Anywhere that we want to check
if someone is logged in, it's:
if (session.getAttribute("currentUser") != null) {
....
}
Or worse, if we want to do something with a user *if* he or she is
logged in:
User user = (User) session.getAttribute("currentUser");
if (user != null) {
....
} else {
....
}
First, I don't like having to know about the "currentUser" key. That
should be hidden away somewhere safe where other developers working on
or maintaining the project don't have to know about it if they don't
want to. Also, the "log a user in" and "is a user logged in?" processes
seem to be behavior, which, under the guidelines of OO design, should
be methods. Not writing them as methods seems to me to promote
procedural programming in a place where the code can already get quite
complex.
One possible solution would be something like this in the User class
(as a minimal implementation):
public void login(HttpSession session) {
session.setAttribute("currentUser", this);
}
public User getUser(HttpSession session) {
return (User) session.getAttribute("currentUser");
}
Or possibly put that in a utility class of some sort. Then it becomes
part of the User (or whatever) interface, easily documented and
understood, and the implementation is hidden, as I feel it should be.
Whatever the case, you run into the MVC argument. User (or a utility
class) is Model. HttpSession is more to the Controller side. The two
shouldn't interact in this way in some people's opinions.
I know there's something wrong here, but I haven't quite figured out
what to do yet. Does anyone else have insight on this or know where to
find some best practices?
user login. The common practice where I am has always been to stick a
User object in the session:
session.setAttribute("currentUser", user);
I'm becoming less and less satisfied with that method. It seems very
cluttered and not very object oriented. Anywhere that we want to check
if someone is logged in, it's:
if (session.getAttribute("currentUser") != null) {
....
}
Or worse, if we want to do something with a user *if* he or she is
logged in:
User user = (User) session.getAttribute("currentUser");
if (user != null) {
....
} else {
....
}
First, I don't like having to know about the "currentUser" key. That
should be hidden away somewhere safe where other developers working on
or maintaining the project don't have to know about it if they don't
want to. Also, the "log a user in" and "is a user logged in?" processes
seem to be behavior, which, under the guidelines of OO design, should
be methods. Not writing them as methods seems to me to promote
procedural programming in a place where the code can already get quite
complex.
One possible solution would be something like this in the User class
(as a minimal implementation):
public void login(HttpSession session) {
session.setAttribute("currentUser", this);
}
public User getUser(HttpSession session) {
return (User) session.getAttribute("currentUser");
}
Or possibly put that in a utility class of some sort. Then it becomes
part of the User (or whatever) interface, easily documented and
understood, and the implementation is hidden, as I feel it should be.
Whatever the case, you run into the MVC argument. User (or a utility
class) is Model. HttpSession is more to the Controller side. The two
shouldn't interact in this way in some people's opinions.
I know there's something wrong here, but I haven't quite figured out
what to do yet. Does anyone else have insight on this or know where to
find some best practices?