def sign_up(): data = request.json # function to check sign-up and handle errors errors = validate_sign_up(data) if (len(errors) > 0): return jsonify({'validated': False, 'errors': errors}) # Create a hashed password password = data['password'].encode() hashed_password = bcrypt.hashpw( password, bcrypt.gensalt(14)).decode('utf-8') # Generate and add new user to db new_user = User(name=data['name'], email=data['email'], hashed_password=hashed_password) db.session() db.session.add(new_user) db.session.commit() # create jwt to return jwt = create_jwt(new_user.to_dict()) return jsonify({'validated': True, "user": new_user.to_dict(), "token": str(jwt)})
def login(): data = request.json user = User.query.filter(User.email == data['email']).first() # print(user) hashed_password = user.hashed_password if bcrypt.checkpw(data['password'].encode(), hashed_password.encode()): print(user) user_data = user.to_dict() print(user_data) jwt = create_jwt(user_data) return jsonify({"user": user_data, "token": str(jwt)}) else: return jsonify('Bad Login')
def signup(): data = request.json if not data: return ('You are here: Failed Request') # Create a hashed password password = data['password'].encode() hashed_password = bcrypt.hashpw(password, bcrypt.gensalt(14)).decode('utf-8') # Generate and add new user to db new_user = User(username=data['userName'], email=data['email'], hashed_password=hashed_password) db.session() db.session.add(new_user) db.session.commit() # get user and create jwt to return user = User.query.filter(User.email == data['email']).first().to_dict() jwt = create_jwt(user) # only commit the new user after everything passes with no errors return jsonify({"user": user, "token": str(jwt)})
from app.auth import create_jwt print(create_jwt({"Some information": "NakNakNak"}))