Basic Usage of Library

Flask-Authlib uses SQLAlchemy(sqlalchemy extension for flask) for the database part. After installation this ORM, you should import AuthManager and use it like this:

1
2
3
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_authlib import AuthManager

Define your Flask application, db and AuthManager base object:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
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)

flask_authlib.AuthManager

AuthManager takes two arguments(required): app (Flask App) and db (SQLAlchemy).

Add a simple route and protect it by flask login's login_required decorator for testing login functionality:

 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()

If we try to navigate /protected, it should navigate us to the login page. That is why we used flask_login's @login_required.

Next, run your flask application and you will see magic things 😂 (navigate to protected route):

  • Login Page LOGIN_PAGE

  • Register Page REGISTER_PAGE Tada 🎉

If we look our application's directory, we can see new folders which are created by flask_authlib:

  • static/ - stores static files ( js, css, fonts ... )
  • templates/ - stores *.html files
Back to top