code: def login(self):
          
          form = self._get_form('SECURITY_LOGIN_FORM')
          if form.validate_on_submit():
              try:
                  self.security_service.login_user(form.user, form.remember.data)
              except AuthenticationError as e:
                  form._errors = {'_error': [str(e)]}
              else:
                  self.after_this_request(self._commit)
                  if request.is_json:
                      return self.jsonify({'token': form.user.get_auth_token(),
                                           'user': form.user})
                  self.flash(_('flask_unchained.bundles.security:flash.login'),
                             category='success')
                  return self.redirect('SECURITY_POST_LOGIN_REDIRECT_ENDPOINT')
          else:
              # FIXME-identity
              identity_attrs = app.config.SECURITY_USER_IDENTITY_ATTRIBUTES
              msg = f"Invalid {', '.join(identity_attrs)} and/or password."
  
              # we just want a single top-level form error
              form._errors = {'_error': [msg]}
              for field in form._fields.values():
                  field.errors = None
  
          if form.errors and request.is_json:
              return self.jsonify({'error': form.errors.get('_error')[0]},
                                  code=HTTPStatus.UNAUTHORIZED)
  
          return self.render('login',
                             login_user_form=form,
                             **self.security.run_ctx_processor('login'))
docs: View function to log a user in. Supports html and json requests.