Files
online-backend/internal/identity/auth/phone.go
GuwandykA 514b9ca5fc Add forgot-password SMS reset, client/staff user lists, and regenerate Swagger.
Fold follow-up migrations into init, expose GET /users/clients and /users/staff, and refresh seed data.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-14 16:42:47 +05:00

47 lines
1008 B
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package auth
import (
"fmt"
"strconv"
"strings"
"unicode"
)
func onlyDigits(s string) string {
var b strings.Builder
for _, r := range s {
if unicode.IsDigit(r) {
b.WriteRune(r)
}
}
return b.String()
}
// NormalizePhone strips a leading +993 / 993 country code, then checks
// that the remaining local number is 8 digits with prefix 61–65 or 71–72.
// It returns +993 XX XX XX XX.
func NormalizePhone(raw string) (string, error) {
digits := onlyDigits(raw)
if strings.HasPrefix(digits, "993") {
digits = digits[3:]
} else if len(digits) == 9 && digits[0] == '8' {
digits = digits[1:]
}
if len(digits) != 8 {
return "", ErrInvalidPhone
}
if !validMobilePrefix(digits[:2]) {
return "", ErrInvalidPhone
}
return fmt.Sprintf("+993 %s %s %s %s", digits[0:2], digits[2:4], digits[4:6], digits[6:8]), nil
}
func validMobilePrefix(prefix string) bool {
n, err := strconv.Atoi(prefix)
if err != nil {
return false
}
return (n >= 61 && n <= 65) || (n >= 71 && n <= 72)
}