temp backup code
temp backup code
js
display: flex;
height: 100vh;
background-color: #f0f2f5;
`;
flex: 1;
display: flex;
`;
function App() {
useEffect(() => {
setIsAuthenticated(!!token);
}, []);
localStorage.removeItem('userId');
localStorage.removeItem('name');
setIsAuthenticated(false);
};
return (
<Router>
<AppContainer>
<MainContent>
<Routes>
<Route path="/" element={isAuthenticated ? <Contacts /> : <Navigate to="/login" />} /> {/*
Default route */}
</Routes>
</MainContent>
</AppContainer>
</Router>
);
flex: 1;
display: flex;
flex-direction: column;
`;
background-color: #f0f2f5;
`;
font-size: 16px;
font-weight: 500;
color: #111b21;
`;
flex: 1;
overflow-y: auto;
padding: 20px;
background-color: #efeae2;
`;
max-width: 65%;
border-radius: 7.5px;
margin-bottom: 10px;
font-size: 14.2px;
line-height: 19px;
color: #111b21;
background-color: #d9fdd3;
align-self: flex-end;
margin-left: auto;
`:`
background-color: #ffffff;
`}
`;
display: flex;
align-items: center;
padding: 10px;
background-color: #f0f2f5;
`;
flex: 1;
border: none;
border-radius: 8px;
font-size: 15px;
`;
background: none;
border: none;
color: #54656f;
font-size: 20px;
cursor: pointer;
margin-left: 10px;
`;
function Chat() {
useEffect(() => {
if (contactId) {
fetchMessages();
fetchContactDetails();
}, [contactId]);
try {
});
setMessages(response.data);
} catch (error) {
};
try {
});
setContact(contactDetails);
} catch (error) {
};
if (!newMessage.trim()) return;
try {
await axios.post('http://localhost:3001/messages',
);
setNewMessage('');
} catch (error) {
};
return (
<ChatContainer>
<ChatHeader>
</ChatHeader>
<MessagesContainer>
{messages.length > 0 ? (
{msg.content}
</Message>
))
):(
)}
</MessagesContainer>
<InputContainer>
<Input
type="text"
value={newMessage}
placeholder="Type a message"
/>
<SendButton onClick={sendMessage}>
<FaPaperPlane />
</SendButton>
</InputContainer>
</ChatContainer>
);
}
Contacts.js
function Contacts() {
useEffect(() => {
fetchContacts();
}, []);
try {
});
setContacts(response.data);
} catch (error) {
};
try {
);
} catch (error) {
setError(error.response.data.error);
} else {
};
return (
<div className="contacts-container">
<h2>Contacts</h2>
{showForm && (
<form onSubmit={addContact}>
<input
type="text"
value={newContact.mobile}
required
/>
<input
type="text"
placeholder="Contact's name"
value={newContact.name}
required
/>
</form>
)}
<ul className="contact-list">
{contacts.map((contact) => (
<li key={contact.contact_id}>
<Link to={`/chat/${contact.contact_id}`}>{contact.contact_name}</Link>
</li>
))}
</ul>
</div>
);
Login.js
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100%;
width: 100%;
background-color: #f0f2f5;
`;
background-color: #ffffff;
padding: 20px;
border-radius: 8px;
max-width: 350px;
`;
width: 100%;
padding: 10px;
margin-bottom: 10px;
border-radius: 4px;
`;
width: 100%;
padding: 10px;
background-color: #00a884;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
`;
color: red;
margin-bottom: 10px;
`;
e.preventDefault();
try {
localStorage.setItem('token', response.data.token);
localStorage.setItem('userId', response.data.userId);
localStorage.setItem('name', response.data.name);
setIsAuthenticated(true);
navigate('/');
} catch (error) {
setError('Invalid credentials');
};
return (
<LoginContainer>
<LoginForm onSubmit={handleSubmit}>
<h2>Login to WhatsApp</h2>
<Input
type="text"
placeholder="Mobile number"
value={mobile}
required
/>
<Input
type="password"
placeholder="Password"
value={password}
required
/>
<Button type="submit">Login</Button>
</LoginForm>
</LoginContainer>
);
Profile.js
flex: 1;
padding: 20px;
background-color: #fff;
`;
margin-bottom: 20px;
`;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
`;
useEffect(() => {
fetchProfile();
}, []);
try {
});
setProfile(response.data);
} catch (error) {
handleLogout();
};
if (!profile) {
}
return (
<ProfileContainer>
<h2>Profile</h2>
<ProfileInfo>
<p><strong>Name:</strong> {profile.name}</p>
<p><strong>Mobile:</strong> {profile.mobile}</p>
</ProfileInfo>
<LogoutButton onClick={handleLogout}>Logout</LogoutButton>
</ProfileContainer>
);
Register.js
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100%;
width: 100%;
background-color: #f0f2f5;
`;
const RegisterForm = styled.form`
background-color: #ffffff;
padding: 20px;
border-radius: 8px;
width: 100%;
max-width: 350px;
`;
width: 100%;
padding: 10px;
margin-bottom: 10px;
border-radius: 4px;
`;
width: 100%;
padding: 10px;
background-color: #00a884;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
`;
color: red;
margin-bottom: 10px;
`;
function Register() {
e.preventDefault();
try {
navigate('/login');
} catch (error) {
};
return (
<RegisterContainer>
<RegisterForm onSubmit={handleSubmit}>
<Input
type="text"
placeholder="Name"
value={name}
required
/>
<Input
type="text"
placeholder="Mobile number"
value={mobile}
required
/>
<Input
type="password"
placeholder="Password"
value={password}
required
/>
<Button type="submit">Register</Button>
</RegisterForm>
</RegisterContainer>
);
Sidebar.js
width: 35%;
max-width: 415px;
background-color: #ffffff;
display: flex;
flex-direction: column;
`;
width: 100%;
background-color: #f0f2f5;
color: #54656f;
border: none;
text-align: left;
font-size: 15px;
cursor: pointer;
&:hover {
background-color: #e9edef;
`;
display: flex;
justify-content: space-between;
align-items: center;
background-color: #f0f2f5;
`;
font-size: 24px;
color: #54656f;
cursor: pointer;
`;
font-size: 22px;
color: #54656f;
cursor: pointer;
`;
background-color: #f0f2f5;
`;
display: flex;
align-items: center;
background-color: #ffffff;
border-radius: 8px;
padding: 0 8px;
`;
font-size: 14px;
color: #54656f;
margin-right: 20px;
`;
width: 100%;
padding: 8px 0;
border: none;
outline: none;
font-size: 15px;
&::placeholder {
color: #3b4a54;
`;
overflow-y: auto;
flex: 1;
`;
display: flex;
align-items: center;
text-decoration: none;
color: inherit;
&:hover {
background-color: #f5f6f6;
`;
const ContactAvatar = styled.div`
width: 49px;
height: 49px;
border-radius: 50%;
background-color: #dfe5e7;
display: flex;
align-items: center;
justify-content: center;
margin-right: 15px;
`;
flex: 1;
`;
font-size: 17px;
color: #111b21;
margin-bottom: 3px;
`;
font-size: 14px;
color: #667781;
`;
text-decoration: none;
color: inherit;
`;
function Sidebar({ handleLogout }) {
useEffect(() => {
fetchContacts();
}, []);
try {
});
setContacts(response.data);
} catch (error) {
};
contact.contact_name.toLowerCase().includes(searchTerm.toLowerCase())
);
return (
<SidebarContainer>
<Header>
<ProfileLink to="/profile">
<ProfileIcon />
</ProfileLink>
<MenuIcon />
</Header>
<SearchContainer>
<SearchWrapper>
<SearchIcon />
<SearchInput
value={searchTerm}
/>
</SearchWrapper>
</SearchContainer>
<ContactsList>
<ContactItem to="/contacts">
<ContactAvatar>
</ContactAvatar>
<ContactInfo>
<ContactName>Contacts</ContactName>
</ContactInfo>
</ContactItem>
{filteredContacts.map((contact) => (
<ContactAvatar>
</ContactAvatar>
<ContactInfo>
<ContactName>{contact.contact_name}</ContactName>
</ContactInfo>
</ContactItem>
))}
</ContactsList>
<LogoutButton onClick={handleLogout}>Logout</LogoutButton>
</SidebarContainer>
);
Server.js
require('dotenv').config();
app.use(express.json());
app.use(cors());
// MySQL connection
const db = mysql.createConnection({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME
});
db.connect((err) => {
if (err) {
return;
});
req.userId = decoded.userId;
next();
});
};
// User registration
db.query('INSERT INTO users (mobile, password, name) VALUES (?, ?, ?)', [mobile, hashedPassword,
name], (err, result) => {
if (err) {
} else {
}
});
});
// User login
db.query('SELECT * FROM users WHERE mobile = ?', [mobile], async (err, results) => {
} else {
if (passwordMatch) {
} else {
});
});
db.query('SELECT id, mobile, name, profile_picture FROM users WHERE id = ?', [req.userId], (err,
results) => {
if (err) {
} else {
res.json(results[0]);
});
});
db.query('SELECT id FROM users WHERE mobile = ?', [contactMobile], (err, results) => {
if (err) {
return res.status(500).json({ error: 'Database error while searching for the user.' });
if (results.length === 0) {
return res.status(404).json({ error: 'User with this mobile number does not exist.' });
db.query('SELECT * FROM contacts WHERE user_id = ? AND contact_id = ?', [req.userId, contactId],
(err, contactExists) => {
if (err) {
return res.status(500).json({ error: 'Error while checking if contact already exists.' });
if (contactExists.length > 0) {
return res.status(400).json({ error: 'Contact already exists in your contact list.' });
}
// Add the contact to the logged-in user's contacts
db.query(
if (err) {
);
});
});
});
if (err) {
} else {
res.json(results);
});
});
// Send a message
if (err) {
} else {
});
});
if (err) {
} else {
res.json(results);
});
});
app.listen(PORT, () => {
});
Database Schema
USE whatapp_db;
name VARCHAR(50),
profile_picture VARCHAR(255),
);
);
contact_name VARCHAR(50),
);