Skip to content

Customize Base Settings

flask_authlib.BaseConfig - contains core settings of the library.

Default values of this object:

1
2
3
4
5
6
7
8
9
HOME_URL: str = "/"
LOGIN_URL: str = "/login"
REGISTER_URL: str = "/register"
LOGOUT_URL: str = "/logout"

LOGIN_MESSAGE_CATEGORY = "info"

MIN_PASSWORD_LENGTH: int = 8
EMAIL_UNIQUE: bool = True
  • HOME_URL - home page URL
  • LOGIN_URL - login page URL
  • REGISTER_URL: register page URL
  • LOGOUT_URL - logout URL
  • LOGIN_MESSAGE_CATEGORY - login message category ( flash message ), it is required by flask_login
  • MIN_PASSWORD_LENGTH - minimum password length, it will be used in registration process.
  • EMAIL_UNIQUE - if it is true, users' email will be validated for uniquennes in the register process.

Customizing Base Config

Basic Application:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_login import login_required
from flask_authlib import AuthManager

app = Flask(__name__)
app.config.update(
    DEBUG=True,
    SQLALCHEMY_DATABASE_URI="sqlite:///database.db"
)
db = SQLAlchemy(app)

auth = AuthManager(app, db)


@app.route("/protected")
@login_required
def protected():
    return "HI, I'M PROTECTED 👋"

if __name__ == "__main__":
    app.run()

Import BaseConfig from flask_authlib and create a python class that will describe your custom settings:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_login import login_required

from flask_authlib import AuthManager
from flask_authlib import BaseConfig

app = Flask(__name__)
app.config.update(
    DEBUG=True,
    SQLALCHEMY_DATABASE_URI="sqlite:///database.db"
)

db = SQLAlchemy(app)

class MySettings(BaseConfig):
    pass

auth = AuthManager(app, db)


@app.route("/protected")
@login_required
def protected():
    return "HI, I'M PROTECTED 👋"

if __name__ == "__main__":
    app.run()

Settings

There are also some settings that I do not recommend changing: TEMPLATES_FOLDER_NAME and others that I have not written above.

Let's customize settings:

1
2
3
4
5
class MySettings(BaseConfig):
    LOGIN_URL="/auth/login"
    REGISTER_URL="/auth/register"
    LOGOUT_URL: str = "/auth/logout"
    MIN_PASSWORD_LENGTH=12

Now, after customizing we can give our settings object as argument( configs are Optional ) to the Auth class:

1
2
3
4
5
6
7
class MySettings(BaseConfig):
    LOGIN_URL="/auth/login"
    REGISTER_URL="/auth/register"
    LOGOUT_URL= "/auth/logout"
    MIN_PASSWORD_LENGTH=12

auth = AuthManager(app, db,base_config=MySettings)
  • Navigate to /login:

LOGIN_PAGE_404

  • Yeah, we changed the login URL, then we should navigate /auth/login:

LOGIN_PAGE_404

  • Check register URL: REGISTER_PAGE

  • Check password length: PASS_LEN

  • Check password length validation: PASS_LEN_VAL

  • Check register functionality: REGISTER_FUNC

  • Check logout URL after login: LOGOUT

HTTP 302

If we navigate /auth/logout, the built-in logout view clears all sessions and will redirect us to HOME_PAGE.

Back to top