远兮 commited on
Commit
2c6e1ad
·
1 Parent(s): 860b1b9

添加重置密码

Browse files
Files changed (1) hide show
  1. redis/test_user_redis.py +50 -2
redis/test_user_redis.py CHANGED
@@ -14,8 +14,8 @@ SECERT_KEY = "8U2LL1"
14
  MY_OPENAI_API_KEY = os.environ.get('MY_OPENAI_API_KEY')
15
 
16
  app = Flask(__name__)
17
- redis = Redis(host='192.168.3.229', port=6379, password='lizhen-redis')
18
- # redis = Redis(host='10.254.13.87', port=6379)
19
  # redis = Redis(host='localhost', port=6379)
20
 
21
 
@@ -57,6 +57,9 @@ def register():
57
  password = request.json.get('password')
58
  verification_code = request.json.get('verification_code')
59
 
 
 
 
60
  # 检查验证码是否匹配
61
  stored_code = redis.get(email)
62
  if stored_code is None or verification_code != stored_code.decode('utf-8'):
@@ -78,6 +81,9 @@ def register():
78
  }
79
  redis.hset('users', username, json.dumps(user_data))
80
 
 
 
 
81
  return jsonify({
82
  'code': 0,
83
  'message': 'Registration successful'
@@ -123,6 +129,26 @@ def protected():
123
  return jsonify({'code': 401, 'message': 'Unauthorized'})
124
 
125
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
  # 用户注销
127
  @app.route('/logout', methods=['POST'])
128
  def logout():
@@ -402,5 +428,27 @@ def exceeded_chat_limit(user_id, package, model):
402
  return False
403
 
404
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
405
  if __name__ == '__main__':
406
  app.run(debug=True)
 
14
  MY_OPENAI_API_KEY = os.environ.get('MY_OPENAI_API_KEY')
15
 
16
  app = Flask(__name__)
17
+ # redis = Redis(host='192.168.3.229', port=6379, password='lizhen-redis')
18
+ redis = Redis(host='10.254.13.87', port=6379)
19
  # redis = Redis(host='localhost', port=6379)
20
 
21
 
 
57
  password = request.json.get('password')
58
  verification_code = request.json.get('verification_code')
59
 
60
+ if is_email_registered(email):
61
+ return jsonify({'code': 400, 'message': '邮箱已被注册'})
62
+
63
  # 检查验证码是否匹配
64
  stored_code = redis.get(email)
65
  if stored_code is None or verification_code != stored_code.decode('utf-8'):
 
81
  }
82
  redis.hset('users', username, json.dumps(user_data))
83
 
84
+ # 清除验证码
85
+ redis.delete(email)
86
+
87
  return jsonify({
88
  'code': 0,
89
  'message': 'Registration successful'
 
129
  return jsonify({'code': 401, 'message': 'Unauthorized'})
130
 
131
 
132
+ @app.route('/resetPassword', methods=['POST'])
133
+ def reset_password():
134
+ email = request.json.get('email')
135
+ verification_code = request.json.get('verification_code')
136
+ new_password = request.json.get('new_password')
137
+ update_password(email, new_password)
138
+
139
+ if not is_email_registered(email):
140
+ return jsonify({'code': 400, 'message': '邮箱未注册'})
141
+
142
+ # 检查验证码是否匹配
143
+ stored_code = redis.get(email)
144
+ if stored_code is None or verification_code != stored_code.decode('utf-8'):
145
+ return jsonify({'code': 400, 'message': 'Invalid verification code'})
146
+
147
+ update_password(email, new_password)
148
+ redis.delete(email)
149
+ return jsonify({'code': 0, 'message': '密码已更新'})
150
+
151
+
152
  # 用户注销
153
  @app.route('/logout', methods=['POST'])
154
  def logout():
 
428
  return False
429
 
430
 
431
+ # 检查邮箱是否已注册
432
+ def is_email_registered(email):
433
+ # 遍历所有用户数据
434
+ for username in redis.hkeys('users'):
435
+ user_data = json.loads(redis.hget('users', username).decode('utf-8'))
436
+ if user_data['email'] == email:
437
+ return True
438
+ return False
439
+
440
+
441
+ # 更新用户密码
442
+ def update_password(email, new_password):
443
+ # 遍历所有用户数据
444
+ for username in redis.hkeys('users'):
445
+ user_data = json.loads(redis.hget('users', username).decode('utf-8'))
446
+ if user_data['email'] == email:
447
+ user_data['password'] = new_password
448
+ redis.hset('users', username, json.dumps(user_data))
449
+ return True
450
+ return False
451
+
452
+
453
  if __name__ == '__main__':
454
  app.run(debug=True)