flask_login Cookie有效期设置

Flask做的网站想让用户登录后处于长期有效状态,网上找遍了,也没有看到相关中文文档,只能看英文说明。

原文地址如下:
flask_login.login_user

flask_login.login_user(user, remember=False, duration=None, force=False, fresh=True)

Logs a user in. You should pass the actual user object to this. If the user’s is_active property is False, they will not be logged in unless force is True.

This will return True if the log in attempt succeeds, and False if it fails (i.e. because the user is inactive).

Parameters:

*user (object) – The user object to log in.
remember (bool) – Whether to remember the user after their session expires. Defaults to False.
duration (datetime.timedelta) – The amount of time before the remember cookie expires. If None the value set in the settings is used. Defaults to None.

  • force (bool) – If the user is inactive, setting this to True will log them in regardless. Defaults to False.
  • fresh (bool) – setting this to False will log in the user with a session marked as not “fresh”. Defaults to True.

有两个关键参数

1.remember 设置为True

2.duration 设置cookie超时时间,如果不设置就读配置里面的设置,如果配置也没有设置就默认为None,如设置一年有效期,配置设置如下

import os
import datetime

class Config:
  ...
   PERMANENT_SESSION_LIFETIME = datetime.timedelta(days=360)

你可能感兴趣的:(flask_login Cookie有效期设置)