def register(): login_form = LoginForm(request.form) create_account_form = CreateAccountForm(request.form) if 'register' in request.form: username = request.form['username'] email = request.form['email'] # Check usename exists user = User.query.filter_by(username=username).first() if user: return render_template('accounts/register.html', msg='Username already registered', success=False, form=create_account_form) # Check email exists user = User.query.filter_by(email=email).first() if user: return render_template('accounts/register.html', msg='Email already registered', success=False, form=create_account_form) # else we can create the user user = User(**request.form) db.session.add(user) db.session.commit() return render_template( 'accounts/register.html', msg='User created please <a href="/login">login</a>', success=True, form=create_account_form) else: return render_template('accounts/register.html', form=create_account_form)
def login(): login_form = LoginForm(request.form) if 'login' in request.form: import requests # read form data username = request.form['username'] password = request.form['password'] # Locate user user = requests.get( f"http://{app.config['IP']}:{app.config['PORT']}/AuthenticateUser", params={ "username": username, "password": password }, headers={ "Authorization": f"{app.config['APIKEY']}" }).json() # Check the password if user: user = User(uid=user["uid"]) db.session.add(user) db.session.commit() login_user(user) z = current_user url = url_for('base_blueprint.route_default') new_redr = redirect(url) return new_redr # Something (user or pass) is not ok return render_template('login/login.html', msg='Wrong user or password', form=login_form) z = current_user if not current_user.is_authenticated: return render_template('login/login.html', form=login_form) return redirect(url_for('home_blueprint.index'))
def register(): token = request.args.get('token', '') try: d = jwt.decode(token, options={"verify_signature": False}) # d = jwt.decode(token, config('SECRET_KEY'), algorithms=["HS256"]) verified = True except: flash('Invalid registration token.', 'danger') return redirect(url_for('base_blueprint.login')) create_account_form = forms.CreateAccountForm(request.form) if 'register' in request.form: if verified != True: flash('Invalid registration token', 'danger') return render_template( 'accounts/register.html', success=False, form=create_account_form) username = request.form['username'] # Check usename exists user = User.query.filter_by(username=username).first() if user: flash('Username already registered', 'danger') return render_template( 'accounts/register.html', success=False, form=create_account_form) # else we can create the user user = User(**request.form) db.session.add(user) db.session.commit() flash('Account successfully created. You can login now.', 'success') return redirect(url_for('base_blueprint.login')) else: return render_template( 'accounts/register.html', form=create_account_form)
def register(): login_form = LoginForm(request.form) create_account_form = CreateAccountForm(request.form) if 'register' in request.form: username = request.form['username'] email = request.form['email'] password = request.form['password'] # Check usename exists user = User.objects(username=username).first() if user: return render_template('accounts/register.html', msg='Username already registered', success=False, form=create_account_form) # Check email exists user = User.objects(email=email).first() if user: return render_template('accounts/register.html', msg='Email already registered', success=False, form=create_account_form) # else we can create the user user = User(username=username, email=email, password=password) user.save() login_user(user) # return render_template('accounts/register.html', # msg='User created please <a href="/login">login</a>', # success=True, # form=create_account_form) return redirect(url_for('base_blueprint.route_default')) else: return render_template('accounts/register.html', form=create_account_form)
def create_user(): user = User(**request.form) db.session.add(user) db.session.commit() return jsonify('success')
def verify_token(token): return User.check_token(token) if token else None
def create_user(): User.create(**request.form) return jsonify("success")
def make_user(self, data): return User(**data)
def register(): create_account_form = CreateAccountForm(request.form) # -------------------------------------------------------------------------# # POST Request - Valid if create_account_form.validate_on_submit(): user = User.query.filter_by( username=create_account_form.username.data).first() # Check if usename exists if user: flash("Username already registered.", "info") return render_template( "accounts/register.html", msg="Username already registered.", form=create_account_form, ) # Check if email exists user = User.query.filter_by( email=create_account_form.email.data).first() if user: flash("Email already registered.", "info") return render_template( "accounts/register.html", msg="Email already registered.", form=create_account_form, ) # Create user from data user = User(**request.form) # Send an activation email confirmation_token = generate_confirmation_token(user.email) confirm_url = url_for("base_blueprint.confirm_email", token=confirmation_token, _external=True) html = render_template("accounts/activate.html", confirm_url=confirm_url) subject = "Please confirm your email." try: send_email(user.email, subject, html) except SMTPAuthenticationError: flash( "Server-side authentication eror. " + "Was mail properly configured for this app?", "error", ) return redirect(url_for("base_blueprint.login")) # Add the unconfirmed user db.session.add(user) db.session.commit() flash("A confirmation email has been sent.", "success") return redirect(url_for("base_blueprint.route_default")) # GET Request else: return render_template("accounts/register.html", form=create_account_form)