Skip to content

Commit

Permalink
Fix indentation in Customizing the Login Form code (#1074)
Browse files Browse the repository at this point in the history
Signed-off-by: Lawrence González <lawrencejgd@gmail.com>
  • Loading branch information
LawrenceJGD authored Feb 23, 2025
1 parent 5ea756b commit 4473f92
Showing 1 changed file with 51 additions and 51 deletions.
102 changes: 51 additions & 51 deletions docs/customizing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -267,59 +267,59 @@ any more, however, Flask-Security's LoginForm uses 2 different input fields (so
appropriate input attributes can be set)::

from flask_security import (
RegisterForm,
LoginForm,
Security,
lookup_identity,
uia_username_mapper,
unique_identity_attribute,
RegisterForm,
LoginForm,
Security,
lookup_identity,
uia_username_mapper,
unique_identity_attribute,
)
from werkzeug.local import LocalProxy
from wtforms import StringField, ValidationError, validators

def username_validator(form, field):
# Side-effect - field.data is updated to normalized value.
# Use proxy to we can declare this prior to initializing Security.
_security = LocalProxy(lambda: app.extensions["security"])
msg, field.data = _security._username_util.validate(field.data)
if msg:
raise ValidationError(msg)

class MyRegisterForm(RegisterForm):
# Note that unique_identity_attribute uses the defined field 'mapper' to
# normalize. We validate before that to give better error messages and
# to set the normalized value into the form for saving.
username = StringField(
"Username",
validators=[
validators.data_required(),
username_validator,
unique_identity_attribute,
],
)
from werkzeug.local import LocalProxy
from wtforms import StringField, ValidationError, validators

def username_validator(form, field):
# Side-effect - field.data is updated to normalized value.
# Use proxy to we can declare this prior to initializing Security.
_security = LocalProxy(lambda: app.extensions["security"])
msg, field.data = _security._username_util.validate(field.data)
if msg:
raise ValidationError(msg)

class MyRegisterForm(RegisterForm):
# Note that unique_identity_attribute uses the defined field 'mapper' to
# normalize. We validate before that to give better error messages and
# to set the normalized value into the form for saving.
username = StringField(
"Username",
validators=[
validators.data_required(),
username_validator,
unique_identity_attribute,
],
)

class MyLoginForm(LoginForm):
email = StringField("email", [validators.data_required()])

def validate(self, **kwargs):
self.user = lookup_identity(self.email.data)
# Setting 'ifield' informs the default login form validation
# handler that the identity has already been confirmed.
self.ifield = self.email
if not super().validate(**kwargs):
return False
return True

# Allow registration with email, but login only with username
app.config["SECURITY_USER_IDENTITY_ATTRIBUTES"] = [
{"username": {"mapper": uia_username_mapper}}
]
security = Security(
datastore=sqlalchemy_datastore,
register_form=MyRegisterForm,
login_form=MyLoginForm,
)
security.init_app(app)
class MyLoginForm(LoginForm):
email = StringField("email", [validators.data_required()])

def validate(self, **kwargs):
self.user = lookup_identity(self.email.data)
# Setting 'ifield' informs the default login form validation
# handler that the identity has already been confirmed.
self.ifield = self.email
if not super().validate(**kwargs):
return False
return True

# Allow registration with email, but login only with username
app.config["SECURITY_USER_IDENTITY_ATTRIBUTES"] = [
{"username": {"mapper": uia_username_mapper}}
]
security = Security(
datastore=sqlalchemy_datastore,
register_form=MyRegisterForm,
login_form=MyLoginForm,
)
security.init_app(app)

Localization
------------
Expand Down

0 comments on commit 4473f92

Please sign in to comment.