Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: Additional attributes for users signing up with GoogleAuth #18

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/pages/registration/GoogleAuth.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import defaultProfile from '../../assets/defaultProfile.jpg';
// Google Auth
const provider = new GoogleAuthProvider();

const generateRandomDigits = () => {
return Math.floor(1000 + Math.random() * 9000).toString(); // generates a random 4-digit number
};
const generateUsername = (email) => {
return email.split('@')[0].replace(/[^a-zA-Z0-9]/g, '');
}

export const signInWithGoogle = async () => {
try {
Expand All @@ -21,17 +21,24 @@ export const signInWithGoogle = async () => {
const userDoc = await getDoc(userDocRef);

if (!userDoc.exists()) {
// Generate username only if the user does not exist in Firestore
const randomDigits = generateRandomDigits();
const username = `${user.displayName.replace(/\s+/g, '').toLowerCase()}${randomDigits}`;
console.log("Generated Username: ", username); // Print the generated username
// Generate username based on email
const username = generateUsername(user.email);
console.log("Generated Username: ", username);

await setDoc(userDocRef, {
email: user.email,
username: username,
email: user.email,
id: user.uid,
profilePic: defaultProfile,
blocked: [],
signUpDate: new Date().toISOString(),
});

const userChatDoc = doc(db, "UserChats", user.uid);
await setDoc(userChatDoc, {
chats: [],
});

toast.success("Account created successfully.");
} else {
toast.success("Login successfully.");
Expand Down
1 change: 0 additions & 1 deletion src/pages/registration/LoginSignupForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ function LoginSignUpForm() {
chats: [],
});

console.log("User ID:", user.uid);
toast.success("Account created successfully.");
navigate(`/profile/${user.uid}`);
}
Expand Down