def post(self): try: body = request.get_json() if not body: return make_response(jsonify(status='Failed', token=None), 400) user_id = body.get('username') password = body.get('password') if not user_id or not password: return make_response(jsonify(status='Failed', token=None), 400) user = db.session.query( models.User).filter_by(user_id=user_id).first() if not user: return make_response( jsonify(status='Failed', token=None, msg='User Does not Exist'), 400) if not util.is_valid_password(password, user.password): return make_response( jsonify(status='Failed', token=None, msg='Invalid Password'), 400) auth_token = util.encode_auth_token(user_id) return util.get_response_with_cookie( { 'status': 'Success', 'token': auth_token }, 'auth_token', auth_token) except Exception as e: return make_response(jsonify(status='Failed', token=None), 500)
def post(self): try: body = request.get_json() if not body: return make_response(jsonify(status='Failed', token=None), 400) user_id = body.get('username') password = body.get('password') email = body.get('email') if not user_id or not password: return make_response(jsonify(status='Failed', token=None), 400) user_exists = bool( db.session.query( models.User).filter_by(user_id=user_id).first()) if user_exists: return make_response( jsonify(status='Failed', token=None, msg='User Already Exists!'), 400) password = util.encrypt_password(password) auth_token = util.encode_auth_token(user_id) user = models.User(user_id=user_id, password=password, email=email) db.session.add(user) db.session.commit() return util.get_response_with_cookie( { 'status': 'Success', 'token': auth_token }, 'auth_token', auth_token) except Exception as e: db.session.rollback() return make_response(jsonify(status='Failed', token=None), 500)
def signup(self, name, email, password): try: cursor = self.__db.cursor() self.__db.start_transaction() sql = "insert into users (name, email, password) values (%s, %s, %s)" ciphered_password = self.encrypt(password) values = (name, email, ciphered_password) cursor.execute(sql, values) user_id = cursor.lastrowid token = encode_auth_token(user_id) sql = "update users set token = %s WHERE id=%s" values = (token, user_id) cursor.execute(sql, values) self.__db.commit() user = { "id": user_id, "name": name, "email": email, "token": token } self._put_in_cache("user_" + str(user_id), user) return user except mysql.connector.Error as error: print("Failed to insert horse record to database rollback: {}". format(error)) self.__db.rollback() # reverting changes because of exception finally: cursor.close()
def post(self): # try: body = request.get_json() if not body: return make_response(jsonify(status='Failed', token=None), 400) user_id = body.get('username') password = body.get('password') email = body.get('email') if not user_id or not password: return make_response(jsonify(status='Failed', token=None), 400) user_exists = bool(db.session.query(models.User).filter_by(user_id=user_id).first()) if user_exists: return make_response(jsonify(status='Failed', token=None, msg='User Already Exists!'), 400) password = util.encrypt_password(password) auth_token = util.encode_auth_token(user_id) user = models.User(user_id=user_id, password=password, email=email) db.session.add(user) db.session.commit() user_key_dict = { 'user_id': user_id } user_value_dict = { 'user_id': user_id, 'password': password, 'email': email or '', 'images': json.dumps([]) } # ap.users_produce_to_kafka(user_key_dict, user_value_dict) return util.get_response_with_cookie({'status': 'Success', 'token': auth_token}, 'auth_token', auth_token)
def register(): data = request.get_json(force=True) users = client.CardDeck.users if users.find_one({'email': data['email']}): return response('USER_EXISTS', 400) password = bcrypt.generate_password_hash( data['password'], BCRYPT_LOG_ROUNDS ).decode() user = users.insert_one( {'name': data['name'], 'email': data['email'], 'password': password}) auth_token = util.encode_auth_token(str(user.inserted_id)) return jsonify({'token': auth_token.decode(), 'name': data['name']})
def login(): global players_list users = client.CardDeck.users data = request.get_json(force=True) user = users.find_one({'email': data['email']}) if players_list.count(user['name']) > 0: return response('ALREADY_AUTHENTICATED', 400) if not user: return response('EMAIL_NOT_FOUND', 400) if not bcrypt.check_password_hash(user['password'], data['password']): return response('INVALID_PASSWORD', 400) auth_token = util.encode_auth_token(str(user['_id'])) return jsonify({'token': auth_token.decode(), 'name': user['name']})
def login(self, email, password): try: teacher = self.__teachers.find_one({"email": email}) stored_password = teacher["password"] decrypted_password = self.decrypt(stored_password) if decrypted_password != password: return None teacher["_id"] = str(teacher["_id"]) token = encode_auth_token(teacher["_id"]) self.__teachers.update_one({"_id": ObjectId(teacher["_id"])}, {"$set": { "token": token }}) return {"token": token, "_id": teacher["_id"]} except Exception as e: raise e
def sign_up(self, name, email, password): try: encrypted_password = self.encrypt(password) teacher_id = self.__teachers.insert_one({ "name": name, "email": email, "password": encrypted_password }).inserted_id teacher_id = str(teacher_id) token = encode_auth_token(teacher_id) self.__teachers.update_one({"_id": ObjectId(teacher_id)}, {"$set": { "token": token }}) return {"token": token, "_id": teacher_id} except Exception as e: raise e
def login(self, email, password): try: cursor = self.__db.cursor() # self.__db.start_transaction() sql = "select id, name, email, password from users where email=%s" values = (email, ) cursor.execute(sql, values) record = cursor.fetchone() if record is None: return None decrypted_password = self.decrypt(record[3]) if decrypted_password != password: return None token = encode_auth_token(record[0]) sql = "update users set token = %s WHERE id=%s" values = (token, record[0]) cursor.execute(sql, values) self.__db.commit() user_dict = { "id": record[0], "name": record[1], "email": record[2], "token": token } self._put_in_cache("user_" + str(record[0]), user_dict) return user_dict except mysql.connector.Error as error: print( "Failed to fetch user record to database rollback: {}".format( error)) self.__db.rollback() # reverting changes because of exception finally: cursor.close()