-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplan.txt
156 lines (84 loc) · 3.5 KB
/
plan.txt
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/registerFarmer, ✅
/loginFarmer, ✅
/logoutFarmer, ✅
/refreshAccessToken, ✅
/changeCurrentPassword, ✅
/getCurrentUser, ✅
/send-otp
/profile
/search-user
/profile/:id
/addCrop,
/crop-details
todo:
1)
- store the user dets in localstorage after clicking on register/login
- send otp and store the sent otp in session
- after clicking on verify otp, send the user dets with the enteredOtp to the server
- if the otp in session and entered otp is matched then register/login the user else send error
2)
- change farmer and buyer schema according to profile and requirements
3)
- user should be able to edit his profile
- farmer should be able to add crops that he/she want to sell
- buyer should be able to find farmer profiles based on place or crop
- buyer should be able to view crop details and can add the crop to his cart
- buyer should be able to put an order for the crops and generate a contract
// controllers/buyerController.js
const Buyer = require('../models/Buyer');
const crypto = require('crypto');
const nodemailer = require('nodemailer');
// Dummy OTP function
function generateOTP() {
return crypto.randomInt(100000, 999999).toString();
}
// Send OTP
exports.sendOtp = async (req, res) => {
const { mobileNumber, email } = req.body;
const otp = generateOTP();
req.session.otp = crypto.createHash('sha256').update(otp).digest('hex'); // Hash OTP before storing
req.session.otpExpiry = Date.now() + 300000; // OTP valid for 5 minutes
// Implement your OTP sending logic here (e.g., using an SMS or email service)
console.log(`OTP sent to ${email}: ${otp}`);
res.status(200).json({ message: 'OTP sent successfully' });
};
// Verify OTP and Register
exports.register = async (req, res) => {
const { fullName, mobileNumber, email, password, enteredOtp } = req.body;
if (!req.session.otp || Date.now() > req.session.otpExpiry) {
return res.status(400).json({ message: 'OTP expired. Please request a new one.' });
}
const hashedOtp = crypto.createHash('sha256').update(enteredOtp).digest('hex');
if (hashedOtp !== req.session.otp) {
return res.status(400).json({ message: 'Invalid OTP' });
}
try {
const buyer = new Buyer({ fullName, mobileNumber, email, password });
await buyer.save();
req.session.otp = null; // Clear OTP after successful registration
res.status(201).json({ message: 'User registered successfully' });
} catch (err) {
res.status(500).json({ message: 'Registration failed', error: err.message });
}
};
// Verify OTP and Login
exports.login = async (req, res) => {
const { email, password, enteredOtp } = req.body;
if (!req.session.otp || Date.now() > req.session.otpExpiry) {
return res.status(400).json({ message: 'OTP expired. Please request a new one.' });
}
const hashedOtp = crypto.createHash('sha256').update(enteredOtp).digest('hex');
if (hashedOtp !== req.session.otp) {
return res.status(400).json({ message: 'Invalid OTP' });
}
try {
const buyer = await Buyer.findOne({ email });
if (!buyer || !(await buyer.isPasswordCorrect(password))) {
return res.status(401).json({ message: 'Invalid email or password' });
}
req.session.otp = null; // Clear OTP after successful login
res.status(200).json({ message: 'Login successful' });
} catch (err) {
res.status(500).json({ message: 'Login failed', error: err.message });
}
};