-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
105 lines (76 loc) · 2.79 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# Import necessary modules and classes from Flask
from flask import Flask, jsonify, request, render_template, redirect
from flask_login import LoginManager, login_user, login_required, logout_user, UserMixin
from flask_wtf.csrf import CSRFProtect
from flask_cors import CORS
# Import forms, helper functions, and data storage modules
from forms import RegistrationForm, LoginForm
from helpers import hash_password, check_password
from data_storage import load_users, save_users
# Initialize the Flask app
app = Flask(__name__, template_folder="")
app.secret_key = "ishanoshada"
# Enable Cross-Origin Resource Sharing (CORS) for API requests
CORS(app)
# Enable Cross-Site Request Forgery (CSRF) protection
csrf = CSRFProtect(app)
# Load user data from storage
users = load_users()
# Configure Flask-Login
login_manager = LoginManager(app)
login_manager.login_view = 'login'
# Define a User class for Flask-Login
class User(UserMixin):
pass
# Callback function to load user by ID
@login_manager.user_loader
def load_user(user_id):
for user in users:
if user.get('user_id') == user_id:
return user
return None
# Route for user registration
@app.route('/signup', methods=['POST'])
def signup():
form = RegistrationForm()
if form.validate_on_submit():
username = form.username.data
password = form.password.data
# Hash the password before saving it
hashed_password = hash_password(password)
# Append user data to the list and save it
users.append({'id': len(users) + 1, 'username': username, 'password': hashed_password})
save_users(users)
return jsonify({'message': 'User created successfully'}), 201
return jsonify({'message': 'Invalid input data'}), 400
# Route for user login
@app.route('/login', methods=['POST'])
def login():
data = request.form
username = data.get('username')
password = data.get('password')
for user in users:
if user['username'] == username and check_password(user['password'], password):
user_obj = User()
user_obj.id = user['id']
login_user(user_obj)
return jsonify({'message': 'Login successful'})
return jsonify({'message': 'Invalid credentials'}), 401
# Route for user logout
@app.route('/logout')
@login_required
def logout():
logout_user()
return jsonify({'message': 'Logged out successfully'})
# Route for the home page
@app.route('/')
def index():
# Initialize registration and login forms
rform = RegistrationForm()
lform = LoginForm()
# Render the index.html template with the forms
return render_template('index.html', registration_form=rform, login_form=lform, template_folder="templates")
# Initialize CSRF protection
csrf.init_app(app)
# Start the Flask application
app.run()