-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
87 lines (69 loc) · 2.86 KB
/
app.py
File metadata and controls
87 lines (69 loc) · 2.86 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from flask import Flask, request, jsonify, render_template
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
import sqlite3
import bcrypt
import re
app = Flask(__name__)
limiter = Limiter(
app=app,
key_func=get_remote_address,
default_limits=["100 per minute"]
)
class Database:
def __init__(self, db_name):
self.conn = sqlite3.connect(db_name, check_same_thread=False)
self.cursor = self.conn.cursor()
self.create_table()
def create_table(self):
self.cursor.execute('''
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
email TEXT NOT NULL UNIQUE,
password TEXT NOT NULL,
fullName TEXT NOT NULL
);
''')
self.conn.commit()
def get_user_by_email(self, email):
self.cursor.execute('SELECT * FROM users WHERE email = ?', (email,))
return self.cursor.fetchone()
def insert_user(self, email, password, fullName):
self.cursor.execute('INSERT INTO users (email, password, fullName) VALUES (?, ?, ?)', (email, password, fullName))
self.conn.commit()
return self.cursor.lastrowid
def close(self):
self.conn.close()
db = Database('database.db')
def validate_email(email):
pattern = r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$'
return re.match(pattern, email) is not None
def validate_password(password):
return len(password) >= 8 and any(c.isdigit() for c in password) and any(not c.isalnum() for c in password)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/api/v1/users/register', methods=['POST'])
@limiter.limit("100 per minute")
def register_user():
try:
data = request.get_json()
email = data.get('email')
password = data.get('password')
fullName = data.get('fullName')
if not email or not password or not fullName:
return jsonify({'message': 'Missing required fields'}), 400
if not validate_email(email):
return jsonify({'message': 'Invalid email'}), 400
if not validate_password(password):
return jsonify({'message': 'Password should be at least 8 characters long and contain at least one number and one special character'}), 400
existing_user = db.get_user_by_email(email)
if existing_user:
return jsonify({'message': 'Email already exists'}), 400
hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
user_id = db.insert_user(email, hashed_password, fullName)
return jsonify({'message': 'User registered successfully', 'user_id': user_id}), 200
except Exception as e:
return jsonify({'message': str(e)}), 500
if __name__ == '__main__':
app.run(host="0.0.0.0",port=5000,debug=True)