python - How to make a simple login without a database and without a form -
i want add simple login feature 1 can either logged in or not logged in (no user-specific data needs stored).
i made user class:
class user(object): def __init__(self, username, hash): self.name = username self.hash = hash
and load list of users ini file.
i implemented user_loader function
@self.login_manager.user_loader def load_user(userid): user in self.users: if user.name == userid return user return none
what goes in login method?
@self.server.route("api/login", methods=["get", "post"]) def login():
i want login ajax request send credentials , json response contains username or error message.
simple example, work post /api/login
username=test_user&hash=test_hash
data or get /api/login?username=test_user&hash=test_hash
:
class user(usermixin): def __init__(self, username, hash): self.name = username self.hash = hash @property def id(self): return self.name @self.server.route('/api/login', methods=['get', 'post']) def login(): user = load_user(request.values.get('username')) if user , user.hash == request.values.get('hash'): login_user(user) return jsonify(status='ok', username=user.username) else: return jsonify(status='error', message='wrong username or hash')
however prefer post http method login.
really own implementation dependent js call, example can use request.get_json
instead post request.form
or request.args
. , login algorithm (credentials fields).
also load_user
can slow big amount of users
.
Comments
Post a Comment