File size: 11,386 Bytes
b63786a
 
 
 
 
 
 
 
 
 
 
 
 
4c888b7
 
 
 
 
 
 
 
b63786a
 
 
 
4c888b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b63786a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c888b7
b63786a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c888b7
 
 
 
 
 
 
 
 
 
b63786a
 
 
 
 
 
 
4c888b7
 
 
b63786a
 
 
 
 
 
 
 
 
 
 
4c888b7
 
 
 
 
 
b63786a
 
 
 
 
4c888b7
 
 
 
 
 
 
 
 
 
 
b63786a
 
 
 
 
 
 
 
4c888b7
b63786a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c888b7
 
 
 
 
 
 
 
 
 
b63786a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
const User = require('../models/User');
const {
  buildContactFields,
  generateToken,
  getContactFromIdentifier,
  getPasswordStrength,
  validatePassword,
} = require('../utils/validators');
const { shouldUseMemoryStore } = require('../config/dbMode');
const memoryStore = require('../store/memoryStore');
const otpStore = require('../store/otpStore');
const { sendVerificationCode } = require('../utils/otpDelivery');
const { serializeUser } = require('../utils/userSerializer');
const {
  DEFAULT_STARTING_TOKENS,
  INVITE_REWARD,
  addTokensToUser,
  ensureUserTokenState,
  generateReferralCode,
  normalizeReferralCode,
} = require('../utils/tokenUtils');

const SIGNUP_CODE_PURPOSE = 'signup';
const RESET_CODE_PURPOSE = 'password-reset';

const findUserRecordByReferralCode = async (referralCode) => {
  const normalizedReferralCode = normalizeReferralCode(referralCode);
  if (!normalizedReferralCode) {
    return null;
  }

  if (shouldUseMemoryStore()) {
    return memoryStore.findUserRecordByReferralCode(normalizedReferralCode);
  }

  return User.findOne({ referralCode: normalizedReferralCode });
};

const generateUniqueReferralCode = async () => {
  for (let attempt = 0; attempt < 12; attempt += 1) {
    const referralCode = generateReferralCode();
    const existingUser = await findUserRecordByReferralCode(referralCode);
    if (!existingUser) {
      return referralCode;
    }
  }

  throw new Error('Could not generate a unique referral code');
};

const getContactQuery = (contact) =>
  contact.type === 'email' ? { email: contact.value } : { phone: contact.value };

const getDuplicateQuery = (username, contact) => {
  const conditions = [{ username }];

  if (contact.type === 'email') {
    conditions.push({ email: contact.value });
  } else if (contact.type === 'phone') {
    conditions.push({ phone: contact.value });
  }

  return { $or: conditions };
};

const resolveContact = (identifier) => {
  const contact = getContactFromIdentifier(identifier);

  if (!contact) {
    const error = new Error('Please enter a valid email address or mobile number.');
    error.status = 400;
    throw error;
  }

  return contact;
};

const findUserRecordByContact = async (contact) => {
  if (shouldUseMemoryStore()) {
    return memoryStore.findUserRecordByIdentifier(contact.value);
  }

  return User.findOne(getContactQuery(contact));
};

const sendCodeAndRespond = async ({ res, purpose, contact, message }) => {
  const otp = otpStore.createCode({
    purpose,
    identifier: contact.value,
    channel: contact.type,
  });

  try {
    const deliveryResult = await sendVerificationCode({
      contact,
      code: otp.code,
      purpose,
    });

    const responseMessage = deliveryResult.delivered
      ? message
      : `${contact.type === 'email' ? 'Email' : 'SMS'} delivery is not configured in this environment, so use the dev code shown below.`;

    return res.json({
      message: responseMessage,
      delivery: {
        channel: contact.type,
        delivered: deliveryResult.delivered,
        mode: deliveryResult.mode,
        provider: deliveryResult.provider,
      },
      ...(deliveryResult.debugCode ? { debugCode: deliveryResult.debugCode } : {}),
    });
  } catch (error) {
    otpStore.clearCode({ purpose, identifier: contact.value });
    throw error;
  }
};

const respondWithError = (res, error, fallbackMessage) => {
  console.error(fallbackMessage, error);

  if (error.code === 11000) {
    return res.status(409).json({ error: 'Email, mobile number, or username already exists' });
  }

  return res.status(error.status || 500).json({
    error: error.message || fallbackMessage,
  });
};

exports.sendSignupCode = async (req, res) => {
  try {
    const contact = resolveContact(req.body.identifier);
    const existingUser = await findUserRecordByContact(contact);

    if (existingUser) {
      return res.status(409).json({ error: 'An account already exists with this email or mobile number' });
    }

    return await sendCodeAndRespond({
      res,
      purpose: SIGNUP_CODE_PURPOSE,
      contact,
      message: `Verification code sent to your ${contact.type}`,
    });
  } catch (error) {
    return respondWithError(res, error, 'Could not send signup code');
  }
};

exports.signup = async (req, res) => {
  try {
    const { username, identifier, password, confirmPassword, code, referralCode } = req.body;
    const normalizedUsername = String(username || '').trim();

    if (!normalizedUsername || !identifier || !password || !confirmPassword || !code) {
      return res.status(400).json({ error: 'All fields are required' });
    }

    if (normalizedUsername.length < 3) {
      return res.status(400).json({ error: 'Username must be at least 3 characters long' });
    }

    const contact = resolveContact(identifier);

    if (password !== confirmPassword) {
      return res.status(400).json({ error: 'Passwords do not match' });
    }

    if (!validatePassword(password)) {
      return res.status(400).json({
        error: 'Password must be at least 8 characters with uppercase, lowercase, and number',
      });
    }

    const otpValidation = otpStore.validateCode({
      purpose: SIGNUP_CODE_PURPOSE,
      identifier: contact.value,
      code,
    });

    if (!otpValidation.ok) {
      return res.status(otpValidation.status).json({ error: otpValidation.error });
    }

    const normalizedReferralCode = normalizeReferralCode(referralCode);
    const referrer = normalizedReferralCode
      ? await findUserRecordByReferralCode(normalizedReferralCode)
      : null;

    if (normalizedReferralCode && !referrer) {
      return res.status(400).json({ error: 'Invalid referral code' });
    }

    const uniqueReferralCode = await generateUniqueReferralCode();
    let user;

    if (shouldUseMemoryStore()) {
      user = await memoryStore.createUser({
        username: normalizedUsername,
        ...buildContactFields(contact),
        password,
        tokenBalance: DEFAULT_STARTING_TOKENS,
        referralCode: uniqueReferralCode,
        referredByUserId: referrer?._id || referrer?.id || '',
      });
    } else {
      const existingUser = await User.findOne(getDuplicateQuery(normalizedUsername, contact));
      if (existingUser) {
        return res.status(409).json({ error: 'Email, mobile number, or username already exists' });
      }

      const createdUser = new User({
        username: normalizedUsername,
        ...buildContactFields(contact),
        password,
        tokenBalance: DEFAULT_STARTING_TOKENS,
        referralCode: uniqueReferralCode,
        referredByUserId: referrer?._id || null,
        rewardedLikePostIds: [],
        rewardedFollowUserIds: [],
        tokenHistory: [],
      });
      await createdUser.save();
      user = createdUser;
    }

    if (referrer) {
      addTokensToUser(referrer, INVITE_REWARD, 'invite-referral', `New signup used referral code ${normalizedReferralCode}`);

      if (shouldUseMemoryStore()) {
        await memoryStore.persistStore();
      } else {
        ensureUserTokenState(referrer);
        await referrer.save();
      }
    }

    otpStore.clearCode({
      purpose: SIGNUP_CODE_PURPOSE,
      identifier: contact.value,
    });

    const token = generateToken(user.id || user._id);

    return res.status(201).json({
      message: `User created successfully. ${DEFAULT_STARTING_TOKENS} signup tokens added.`,
      token,
      user: serializeUser(user),
    });
  } catch (error) {
    return respondWithError(res, error, 'Signup failed');
  }
};

exports.login = async (req, res) => {
  try {
    const { identifier, email, phone, password, rememberMe } = req.body;
    const rawIdentifier = identifier || email || phone;

    if (!rawIdentifier || !password) {
      return res.status(400).json({ error: 'Email/mobile number and password are required' });
    }

    const contact = resolveContact(rawIdentifier);
    const user = await findUserRecordByContact(contact);

    if (!user) {
      return res.status(401).json({ error: 'Invalid email/mobile number or password' });
    }

    const isPasswordValid = shouldUseMemoryStore()
      ? await memoryStore.comparePassword(user, password)
      : await user.comparePassword(password);

    if (!isPasswordValid) {
      return res.status(401).json({ error: 'Invalid email/mobile number or password' });
    }

    if (!user.referralCode) {
      user.referralCode = await generateUniqueReferralCode();

      if (shouldUseMemoryStore()) {
        await memoryStore.persistStore();
      } else {
        await user.save();
      }
    }

    const token = generateToken(user.id || user._id);

    return res.json({
      message: 'Login successful',
      token,
      user: serializeUser(user),
      rememberMe,
    });
  } catch (error) {
    return respondWithError(res, error, 'Login failed');
  }
};

exports.sendPasswordResetCode = async (req, res) => {
  try {
    const contact = resolveContact(req.body.identifier);
    const user = await findUserRecordByContact(contact);

    if (!user) {
      return res.status(404).json({ error: 'No account found with this email or mobile number' });
    }

    return await sendCodeAndRespond({
      res,
      purpose: RESET_CODE_PURPOSE,
      contact,
      message: `Password reset code sent to your ${contact.type}`,
    });
  } catch (error) {
    return respondWithError(res, error, 'Could not send password reset code');
  }
};

exports.resetPassword = async (req, res) => {
  try {
    const { identifier, code, password, confirmPassword } = req.body;

    if (!identifier || !code || !password || !confirmPassword) {
      return res.status(400).json({ error: 'All fields are required' });
    }

    const contact = resolveContact(identifier);

    if (password !== confirmPassword) {
      return res.status(400).json({ error: 'Passwords do not match' });
    }

    if (!validatePassword(password)) {
      return res.status(400).json({
        error: 'Password must be at least 8 characters with uppercase, lowercase, and number',
      });
    }

    const user = await findUserRecordByContact(contact);

    if (!user) {
      return res.status(404).json({ error: 'No account found with this email or mobile number' });
    }

    const otpValidation = otpStore.validateCode({
      purpose: RESET_CODE_PURPOSE,
      identifier: contact.value,
      code,
    });

    if (!otpValidation.ok) {
      return res.status(otpValidation.status).json({ error: otpValidation.error });
    }

    if (shouldUseMemoryStore()) {
      await memoryStore.updateUserPassword(user._id, password);
    } else {
      user.password = password;
      await user.save();
    }

    otpStore.clearCode({
      purpose: RESET_CODE_PURPOSE,
      identifier: contact.value,
    });

    return res.json({ message: 'Password reset successful. Please log in with your new password.' });
  } catch (error) {
    return respondWithError(res, error, 'Password reset failed');
  }
};

exports.checkPasswordStrength = async (req, res) => {
  try {
    const { password } = req.body;

    if (!password) {
      return res.status(400).json({ error: 'Password is required' });
    }

    const strength = getPasswordStrength(password);

    return res.json({ strength });
  } catch (error) {
    return res.status(500).json({ error: 'Failed to check password strength' });
  }
};