Firebase Merged
Firebase Merged
js
const firebaseConfig = {
apiKey: "AIzaSyBpmX4IAa3TSjgOySyEsfIUIoc_zZyJouo",
authDomain: "my-vue-app-64fa3.firebaseapp.com",
projectId: "my-vue-app-64fa3",
storageBucket: "my-vue-app-64fa3.firebasestorage.app",
messagingSenderId: "448636428034",
appId: "1:448636428034:web:2e2792b58e369dcd966c1b",
measurementId: "G-LGTYT6H641"
};
const db = getFirestore(app);
import "./App.css";
from "react-router-dom";
function App() {
return (
<div className="App">
<BrowserRouter>
<Routes>
<Route path="/Signup"
<Route path="/chat-home/:receiverId"
</Routes>
</BrowserRouter>
</div>
);
import {
createUserWithEmailAndPassword,
updateProfile
} from "firebase/auth";
function Copyright(props) {
return (
<Typography
variant="body2"
color="text.secondary"
align="center"
{...props}
>
{"Copyright © "}
Your Website
</Link>{" "}
{new Date().getFullYear()}
{"."}
</Typography>
);
event.preventDefault();
try {
auth,
email,
password
);
displayName: username,
});
email: email,
userId: user.uid,
});
navigate("/");
} catch (error) {
alert(error.message);
};
return (
<ThemeProvider theme={theme}>
<CssBaseline />
<Box
sx={{
marginTop: 8,
display: "flex",
flexDirection: "column",
alignItems: "center",
}}
>
<LockOutlinedIcon />
</Avatar>
Sign up
</Typography>
<Box
component="form"
noValidate
onSubmit={handleSubmit}
sx={{ mt: 3 }}
>
<TextField
autoComplete="given-name"
name="firstName"
required
fullWidth
id="firstName"
label="First Name"
autoFocus
value={username}
/>
</Grid>
<TextField
required
fullWidth
id="email"
label="Email Address"
name="email"
autoComplete="email"
value={email}
/>
</Grid>
<TextField
required
fullWidth
name="password"
label="Password"
type="password"
id="password"
autoComplete="new-password"
value={password}
/>
</Grid>
</Grid>
<Button
type="submit"
fullWidth
variant="contained"
>
Sign Up
</Button>
<Grid item>
</Link>
</Grid>
</Grid>
</Box>
</Box>
</Container>
</ThemeProvider>
);
}
Signin.jsx
event.preventDefault();
.then((userCredential) => {
// Signed in
const user = userCredential.user;
navigate("/chat-home/1");
// ...
})
.catch((error) => {
alert(errorMessage);
});
};
return (
<ThemeProvider theme={theme}>
<CssBaseline />
<Box
sx={{
marginTop: 8,
display: "flex",
flexDirection: "column",
alignItems: "center",
}}
>
<LockOutlinedIcon />
</Avatar>
Sign in
</Typography>
<Box
component="form"
onSubmit={handleSubmit}
noValidate
sx={{ mt: 1 }}
>
<TextField
margin="normal"
required
fullWidth
id="email"
label="Email Address"
name="email"
autoComplete="email"
autoFocus
value={email}
/>
<TextField
margin="normal"
required
fullWidth
name="password"
label="Password"
type="password"
id="password"
autoComplete="current-password"
value={password}
/>
<FormControlLabel
control={<Checkbox value="remember"
color="primary" />}
label="Remember me"
/>
<Button
type="submit"
fullWidth
variant="contained"
>
Sign In
</Button>
<Grid container>
Forgot password?
</Link>
</Grid>
<Grid item>
</Link>
</Grid>
</Grid>
</Box>
</Box>
</Container>
</ThemeProvider>
);
}
Chathome.jsx
import {
addDoc,
collection,
onSnapshot,
orderBy,
query,
} from "firebase/firestore";
function UsersComponent(props) {
props.setReceiverData({
username: username,
userId: userId,
});
props.navigate(`/chat-home/${userId}`);
};
Chathome.jsx
return (
<List
dense
sx={{
bgcolor: "background.paper"
}}
>
return (
<ListItemButton
onClick={() => {
handleToggle(value.username, value.userId);
}}
>
<ListItemAvatar>
<Avatar
alt={`${value.username}`}
src={`${value.username}.jpg`}
/>
</ListItemAvatar>
<ListItemText id={labelId}
primary={`${value.username}`} />
</ListItemButton>
</ListItem>
);
})}
Chathome.jsx
</List>
);
useEffect(() => {
});
return unsub;
}, []);
useEffect(() => {
if (receiverData) {
query(
collection(
db,
"users",
user?.uid,
Chathome.jsx
"chatUsers",
receiverData?.userId,
"messages"
),
orderBy("timestamp")
),
(snapshot) => {
setAllMessages(
snapshot.docs.map((doc) => ({
id: doc.id,
messages: doc.data(),
}))
);
);
return unsub;
}, [receiverData?.userId]);
try {
await addDoc(
collection(
db,
"users",
user.uid,
"chatUsers",
receiverData.userId,
"messages"
),
Chathome.jsx
username: user.displayName,
messageUserId: user.uid,
message: chatMessage,
);
await addDoc(
collection(
db,
"users",
receiverData.userId,
"chatUsers",
user.uid,
"messages"
),
username: user.displayName,
messageUserId: user.uid,
message: chatMessage,
);
} catch (error) {
console.log(error);
setChatMessage("");
};
Chathome.jsx
return (
<div style={root}>
<Paper style={left}>
<div
style={{
display: "flex",
padding: 5,
justifyContent: "space-between",
}}
>
<Button
color="secondary"
onClick={() => {
auth.signOut();
navigate("/");
}}
>
Logout
</Button>
</div>
<Divider />
All users
<UsersComponent
users={users}
setReceiverData={setReceiverData}
navigate={navigate}
currentUserId={user?.uid}
/>
</div>
Chathome.jsx
</Paper>
<Paper style={right}>
</h4>
<Divider />
<div style={messagesDiv}>
{allMessages &&
return (
<div
key={id}
style={{
margin: 2,
display: "flex",
flexDirection:
user?.uid == messages.messageUserId
? "row-reverse"
: "row",
}}
>
<span
style={{
backgroundColor: "#BB8FCE",
padding: 6,
borderTopLeftRadius:
user?.uid == messages.messageUserId ? 10 : 0,
Chathome.jsx
borderTopRightRadius:
borderBottomLeftRadius: 10,
borderBottomRightRadius: 10,
maxWidth: 400,
fontSize: 15,
textAlign:
}}
>
{messages.message}
</span>
</div>
);
})}
</div>
<input
value={chatMessage}
style={input}
type="text"
placeholder="Type message..."
/>
<IconButton onClick={sendMessage}>
</IconButton>
</div>
</Paper>
</div>
Chathome.jsx
);
const root = {
display: "flex",
flexDirection: "row",
flex: 1,
width: "100%",
};
const left = {
display: "flex",
flex: 0.2,
height: "95vh",
margin: 10,
flexDirection: "column",
};
const right = {
display: "flex",
flex: 0.8,
height: "95vh",
margin: 10,
flexDirection: "column",
};
const input = {
flex: 1,
outline: "none",
borderRadius: 5,
border: "none",
Chathome.jsx
};
const messagesDiv = {
backgroundColor: "#FBEEE6",
padding: 5,
display: "flex",
flexDirection: "column",
flex: 1,
maxHeight: 460,
overflowY: "scroll",
};
13.FUTURE SCOPE AND FURTHER ENHANCEMENTS
47 | P a g e
14.LIMITATIONS
There are several limitations that you may encounter when developing a
single message chat application using the (FireBase, Express.js,
React, Node.js) technology stack. Here are a few potential limitations:
Scalability: The MERN stack can handle moderate levels of traffic and
users, but it may face scalability challenges when dealing with a large
number of concurrent connections or heavy message traffic. Scaling the
application to handle high loads might require implementing techniques
such as load balancing, horizontal scaling, or using a dedicated messaging
service.
Thes technology benefits from a large and active community. This means
developers can find extensive resources, tutorials, and libraries to assist
in the development process and troubleshoot any issues.
While considering the advantages of the this tech it's crucial to take
into account the specific requirements and constraints of your project.
Proper planning, architectural considerations, and adherence to best
practices are essential for ensuring a secure, efficient, and user-friendly
single message chat application.
49 | P a g e
16.BIBLIOGRAPHY
-Thank You
------------X-----------
50 | P a g e