'Pyramid Authentication remember() header returns Attribute Error 'Nonetype' Object has no attribute USER_ID
I am trying to use Pyramid to create a log in system with a composite key. So there is a database with two tables, one for the customer with a customer id and company related information, and then another table with a completely unique username ([email protected]), USER_ID, and user related information plus permissions. Every user will belong to a company so the customer id is a composite key.
In my login view, I have this:
from pyramid.security import (
remember,
forget,
)
#other imports
.
.
.
@view_config(route_name='login_en', renderer='../templates/login-en.jinja2')
def login(request):
next_url = request.route_url('main_en')
message = ''
login = ''
if(request.method == 'POST'):
login = request.params['login'] #This is the html element for the username
password = request.params['password'] #Password
user = (
request.dbsession.query(models.CustomerUsers)
.filter_by(USERNAME_EMAIL=login)
.first() #Find user in db
)
if user is not None and user.check_password(password): #If user is in db
headers = remember(request, user.USER_ID) #remember them
return HTTPFound(location=next_url, headers=headers) #redirect them
message = 'Failed login' #bad credentials
request.response.status = 400
return dict(
message=message,
url=request.route_url('login_en'),
next_url=next_url,
login=login,
)
And this is functional in the sense that bad credentials produce failed login, and good credentials redirect. The problem is that when trying to render main_en an internal server is generated that says:
File "/usr/home/iot/frameworks/mywebapp/env/lib/python3.9/site-packages/mywebapp/views/default.py", line 20, in main_en
user_id = request.user.USER_ID
AttributeError: 'NoneType' object has no attribute 'USER_ID'
The main_en view looks like this
@view_config(route_name='main_en', renderer='../templates/main-en.jinja2')
def main_en(request):
user_id = request.user.USER_ID #get their id
if user_id is None: #they are not authenticated
raise HTTPForbidden #HTTPForbidden view is defined afterwards
message = '' #otherwise display the page
return dict(message=message)
I am confused about why headers = remember(request, user.USER_ID) is not passing the user information to the next url. Since it redirects only on good credentials, user and USER_ID definitely aren't none. So my question is am I using remember() incorrectly? Why is it either returning Nonetype to the header and/or not remembering when it redirects?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
