Skip to content

Commit 064d3a6

Browse files
committed
fix: otp read in otp verification jsx
1 parent 22b6583 commit 064d3a6

3 files changed

Lines changed: 19 additions & 9 deletions

File tree

‎apps/dashboard-api/src/controllers/auth.controller.js‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ module.exports.register = async (req, res) => {
116116
const salt = await bcrypt.genSalt(10);
117117
const hashedPassword = await bcrypt.hash(password, salt);
118118

119-
const newDev = new Developer({ email, password: hashedPassword });
119+
const newDev = new Developer({ email: email.toLowerCase().trim(), password: hashedPassword });
120120
await newDev.save();
121121

122122
res.status(201).json({ message: "Registered successfully" });
@@ -132,7 +132,7 @@ module.exports.login = async (req, res) => {
132132
try {
133133
const { email, password } = loginSchema.parse(req.body);
134134

135-
const dev = await Developer.findOne({ email });
135+
const dev = await Developer.findOne({ email: email.toLowerCase().trim() });
136136
if (!dev) return res.status(400).json({ error: "User not found" });
137137

138138
const validPass = await bcrypt.compare(password, dev.password);
@@ -200,12 +200,14 @@ module.exports.deleteAccount = async (req, res) => {
200200
module.exports.sendOtp = async (req, res) => {
201201
try {
202202
const { email } = onlyEmailSchema.parse(req.body);
203+
const normalizedEmail = email.toLowerCase().trim();
203204

204-
const existingUser = await Developer.findOne({ email });
205+
const existingUser = await Developer.findOne({ email: normalizedEmail });
205206
if (!existingUser) {
206207
return res.status(400).json({ error: "User not found. Ensure you are using the correct email." });
207208
}
208209

210+
209211
if (existingUser.isVerified) {
210212
return res.status(400).json({ error: "Account is already verified. Please login." });
211213
}

‎apps/public-api/src/controllers/userAuth.controller.js‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,7 @@ module.exports.signup = async (req, res) => {
939939
const project = req.project;
940940

941941
const { email, password, username, ...otherData } = userSignupSchema.parse(req.body);
942+
const normalizedEmail = email.toLowerCase().trim();
942943

943944
// Get Mongoose Model
944945
const usersColConfig = project.collections.find(c => c.name === 'users');
@@ -947,7 +948,8 @@ module.exports.signup = async (req, res) => {
947948
const connection = await getConnection(project._id);
948949
const Model = getCompiledModel(connection, usersColConfig, project._id, project.resources.db.isExternal);
949950

950-
const existingUser = await Model.findOne({ email });
951+
const existingUser = await Model.findOne({ email: normalizedEmail });
952+
951953
if (existingUser) {
952954
// Check if user is unverified. If so, we can trigger a resend instead of a hard error.
953955
const verificationField = getVerificationField(usersColConfig);
@@ -1040,14 +1042,16 @@ module.exports.login = async (req, res) => {
10401042
try {
10411043
const project = req.project;
10421044
const { email, password } = loginSchema.parse(req.body);
1045+
const normalizedEmail = email.toLowerCase().trim();
10431046

10441047
const usersColConfig = project.collections.find(c => c.name === 'users');
10451048
if (!usersColConfig) return res.status(404).json({ error: "Auth collection not found" });
10461049

10471050
const connection = await getConnection(project._id);
10481051
const Model = getCompiledModel(connection, usersColConfig, project._id, project.resources.db.isExternal);
10491052

1050-
const user = await Model.findOne({ email });
1053+
const user = await Model.findOne({ email: normalizedEmail });
1054+
10511055
if (!user) return res.status(400).json({ error: "Invalid email or password" });
10521056

10531057
const validPass = await bcrypt.compare(password, user.password);
@@ -1222,11 +1226,13 @@ module.exports.verifyEmail = async (req, res) => {
12221226
try {
12231227
const project = req.project;
12241228
const { email, otp } = verifyOtpSchema.parse(req.body);
1229+
const normalizedEmail = email.toLowerCase().trim();
12251230

1226-
const redisKey = `project:${project._id}:otp:verification:${email}`;
1231+
const redisKey = `project:${project._id}:otp:verification:${normalizedEmail}`;
12271232
const storedOtp = await redis.get(redisKey);
12281233

12291234
if (!storedOtp || storedOtp !== otp) {
1235+
12301236
return res.status(400).json({ error: "Invalid or expired OTP" });
12311237
}
12321238

@@ -1261,11 +1267,13 @@ module.exports.resendVerificationOtp = async (req, res) => {
12611267
try {
12621268
const project = req.project;
12631269
const { email } = onlyEmailSchema.parse(req.body);
1270+
const normalizedEmail = email.toLowerCase().trim();
12641271

12651272
const { usersColConfig, Model } = await getUsersModel(project);
12661273
if (!Model) return res.status(404).json({ error: "Auth collection not found" });
12671274

1268-
const user = await Model.findOne({ email });
1275+
const user = await Model.findOne({ email: normalizedEmail });
1276+
12691277
if (!user) {
12701278
return res.status(400).json({ error: "User not found with this email." });
12711279
}

‎apps/web-dashboard/src/pages/OtpVerification.jsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ function OtpVerification() {
1010
const { user, login, isLoading: authLoading } = useAuth(); // Destructure login
1111
const [otp, setOtp] = useState('');
1212

13-
// Derived email to avoid stale state if user is still loading
14-
const email = location.state?.email || user?.email || '';
13+
// Derived email with normalization to avoid stale/incorrect state
14+
const email = (location.state?.email || user?.email || '').toLowerCase().trim();
1515

1616
// Ref to track if we have already auto-sent OTP to prevent double sends in StrictMode
1717
const hasSentOtp = useRef(false);

0 commit comments

Comments
 (0)