def register(): # FUNCTIONALITY OF USER REGISTER form = RegistrationForm(request.form) if form.validate_on_submit(): user = mongo.db.users existing_user = user.find_one({'username': request.form['username']}) if existing_user is None: # HASHED PW AND INSERT TO COLL hashed_pw = bcrypt.hashpw(request.form['password'].encode('utf-8'), bcrypt.gensalt()) mongo.db.users.insertOne({ 'username': request.form['username'], 'email': request.form['email'], 'password': hashed_pw }) session['username'] = request.form['username'] # IF REGISTER IS SUCCESSFUL flash( f'Account created for {form.username.data}.. now just to login', 'success') return redirect(url_for('login')) #TODO CHANGE TO USER PROFILE else: flash(f'Registration Failed. Check yo\'self, please try again!', 'danger') return render_template('register.html', title='Register', form=form)
def hello_world(): form = RegistrationForm(request.form) if request.form.get('signup'): form = RegistrationForm(request.form) if request.method == 'POST' and form.validate(): userdet = {} for i in form: userdet[i.name] = i.data cur.execute( f"Select 1 from users where username = '******'username']}'" ) if len(cur.fetchall()) > 0: flash('Username already taken! Choose another', 'danger') return render_template('login.html', pageType=str(request.url_rule), form=form) userdet['password'] = bcrypt.generate_password_hash( userdet['password']).decode('utf-8') cur.execute( f"insert into users(fname, lname, email_id, username, password, contact_no) values('{userdet['fname']}', '{userdet['lname']}', '{userdet['email']}', '{userdet['username']}', '{userdet['password']}', {userdet['phno']})" ) conn.commit() session['user'] = userdet flash('Registered successfully!', 'success') elif not form.validate(): print(":((((((((()))))))))") flash('Please enter details properly', 'danger') # return render_template('login.html', pageType = str(request.url_rule), form = form) else: form1 = LoginForm(request.form) if request.method == 'POST' and form1.validate(): print('abcd') u = request.form.get('username') p = request.form.get('password') print(u, p) print( f"Select * from users where username='******' and password='******'utf-8')}'" ) cur.execute(f"Select * from users where username='******'") ulist = cur.fetchall() # print(ulist) if len(ulist) > 0: if bcrypt.check_password_hash(ulist[0][5], p): flash('Login successful!', 'success') session['user'] = list(ulist[0]) for i in range(len(session['user'])): session['user'][i] = str(session['user'][i]) session['user'] = dict(zip(user_col, session['user'])) return redirect(url_for('new_homepage')) else: flash( 'Could not find matching username or password. Please check your login credentials.', 'danger') else: flash( 'Could not find matching username or password. Please check your login credentials.', 'danger') print('wxyz', request.url_rule, type(request.url_rule)) return render_template('login.html', pageType=str(request.args.get('type')), form=form)