def get_auth_token(): refresh_token = request.cookies.get('refresh_token') print(refresh_token) if not refresh_token: raise InvalidUsage("No refresh token", 404) user = User.verify_auth_token(refresh_token) access_token = user.generate_auth_token(expiration=300) return jsonify({'token': access_token, 'user': user_schema.dumps(user)})
def verify_email(token): user = User.verify_email_token(token) if not user: flash('This is an invalid validate request.', 'warning') return redirect(url_for('main.home')) user.email_validated = True db.session.commit() flash(f'The email account {user.email} has been validated!', 'success') return redirect(url_for('main.home'))
def create_guest_user(): guest_username = secrets.token_urlsafe(8) guest_password = secrets.token_urlsafe(8) guest_email = secrets.token_urlsafe(8) guest_user = User(username=guest_username, password=guest_password, email=guest_email, temporary=True) db.session.add(guest_user) db.session.commit() return guest_user
def verify_email(): print("verifying email") token = request.args.get("token") print(token) print("args", request.args) if not token: raise InvalidUsage("No token received! Did you put it in the url?") user = User.verify_auth_token(token) if not user: raise InvalidUsage("Unable to get user from token.") user.email_validated = True db.session.commit() return jsonify(user_schema.dump(user))
def reset_token(token): if current_user.is_authenticated: return redirect(url_for('main.home')) user = User.verify_reset_token(token) if not user: flash('That is an invalid or expired token.', 'warning') return redirect(url_for('account.reset_request')) reset_form = ResetPasswordForm() if reset_form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash(reset_form.password.data).decode('utf-8') user.password = hashed_password db.session.commit() flash('Your password has been updated!', 'success') return redirect(url_for('account.login')) return render_template('reset_token.html', reset_form=reset_form)
def register(): register_form = RegistrationForm() if register_form.validate_on_submit(): print('here') hashed_password = bcrypt.generate_password_hash(register_form.password.data).decode('utf-8') if not current_user.is_authenticated: user = User(username=register_form.username.data, email=register_form.email.data, password=hashed_password) db.session.add(user) else: user = User.query.get(current_user.id) user.username = register_form.username.data user.email = register_form.email.data user.password = hashed_password user.temporary = False try: db.session.commit() flash("Account created successfully!", "success") send_validate_email(user) except exc.IntegrityError as error: db.session.rollback() print(error.args) flash('Error. Username or email is already in use. Please choose a new one.', 'danger') return render_template('register.html', register_form=register_form) return redirect(url_for('account.login')) if current_user.is_authenticated: if current_user.temporary: guest_list = CompiledList.query.filter_by(user_id=current_user.id).first() return render_template('register.html', register_form=register_form, guest_list=guest_list) else: return redirect(url_for('main.home')) return render_template('register.html', register_form=register_form)
def return_new_user(self, data, **kwargs): data["hashed_password"] = data.pop( "password") # prevent typeerror when creating User new_user = User(**data) new_user.hash_password(new_user.hashed_password) return new_user
def return_user(self, data, **kwargs): existing_user = User.query.filter_by(email=data["email"]).first() if existing_user: return existing_user else: return User(**data)