File size: 3,297 Bytes
b63786a
 
 
 
 
4798adb
 
 
 
 
 
 
 
 
b63786a
4798adb
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
const jwt = require('jsonwebtoken');
const validator = require('validator');

const PHONE_REGEX = /^\d{10,15}$/;

const getJwtSecret = () => {
  const secret = process.env.JWT_SECRET;
  if (!secret) {
    console.warn('JWT_SECRET not set - using insecure fallback. Set JWT_SECRET in environment for production.');
    return 'fallback_secret_change_me_in_production';
  }
  return secret;
};

const generateToken = (userId) => {
  return jwt.sign({ userId }, getJwtSecret(), { expiresIn: '7d' });
};

const normalizeEmail = (email = '') => String(email || '').trim().toLowerCase();

const normalizePhone = (phone = '') => String(phone || '').trim().replace(/[^\d]/g, '');

const validateEmail = (email) => {
  const normalizedEmail = normalizeEmail(email);
  return Boolean(normalizedEmail) && validator.isEmail(normalizedEmail);
};

const validatePhone = (phone) => {
  const normalizedPhone = normalizePhone(phone);
  return PHONE_REGEX.test(normalizedPhone);
};

const getSmsDefaultCountryCode = () =>
  String(process.env.SMS_DEFAULT_COUNTRY_CODE || '')
    .trim()
    .replace(/[^\d]/g, '');

const getContactFromIdentifier = (identifier) => {
  const rawIdentifier = String(identifier || '').trim();

  if (!rawIdentifier) {
    return null;
  }

  const normalizedEmail = normalizeEmail(rawIdentifier);
  if (validateEmail(normalizedEmail)) {
    return {
      type: 'email',
      value: normalizedEmail,
      label: normalizedEmail,
    };
  }

  const normalizedPhone = normalizePhone(rawIdentifier);
  if (validatePhone(normalizedPhone)) {
    return {
      type: 'phone',
      value: normalizedPhone,
      label: normalizedPhone,
    };
  }

  return null;
};

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

const formatPhoneForSms = (phone) => {
  const normalizedPhone = normalizePhone(phone);
  if (!normalizedPhone) {
    return '';
  }

  const defaultCountryCode = getSmsDefaultCountryCode();

  if (normalizedPhone.length === 10) {
    if (!defaultCountryCode) {
      const error = new Error(
        'For SMS delivery, enter the mobile number with country code (for example, +919876543210) or configure SMS_DEFAULT_COUNTRY_CODE on the server.'
      );
      error.status = 400;
      throw error;
    }

    return `+${defaultCountryCode}${normalizedPhone}`;
  }

  return `+${normalizedPhone}`;
};

const validatePassword = (password) => {
  const re = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d@$!%*?&]{8,}$/;
  return re.test(password);
};

const getPasswordStrength = (password) => {
  let strength = 0;
  if (password.length >= 8) strength++;
  if (password.length >= 12) strength++;
  if (/[a-z]/.test(password)) strength++;
  if (/[A-Z]/.test(password)) strength++;
  if (/\d/.test(password)) strength++;
  if (/[@$!%*?&]/.test(password)) strength++;

  if (strength <= 2) return 'Weak';
  if (strength <= 4) return 'Fair';
  if (strength <= 5) return 'Good';
  return 'Strong';
};

module.exports = {
  buildContactFields,
  formatPhoneForSms,
  generateToken,
  getContactFromIdentifier,
  getPasswordStrength,
  getSmsDefaultCountryCode,
  normalizeEmail,
  normalizePhone,
  validateEmail,
  validatePassword,
  validatePhone,
};