-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateUser.ts
More file actions
132 lines (108 loc) Β· 5.25 KB
/
Copy pathcreateUser.ts
File metadata and controls
132 lines (108 loc) Β· 5.25 KB
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
import { createUserWithEmailAndPassword, User } from 'firebase/auth'
import { collection, doc, DocumentData, getDocs, Query, query, QuerySnapshot, setDoc, where } from "firebase/firestore";
import toast from "react-hot-toast";
import getCurrentUserData from "./getCurrentUserData";
import { auth, fireStore } from '../auth/Firebase'
import { capitalizeWord } from "./capitalize";
import { getRandomColor } from "./getRandomColor";
import { createNotification } from './createNotification';
import { NewUser } from "../interfaces"
import isEmail from 'validator/lib/isEmail';
import isStrongPassword from 'validator/lib/isStrongPassword';
import isAlpha from 'validator/lib/isAlpha';
import equals from 'validator/lib/equals'
const createUser = async (user: NewUser): Promise<void> => {
const passwordOptions = {
minLength: 8,
minLowercase: 0,
minUppercase: 0,
minNumbers: 0,
minSymbols: 0,
returnScore: false,
pointsPerUnique: 1,
pointsPerRepeat: 0.5,
pointsForContainingLower: 10,
pointsForContainingUpper: 10,
pointsForContainingNumber: 10,
pointsForContainingSymbol: 10
}
// Clear username from right and left spaces, then replace spaces with underscores (_)
const username = user.username;
username.trim();
username.replace(/\s+/g,"_");
username.toLowerCase();
// Check if there is a user with the same username
const q: Query<DocumentData> = query(collection(fireStore, "users"), where("username", "==", username));
const queryData: QuerySnapshot<DocumentData> = await getDocs(q);
const users = queryData.docs.map((doc) => ({ ...doc.data(), id: doc.id }));
// If username is not used
if (users.length == 0) {
// If email is valid
if (isEmail(user.email)) {
// If password is strong
if (isStrongPassword(user.password, passwordOptions)) {
// If passwords match
if(equals(user.password, user.confirmpassword)) {
// If firstname does not contain numbers
if (isAlpha(user.firstname)) {
// If lastname does not contain numbers
if (isAlpha(user.lastname)) {
// If email is not username
if (!equals(username, user.email)) {
try {
// Create new user
await createUserWithEmailAndPassword(auth, user.email, user.password);
// Get created user data
const userData: User = getCurrentUserData();
// Push user details to Firebase
const docRef = await setDoc(doc(fireStore, "users", userData.uid), {
id: userData.uid,
firstname: capitalizeWord(user.firstname.trim()),
lastname: capitalizeWord(user.lastname.trim()),
email: user.email,
username: username,
followers: [],
following: [],
claps: 0,
joinDate: new Date(),
avatarColor: getRandomColor()
});
createNotification(`@${username} joined the club.`);
// Show success message to the user
toast.success('Welcome to the club');
} catch (e) {
// If Email in use
if (e == "FirebaseError: Firebase: Error (auth/email-already-in-use).") {
toast.error('Email is already used');
}
}
} else {
// If username is email
toast.error('Username cannot be your email');
}
} else {
// If lastname is not strong
toast.error('Lastname cannot contain numbers');
}
} else {
// If firstname is not strong
toast.error('Firstname cannot contain numbers');
}
} else {
// if passwords does not match
toast.error("Passwords does not match")
}
} else {
// If password is not strong
toast.error('Weak password');
}
} else {
// If email is not valid
toast.error('Invalid email');
}
} else {
// Username is already used
toast.error('Username is already used');
}
}
export { createUser }