raj-tomar001 commited on
Commit
74ece7a
·
verified ·
1 Parent(s): cb2d92d

Update login.py

Browse files
Files changed (1) hide show
  1. login.py +69 -69
login.py CHANGED
@@ -1,70 +1,70 @@
1
- from flask import request, jsonify
2
- from models import get_user_by_email, bcrypt
3
- import logging
4
- from flask_jwt_extended import create_access_token
5
- from datetime import timedelta
6
-
7
- import traceback
8
-
9
- logging.basicConfig(
10
- filename='app.log',
11
- level=logging.DEBUG,
12
- format='%(asctime)s - %(levelname)s - %(message)s'
13
- )
14
-
15
- def login_route():
16
- try:
17
- if request.method == 'POST':
18
-
19
- if request.is_json:
20
- data = request.get_json()
21
- email = data.get('email')
22
- password = data.get('password')
23
- else:
24
- email = request.form.get('email')
25
- password = request.form.get('password')
26
-
27
- logging.info("Attempted login with Email: %s", email)
28
-
29
-
30
-
31
-
32
-
33
- if not email or not password:
34
- logging.warning("Missing email or password for login attempt.")
35
- return jsonify({'message': 'Please enter both email and password.', 'status': 'danger'}), 400
36
-
37
- try:
38
- user = get_user_by_email(email)
39
- logging.info("User found: %s", user)
40
-
41
- if not user:
42
- logging.warning("No account found with this email: %s", email)
43
- return jsonify({'message': 'No account found with this email', 'status': 'danger'}), 400
44
-
45
- if not bcrypt.check_password_hash(user['password'], password):
46
- logging.warning("Incorrect password attempt for email: %s", email)
47
- return jsonify({'message': 'Incorrect password. Please try again.', 'status': 'danger'}), 400
48
-
49
- except Exception as db_error:
50
- logging.error("Error retrieving user from database: %s", traceback.format_exc())
51
- return jsonify({'message': 'Internal server error while processing login.', 'status': 'danger'}), 500
52
-
53
- try:
54
- expires = timedelta(minutes=600)
55
- access_token = create_access_token(identity=user['id'],expires_delta=expires)
56
-
57
- except Exception as token_error:
58
- logging.error("Error creating access token: %s", traceback.format_exc())
59
- return jsonify({'message': 'Internal server error while generating token.', 'status': 'danger'}), 500
60
-
61
- logging.info("Login successful for user: %s", user['username'])
62
- return jsonify({'token': access_token, 'message': f'Welcome, {user["username"]}!', 'status': 'success'}), 200
63
-
64
-
65
- logging.error("Method not allowed: %s", request.method)
66
- return jsonify({'message': 'Method not allowed', 'status': 'danger'}), 405
67
-
68
- except Exception as e:
69
- logging.error("Unexpected error in login: %s", traceback.format_exc())
70
  return jsonify({'message': 'Internal server error', 'status': 'danger'}), 500
 
1
+ from flask import request, jsonify
2
+ from models import get_user_by_email, bcrypt
3
+ import logging
4
+ from flask_jwt_extended import create_access_token
5
+ from datetime import timedelta
6
+ import os
7
+ import traceback
8
+
9
+ logging.basicConfig(
10
+ filename=os.path.join('/tmp', 'app.log'),
11
+ level=logging.DEBUG,
12
+ format='%(asctime)s - %(levelname)s - %(message)s'
13
+ )
14
+
15
+ def login_route():
16
+ try:
17
+ if request.method == 'POST':
18
+
19
+ if request.is_json:
20
+ data = request.get_json()
21
+ email = data.get('email')
22
+ password = data.get('password')
23
+ else:
24
+ email = request.form.get('email')
25
+ password = request.form.get('password')
26
+
27
+ logging.info("Attempted login with Email: %s", email)
28
+
29
+
30
+
31
+
32
+
33
+ if not email or not password:
34
+ logging.warning("Missing email or password for login attempt.")
35
+ return jsonify({'message': 'Please enter both email and password.', 'status': 'danger'}), 400
36
+
37
+ try:
38
+ user = get_user_by_email(email)
39
+ logging.info("User found: %s", user)
40
+
41
+ if not user:
42
+ logging.warning("No account found with this email: %s", email)
43
+ return jsonify({'message': 'No account found with this email', 'status': 'danger'}), 400
44
+
45
+ if not bcrypt.check_password_hash(user['password'], password):
46
+ logging.warning("Incorrect password attempt for email: %s", email)
47
+ return jsonify({'message': 'Incorrect password. Please try again.', 'status': 'danger'}), 400
48
+
49
+ except Exception as db_error:
50
+ logging.error("Error retrieving user from database: %s", traceback.format_exc())
51
+ return jsonify({'message': 'Internal server error while processing login.', 'status': 'danger'}), 500
52
+
53
+ try:
54
+ expires = timedelta(minutes=600)
55
+ access_token = create_access_token(identity=user['id'],expires_delta=expires)
56
+
57
+ except Exception as token_error:
58
+ logging.error("Error creating access token: %s", traceback.format_exc())
59
+ return jsonify({'message': 'Internal server error while generating token.', 'status': 'danger'}), 500
60
+
61
+ logging.info("Login successful for user: %s", user['username'])
62
+ return jsonify({'token': access_token, 'message': f'Welcome, {user["username"]}!', 'status': 'success'}), 200
63
+
64
+
65
+ logging.error("Method not allowed: %s", request.method)
66
+ return jsonify({'message': 'Method not allowed', 'status': 'danger'}), 405
67
+
68
+ except Exception as e:
69
+ logging.error("Unexpected error in login: %s", traceback.format_exc())
70
  return jsonify({'message': 'Internal server error', 'status': 'danger'}), 500