def sign_up(): data = request.get_json() name = data['name'] dob = data['dob'] phone_no = data['phone_no'] country = data['country'] email = data['email'] password = data['password'] user = Users.query.filter_by(email=email).first() if user: return jsonify({"message": "User already registered"}) else: hashed_password = bcrypt.generate_password_hash(password).decode( 'utf-8') users = Users() users.name = name users.dob = dob users.email = email users.phone_no = phone_no users.country = country users.password = hashed_password try: db.session.add(users) db.session.commit() except: return jsonify({ "status": "error", "message": "Could not add user" }), 401 return jsonify({ "status": "success", "message": "User added successfully" }), 201
def reg(): form = Sign_Up() if form.validate_on_submit(): users = Users() users.name = form.name.data users.password = bcrypt.generate_password_hash( form.password.data).decode('utf-8') users.dob = form.dob.data user = Users.query.filter_by(email=form.email.data).first() if user: return jsonify({"message": "User already registered"}) users.email = form.email.data db.session.add(users) db.session.commit() return redirect(url_for('raw.login_users')) return render_template('sign_up.html', form=form)
def create(self, validated_data): username = validated_data['username'] full_name = validated_data['full_name'] email = validated_data['email'] password = validated_data['password'] img = validated_data['img'] address = validated_data['address'] city_town = validated_data['city_town'] country = validated_data['country'] birthday = validated_data['birthday'] gender = validated_data['gender'] user_obj = Users( username=username, email=email, full_name=full_name, img=img, address=address, city_town=city_town, country=country, birthday=birthday, gender=gender, ) user_obj.set_password(password) user_obj.save() user_qs = Users.objects.get(username=username) user = authenticate(username=username, password=password) payload = jwt_payload_handler(user) validated_data['token'] = jwt_encode_handler(payload) validated_data['id'] = user_qs.id validated_data['password'] = '******' del validated_data['full_name'] del validated_data['address'] del validated_data['city_town'] del validated_data['country'] del validated_data['birthday'] del validated_data['gender'] result = {'token': jwt_encode_handler(payload), 'user': user} return validated_data