def login(): if request.method == 'POST': if not request.json: return create_error('Bad Request', 400, ['no json sent']), 400 username = request.json.get('username') password = request.json.get('password') error = None if username is None or password is None: error = 'missing either username, password or both' elif not isinstance(username, str) or not isinstance(password, str): error = 'username or password are not string, or both' if error is None: relatedUser = User.query.filter_by(username=username).first() if relatedUser is not None and verify_password( relatedUser.password, password): existingToken = Token.query.filter_by( user_id=relatedUser.id).first() if existingToken is not None: return { 'message': 'OK', 'data': existingToken.as_dict() }, 200 else: newToken = Token(code=token_hex(16), user_id=relatedUser.id) db.session.add(newToken) db.session.commit() return {'message': 'OK', 'data': newToken.as_dict()}, 201 else: error = 'user referenced with this username and password does not exist' return create_error('Bad Request', 400, [error]), 400
def login(): loginData = request.get_json('loginData') email = loginData['email'] password = loginData['password'] print(email, password) user = db.session.query(User).filter(User.email == email).first() if user is not None and user.check_password(password): print("correct email and password") token_query = Token.query.filter_by(user_id=user.id) try: token = token_query.one() except NoResultFound: token = Token(user_id=user.id, uuid=str(uuid.uuid4().hex)) db.session.add(token) db.session.commit() return jsonify({ "loggedIn": True, "token": token.uuid, "user_id": token.user_id }) else: print("wrong username or password") return jsonify({"error": "The email or password entered is incorrect"}) return jsonify({'loginData': loginData})
def login(): if request.method == 'POST': data = request.get_json() frontend_email = data['email'] frontend_password = data['password'] check_email = Member.query.filter_by(email=frontend_email).first() if check_email: # neu co email if check_email.check_password(frontend_password): token = Token.query.filter_by(user_id=check_email.id).first() if not token: token = Token(user_id=check_email.id, uuid=str(uuid.uuid4().hex)) db.session.add(token) db.session.commit() login_user(check_email) return jsonify({ "success": True, "user": { "name": check_email.name, "email": check_email.email, "id": check_email.id, }, 'token': token.uuid }) else: return jsonify({"success": False}) else: return jsonify({"success": False}) return jsonify({"success": False})
def facebook_logged_in(blueprint, token): if not token: flash("Failed to log in.", category="error") return False resp = blueprint.session.get("/me") if not resp.ok: msg = "Failed to fetch user info." flash(msg, category="error") return False info = resp.json() user_id = info["id"] print('info', info) # Find this OAuth token in the database, or create it query = OAuth.query.filter_by(provider=blueprint.name, provider_user_id=user_id) try: oauth = query.one() except NoResultFound: oauth = OAuth(provider=blueprint.name, provider_user_id=user_id, token=token) if oauth.user: login_user(oauth.user) flash("Successfully signed in.") else: # Create a new local user account for this user user = Member(name=info["name"]) # Associate the new local user account with the OAuth token oauth.user = user # Save and commit our database models db.session.add_all([user, oauth]) db.session.commit() # Log in the new local user account login_user(user) flash("Successfully signed in.") # Disable Flask-Dance's default behavior for saving the OAuth token token_query = Token.query.filter_by(user_id=current_user.id) try: token = token_query.one() except NoResultFound: token = Token(user_id=current_user.id, uuid=str(uuid.uuid4().hex)) db.session.add(token) db.session.commit() return redirect("http://localhost:3000/?api_key={}".format(token.uuid))
def login(): check_user = User.query.filter_by(email=request.get_json()['email']).first() if request.method == "POST": if check_user: if check_user.check_password(request.get_json()['password']): token = Token.query.filter_by(user_id = check_user.id).first() if not token: token = Token(user_id= check_user.id, uuid=str(uuid.uuid4().hex)) db.session.add(token) db.session.commit() login_user(check_user) return jsonify({"user": check_user.username, "token" : token.uuid , "state" : "success", "name" : check_user.username, "role" : check_user.role}) return jsonify({"state": "WrongPass"}) return jsonify({"state" : "no_user"})
def adduser(): #get form input and check if username is already in database data = request.get_json() print(data) email = data['email'] password = data['password'] firstname = data['firstname'] lastname = data['lastname'] storename = data['storename'] print(email) print(password) signup_user = db.session.query(User).filter(User.email == email).first() # Add user to database if signup_user is None: #add email and password to db print("user email is not in db") add_user = User(email=email, firstname=firstname, lastname=lastname, storename=storename) add_user.set_password(password) db.session.add(add_user) db.session.commit() #create token token = Token(user_id=add_user.id, uuid=str(uuid.uuid4().hex)) db.session.add(token) db.session.commit() #send email print("these users were added to db", add_user.email, add_user.firstname, add_user.lastname, add_user.storename) return jsonify({"Account_Created": True}) else: print("user is already in database") return jsonify({'Account_Created': False}) return jsonify({'data': data})
def parse_token(self, raw_token): return Token(name=raw_token['name'], symbol=raw_token['symbol'], original_symbol=raw_token['original_symbol'], total_supply=Decimal(raw_token['total_supply']), owner=raw_token['owner'])