I was looking for a similar solution. I'm using bottle-cork.py for my user authentication and needed a way to give users the option "Keep me logged in"
from bottle, import request, response # etc...
def post_get(name, default=''):
return bottle.request.POST.get(name, default).strip()
def login():
"""Authenticate users"""
username = post_get('username').lower()
password = post_get('password')
keep_login = post_get('keep_login')
session = request.environ['beaker.session']
if keep_login == 'true':
session.cookie_expires = False
response.set_cookie('keep_login', "true")
else:
session.cookie_expires = True
response.set_cookie('keep_login', "false")
aaa.login(username, password)
However, every time a request is sent to the server, bottle returns a new session cookie that defaults back to expiring when the browser closes. To fix this, I added a function that I call every time a request is sent:
def preserve_cookie(request):
keep_login = request.get_cookie('keep_login')
session = request.environ['beaker.session']
if keep_login == 'true':
session.cookie_expires = False
return request
So, for instance:
@bottle.get('/get_username')
def check_login(user=None):
try:
aaa.require(username=user)
except:
raise bottle.HTTPError(401)
preserve_cookie(request)
return aaa.current_user.username
This way the new cookie that is returned maintains the user's preference of keeping the login session saved. However, the way beaker.SessionMiddleware is currently implemented, it just sets the cookie to expire Jan 18, 2038.