-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.py
68 lines (49 loc) · 2.02 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from flask import Flask, render_template, url_for, request, session, redirect, flash
from flask_pymongo import PyMongo
import bcrypt
app = Flask(__name__)
app.config['SECRET_KEY'] = 'testing'
app.config['MONGO_dbname'] = 'users'
app.config['MONGO_URI'] = 'mongodb://localhost:27017/users'
mongo = PyMongo(app)
@app.route("/")
@app.route("/main")
def main():
return render_template('index.html')
@app.route("/signup", methods=['POST', 'GET'])
def signup():
if request.method == 'POST':
users = mongo.db.users
signup_user = users.find_one({'username': request.form['username']})
if signup:
flash(request.form['username'] + ' username is already exist')
return redirect(url_for('signup'))
hashed = bcrypt.hashpw(request.form['password'].encode('utf-8'), bcrypt.gensalt(14))
users.insert({'username': request.form['username'], 'password': hashed, 'email': request.form['email']})
return redirect(url_for('signin'))
return render_template('signup.html')
@app.route('/index')
def index():
if 'username' in session:
return render_template('index.html', username=session['username'])
return render_template('index.html')
@app.route('/signin', methods=['GET', 'POST'])
def signin():
if request.method == 'POST':
users = mongo.db.users
signin_user = users.find_one({'username': request.form['username']})
if signin_user:
if bcrypt.hashpw(request.form['password'].encode('utf-8'), signin_user['password'].encode('utf-8')) == \
signin_user['password'].encode('utf-8'):
session['username'] = request.form['username']
return redirect(url_for('index'))
flash('Username and password combination is wrong')
return render_template('signin.html')
return render_template('signin.html')
@app.route('/logout')
def logout():
session.pop('username', None)
return redirect(url_for('index'))
if __name__ == "__main__":
app.run(debug=True)
app.run()