-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.py
51 lines (41 loc) · 1.55 KB
/
backend.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
# app.py
from flask import Flask, request, jsonify
from flask_mysqldb import MySQL
from flask_bcrypt import Bcrypt
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root' # Replace with your MySQL username
app.config['MYSQL_PASSWORD'] = '' # Replace with your MySQL password
app.config['MYSQL_DB'] = 'nark' # Replace with your database name
mysql = MySQL(app)
bcrypt = Bcrypt(app)
# Route to handle user registration
@app.route('/loginSignUp', methods=['POST'])
def register():
data = request.get_json()
email = data.get('email')
password = data.get('password')
hashed_password = bcrypt.generate_password_hash(password).decode('utf-8')
cur = mysql.connection.cursor()
cur.execute("INSERT INTO users (email, password) VALUES (%s, %s)", (email, hashed_password))
mysql.connection.commit()
cur.close()
return jsonify({'message': 'User registered successfully'}), 201
# Route to handle user login
@app.route('/login', methods=['POST'])
def login():
data = request.get_json()
email = data.get('email')
password = data.get('password')
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM users WHERE email = %s", (email,))
user = cur.fetchone()
cur.close()
if user and bcrypt.check_password_hash(user['password'], password):
return jsonify({'message': 'Login successful'}), 200
else:
return jsonify({'message': 'Invalid email or password'}), 401
if __name__ == '__main__':
app.run(debug=True,port=4000)