Spaces:
Runtime error
Runtime error
远兮
commited on
Commit
·
dd2231b
1
Parent(s):
d674f7c
生成验证码、注册、登录
Browse files- redis/test_user_redis.py +71 -18
redis/test_user_redis.py
CHANGED
@@ -1,38 +1,91 @@
|
|
1 |
-
import
|
|
|
|
|
2 |
from flask import Flask, request, jsonify
|
|
|
3 |
|
4 |
app = Flask(__name__)
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
|
8 |
@app.route('/register', methods=['POST'])
|
9 |
def register():
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
13 |
|
14 |
-
#
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
16 |
return jsonify({'message': 'Username already exists'}), 400
|
17 |
|
18 |
-
#
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
|
22 |
|
23 |
|
24 |
@app.route('/login', methods=['POST'])
|
25 |
def login():
|
26 |
-
|
27 |
-
username =
|
28 |
-
password =
|
29 |
-
|
30 |
-
# 获取保存在 Redis 中的密码
|
31 |
-
saved_password = db.hget(username, 'password')
|
32 |
|
33 |
# 检查用户名和密码是否匹配
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
return jsonify({'message': 'Login successful'})
|
38 |
|
|
|
1 |
+
import json
|
2 |
+
import random
|
3 |
+
import string
|
4 |
from flask import Flask, request, jsonify
|
5 |
+
from redis import Redis
|
6 |
|
7 |
app = Flask(__name__)
|
8 |
+
redis = Redis(host='10.254.13.87', port=6379)
|
9 |
+
|
10 |
+
# 生成验证码
|
11 |
+
|
12 |
+
|
13 |
+
def generate_verification_code():
|
14 |
+
# code = ''.join(random.choices(string.ascii_uppercase + string.digits, k=6))
|
15 |
+
code = ''.join(random.choices(string.digits, k=6))
|
16 |
+
return code
|
17 |
+
|
18 |
+
# 发送验证码到用户邮箱(这里只是模拟发送过程)
|
19 |
+
|
20 |
+
|
21 |
+
def send_verification_code(email, code):
|
22 |
+
print(f'Sending verification code {code} to {email}...')
|
23 |
+
|
24 |
+
# 用户请求发送验证码
|
25 |
+
|
26 |
+
|
27 |
+
@app.route('/send_verification_code', methods=['POST'])
|
28 |
+
def send_verification_code_endpoint():
|
29 |
+
# 从请求中获取邮箱地址
|
30 |
+
email = request.json.get('email')
|
31 |
+
|
32 |
+
# 生成验证码
|
33 |
+
verification_code = generate_verification_code()
|
34 |
+
|
35 |
+
# 发送验证码到用户邮箱
|
36 |
+
send_verification_code(email, verification_code)
|
37 |
+
|
38 |
+
# 保存验证码到Redis,并设置过期时间(例如,5分钟)
|
39 |
+
redis.setex(email, 300, verification_code)
|
40 |
+
|
41 |
+
return jsonify({'message': 'Verification code sent'})
|
42 |
+
|
43 |
+
# 用户注册
|
44 |
|
45 |
|
46 |
@app.route('/register', methods=['POST'])
|
47 |
def register():
|
48 |
+
# 从请求中获取注册信息
|
49 |
+
email = request.json.get('email')
|
50 |
+
username = request.json.get('username')
|
51 |
+
password = request.json.get('password')
|
52 |
+
verification_code = request.json.get('verification_code')
|
53 |
|
54 |
+
# 检查验证码是否匹配
|
55 |
+
stored_code = redis.get(email)
|
56 |
+
if stored_code is None or verification_code != stored_code.decode('utf-8'):
|
57 |
+
return jsonify({'message': 'Invalid verification code'}), 400
|
58 |
+
|
59 |
+
# 检查用户名是否已被注册
|
60 |
+
if redis.hexists('users', username):
|
61 |
return jsonify({'message': 'Username already exists'}), 400
|
62 |
|
63 |
+
# 保存用户信息到Redis
|
64 |
+
user_data = {
|
65 |
+
'email': email,
|
66 |
+
'password': password
|
67 |
+
}
|
68 |
+
redis.hset('users', username, json.dumps(user_data))
|
69 |
+
|
70 |
+
return jsonify({'message': 'Registration successful'})
|
71 |
|
72 |
+
# 用户登录
|
73 |
|
74 |
|
75 |
@app.route('/login', methods=['POST'])
|
76 |
def login():
|
77 |
+
# 从请求中获取登录信息
|
78 |
+
username = request.json.get('username')
|
79 |
+
password = request.json.get('password')
|
|
|
|
|
|
|
80 |
|
81 |
# 检查用户名和密码是否匹配
|
82 |
+
user_data = redis.hget('users', username)
|
83 |
+
if not user_data:
|
84 |
+
return jsonify({'message': 'Invalid username'}), 400
|
85 |
+
|
86 |
+
user_data = user_data.decode('utf-8')
|
87 |
+
if password != eval(user_data)['password']:
|
88 |
+
return jsonify({'message': 'Invalid password'}), 400
|
89 |
|
90 |
return jsonify({'message': 'Login successful'})
|
91 |
|