app.authentication package
Submodules
app.authentication.forms module
authentication/forms.py
Forms module for the Authentication Blueprint.
Flask Forms for User Registration and Login.
This module defines Flask forms for user registration and login using Flask-WTF. It includes two forms: RegistrationForm and LoginForm, each with fields for different user input, along with validation rules.
- Classes:
- RegistrationForm: Form for user registration with fields for first name, last name,
username, email, password, and password confirmation.
LoginForm: Form for user login with fields for username, password, and a remember me checkbox.
- Usage:
These forms can be used in Flask routes to handle user registration and login forms.
- class app.authentication.forms.LoginForm
Bases:
FlaskForm
Form for User Login.
This form includes fields for username, password, a “Remember Me” checkbox, and a submit button. It uses validators to ensure that input requirements are met.
- Fields:
‘’username’’ (StringField): Username field with length validation.
‘’password’’ (PasswordField): Password field with input required validation.
‘’remember’’ (BooleanField): Checkbox for “Remember Me” option.
‘’submit’’ (SubmitField): Submit button to trigger the form submission.
- Usage:
This form can be used in Flask routes to handle user login forms.
- password = <UnboundField(PasswordField, ('Password',), {'validators': [<wtforms.validators.InputRequired object>]})>
- remember = <UnboundField(BooleanField, ('Remember Me',), {})>
- submit = <UnboundField(SubmitField, ('Login',), {})>
- username = <UnboundField(StringField, ('Username',), {'validators': [<wtforms.validators.InputRequired object>, <wtforms.validators.Length object>]})>
- class app.authentication.forms.RegistrationForm
Bases:
FlaskForm
Form for User Registration.
This form includes fields for first name, last name, username, email, password, and password confirmation. It uses validators to ensure that input requirements are met.
- Fields:
‘’fname’’ (StringField): First name field with length validation.
‘’lname’’ (StringField): Last name field with length validation.
‘’username’’ (StringField): Username field with length validation and a custom placeholder.
‘’email’’ (StringField): Email field with email format validation.
‘’password’’ (PasswordField): Password field with input required validation.
- ‘’confirm_password’’ (PasswordField): Password confirmation field with input required validation
and equality to the password field.
‘’submit’’ (SubmitField): Submit button to trigger the form submission.
- Usage:
This form can be used in Flask routes to handle user registration forms.
- confirm_password = <UnboundField(PasswordField, ('Confirm Password',), {'validators': [<wtforms.validators.InputRequired object>, <wtforms.validators.EqualTo object>]})>
- email = <UnboundField(StringField, ('Email',), {'validators': [<wtforms.validators.InputRequired object>, <wtforms.validators.Email object>]})>
- fname = <UnboundField(StringField, ('First Name',), {'validators': [<wtforms.validators.InputRequired object>, <wtforms.validators.Length object>]})>
- lname = <UnboundField(StringField, ('Last Name',), {'validators': [<wtforms.validators.InputRequired object>, <wtforms.validators.Length object>]})>
- password = <UnboundField(PasswordField, ('Password',), {'validators': [<wtforms.validators.InputRequired object>]})>
- submit = <UnboundField(SubmitField, ('Sign Up',), {})>
- username = <UnboundField(StringField, ('Username',), {'validators': [<wtforms.validators.InputRequired object>, <wtforms.validators.Length object>], 'render_kw': {'placeholder': 'important!'}})>
app.authentication.models module
authentication/models.py
Models module for the Authentication Blueprint.
This module defines the User model class, representing user-related data in the database.
- Classes:
User: User model class representing user-related data.
- Attributes (User):
id (int): Primary key identifier for the user.
username (str): Unique username for the user.
password (str): Password for the user.
- Usage:
This module contains the User model class, which is used to interact with the database and manage user-related data.
- class app.authentication.models.User
Bases:
UserMixin
,Model
User Model Class.
This class represents the User model in the database. It extends the Flask-Login UserMixin for additional user management functionality.
- - id
Primary key identifier for the user.
- Type:
int
- - username
Unique username for the user.
- Type:
str
- - password
Password for the user.
- Type:
str
- Usage:
This class is used to interact with the database and manage user-related data.
- __init__(**kwargs)
A simple constructor that allows initialization from kwargs.
Sets attributes on the constructed instance using the names and values in
kwargs
.Only keys that are present as attributes of the instance’s class are allowed. These could be, for example, any mapped columns or relationships.
- id
- password
- query: t.ClassVar[Query]
A SQLAlchemy query for a model. Equivalent to
db.session.query(Model)
. Can be customized per-model by overridingquery_class
.Warning
The query interface is considered legacy in SQLAlchemy. Prefer using
session.execute(select())
instead.
- username
app.authentication.routes module
authentication/routes.py
Routes module for the Authentication Blueprint.
This module defines the routes for user authentication, including registration, login, and logout. It also includes the user loader function for Flask-Login.
- Routes:
/register: Handles user registration.
/login: Handles user login.
/logout: Handles user logout.
- Functions:
load_user: User loader function for Flask-Login.
- Usage:
This module contains routes for user authentication and should be registered as part of the Flask application.
- app.authentication.routes.load_user(user_id)
User loader function for Flask-Login.
- Parameters:
user_id (int) – The user identifier.
- Returns:
The User object for the given user_id.
- Return type:
- app.authentication.routes.login()
User login route.
- Returns:
Redirects to the library route on successful login.
- Return type:
Response
- app.authentication.routes.logout()
User logout route.
- Returns:
Redirects to the main index route after logout.
- Return type:
Response
- app.authentication.routes.register()
User registration route.
- Returns:
Redirects to the library route on successful registration.
- Return type:
Response
Module contents
authentication/__init__.py
Initialization module for the Authentication Blueprint.
This module initializes the Authentication Blueprint, which captures routes, models, and forms related to user authentication within your Flask application.
- Usage:
The Authentication Blueprint can be registered with the Flask application using the following code:
- Example:
from app.authentication import auth
app.register_blueprint(auth, url_prefix=’/auth’)
This would make the authentication routes accessible at ‘/auth/register’ and ‘/auth/login’.
Note: Make sure to update the URLs as needed based on your project structure and requirements.
_important!: this file is currently empty but helps to initialise the module for the package!