public boolean register(User user) { User exist = context.users().get(user.getEmail()); if (exist == null) { user.setPassword(BCrypt.hashpw(user.getPassword(), BCrypt.gensalt())); context.users().create(user); return true; } else { return false; } }
public User login(User user) { User registered = context.users().get(user.getEmail()); if (registered != null && BCrypt.checkpw(user.getPassword(), registered.getPassword())) { Cookie cookie = new Cookie("authToken", BCrypt.hashpw(registered.getEmail(), BCrypt.gensalt())); cookie.setMaxAge(60 * 60); cookie.setPath("/"); registered.setCookie(cookie); context.users().update(registered); registered.setPassword(null); return registered; } return null; }
public User get(Cookie cookie) { User registered = context.users().get(cookie); if (registered != null) { registered.setCookie(cookie); return registered; } return null; }
public void logout(User entity) { entity.setCookie(null); context.users().update(entity); }