def authenticate(): i = ctx.request.input(remember='') email = i.email.strip().lower() password = i.password remember = i.remember user = User.find_first('where email=?', email) if user is None: raise APIError('auth:failed', 'email', 'Invalid email.') elif user.password != password: raise APIError('auth:failed', 'password', 'Invalid password.') max_age = 604800 if remember == 'true' else None cookie = make_signed_cookie(user.id, user.password, max_age) ctx.response.set_cookie(_COOKIE_NAME, cookie, max_age=max_age) user.password = '******' return user
def register_user(): i = ctx.request.input(name='', email='', password='') name = i.name.strip() email = i.email.strip().lower() password = i.password if not name: raise APIValueError('name') if not email or not _RE_EMAIL.match(email): raise APIValueError('email') if not password or not _RE_MD5.match(password): raise APIValueError('password') user = User.find_first('where email=?', email) if user: raise APIError('register:failed', 'email', 'Email is already in use.') user = User(name=name, email=email, password=password, image='http://www.gravatar.com/avatar/%s?d=mm&s=120' % hashlib.md5(email).hexdigest()) user.insert() cookie = make_signed_cookie(user.id, user.password, None) ctx.response.set_cookie(_COOKIE_NAME, cookie) return user
def index(): blogs = Blog.find_all() user = User.find_first('where email=?', '*****@*****.**') return dict(blog=blogs, user=user)