I am new to Flask learning it from a tutorial video. I am trying to authenticate and get a access token. I have created user class which is in user.py :
class User:
def __init__(self, _id, username, password):
self.id = _id,
self.username = username,
self.password = password
in my security.py :
from werkzeug.security import safe_str_cmp
from user import User
users = [
User(1, 'user1', 'abcxyz'),
]
username_table = {u.username: u for u in users}
userid_table = {u.id: u for u in users}
def authenticate(username, password):
user = username_table.get(username, None)
print(username) # user1
print(password) # abcxyz
print(username_table) #{('user1',): <user.User object at 0x000001AD37543358>}
print(user) # None
if user and safe_str_cmp(user.password.encode('utf-8'), password.encode('utf-8')):
return user
def identity(payload):
user_id = payload['identity']
return userid_table.get(user_id, None)
As you can see I was trying to debug with printing out everything. Inside authentication function I got username and password successfully from the request. But In result the user variable is None. Looks like .get method is not working and that is causing the error but I can not figure out why am I getting None there. I have showed the outputs in front of print functions
I think you have a typo in your program. Please remove the ,
after the username in __init__
of the User
class. That is syntactically correct and way of creating a tuple with a single element which is not what you need.
>>> tuple_with_single_element = "python",
>>> tuple_with_single_element
... ('python',)
>>> python_string = "python"
>>> python_string
... 'python'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.