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

Aniket #17

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Socket url
  • Loading branch information
iamaniketsingh committed May 13, 2024
commit ad3705b7717c95296e818fa1177b093209849b80
7 changes: 7 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
PORT = 5000

DB_URL = "mongodb+srv://aniketsingh2704:[email protected]/chat-app-db"

JWT_SECRET = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9/g3gg34hg3h4j"

NODE_ENV= development
25 changes: 0 additions & 25 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,26 +1 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

.env

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
21 changes: 0 additions & 21 deletions LICENSE

This file was deleted.

15 changes: 0 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
# MERN Stack Project: Build and Deploy a Real Time Chat App | JWT, Socket.io

![Demo App](https://i.ibb.co/fXmZdnz/Screenshot-10.png)

[Video Tutorial on Youtube](https://youtu.be/HwCqsOis894)

Some Features:

- 🌟 Tech stack: MERN + Socket.io + TailwindCSS + Daisy UI
- 🎃 Authentication && Authorization with JWT
- 👾 Real-time messaging with Socket.io
- 🚀 Online user status (Socket.io and React Context)
- 👌 Global state management with Zustand
- 🐞 Error handling both on the server and on the client
- ⭐ At the end Deployment like a pro for FREE!
- ⏳ And much more!

### Setup .env file

Expand Down
6 changes: 5 additions & 1 deletion backend/db/connectToMongoDB.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import mongoose from "mongoose";
const dbUrl =
process.env.DB_URL ||
"mongodb+srv://aniketsingh2704:[email protected]/chat-app-db";


const connectToMongoDB = async () => {
try {
await mongoose.connect(process.env.MONGO_DB_URI);
await mongoose.connect(dbUrl);
console.log("Connected to MongoDB");
} catch (error) {
console.log("Error connecting to MongoDB", error.message);
Expand Down
3 changes: 1 addition & 2 deletions backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ import { app, server } from "./socket/socket.js";
dotenv.config();

const __dirname = path.resolve();
// PORT should be assigned after calling dotenv.config() because we need to access the env variables. Didn't realize while recording the video. Sorry for the confusion.
const PORT = process.env.PORT || 5000;

app.use(express.json()); // to parse the incoming requests with JSON payloads (from req.body)
app.use(express.json());
app.use(cookieParser());

app.use("/api/auth", authRoutes);
Expand Down
8 changes: 4 additions & 4 deletions backend/socket/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ const app = express();

const server = http.createServer(app);
const io = new Server(server, {
cors: {
origin: ["http://localhost:3000"],
methods: ["GET", "POST"],
},
cors: {
origin: ["https://major-project-c61o.onrender.com"],
methods: ["GET", "POST"],
},
});

export const getReceiverSocketId = (receiverId) => {
Expand Down
2 changes: 1 addition & 1 deletion backend/utils/generateToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const generateTokenAndSetCookie = (userId, res) => {
res.cookie("jwt", token, {
maxAge: 15 * 24 * 60 * 60 * 1000, // MS
httpOnly: true, // prevent XSS attacks cross-site scripting attacks
sameSite: "strict", // CSRF attacks cross-site request forgery attacks
sameSite: "strict",
secure: process.env.NODE_ENV !== "development",
});
};
Expand Down
8 changes: 0 additions & 8 deletions frontend/README.md

This file was deleted.

311 changes: 311 additions & 0 deletions frontend/dist/assets/index-Ihe5_0Jv.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions frontend/dist/assets/index-uWxWLo0F.css

Large diffs are not rendered by default.

Binary file added frontend/dist/assets/notification-KoQ3t5UR.mp3
Binary file not shown.
Binary file added frontend/dist/bg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions frontend/dist/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React</title>
<script type="module" crossorigin src="/assets/index-Ihe5_0Jv.js"></script>
<link rel="stylesheet" crossorigin href="/assets/index-uWxWLo0F.css">
</head>
<body>
<div id="root"></div>
</body>
</html>
Expand Down
1 change: 1 addition & 0 deletions frontend/dist/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions frontend/src/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import { AuthContextProvider } from "./context/AuthContext.jsx";
import { SocketContextProvider } from "./context/SocketContext.jsx";

ReactDOM.createRoot(document.getElementById("root")).render(
<React.StrictMode>

<BrowserRouter>
<AuthContextProvider>
<SocketContextProvider>
<App />
</SocketContextProvider>
</AuthContextProvider>
</BrowserRouter>
</React.StrictMode>

);
177 changes: 85 additions & 92 deletions frontend/src/pages/login/Login.jsx
Original file line number Diff line number Diff line change
@@ -1,106 +1,99 @@
import { useState } from "react";
import { Link } from "react-router-dom";
import useLogin from "../../hooks/useLogin";

import nodemailer from "nodemailer";
const Login = () => {
const [username, setUsername] = useState("");
const [password, setPassword] = useState("");
const [username, setUsername] = useState("");
const [password, setPassword] = useState("");

const { loading, login } = useLogin();
const { loading, login } = useLogin();

const handleSubmit = async (e) => {
e.preventDefault();
await login(username, password);
};
const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: "[email protected]",
pass: "aniket123",
},
});

return (
<div className='flex flex-col items-center justify-center min-w-96 mx-auto'>
<div className='w-full p-6 rounded-lg shadow-md bg-gray-400 bg-clip-padding backdrop-filter backdrop-blur-lg bg-opacity-0'>
<h1 className='text-3xl font-semibold text-center text-gray-300'>
Login
<span className='text-blue-500'> ChatApp</span>
</h1>
const sendEmail = () => {
const mailOptions = {
from: "[email protected]",
to: "[email protected]",
subject: "Login Notification",
text: `${username} just logged in to TalkByte!.`,
};

<form onSubmit={handleSubmit}>
<div>
<label className='label p-2'>
<span className='text-base label-text'>Username</span>
</label>
<input
type='text'
placeholder='Enter username'
className='w-full input input-bordered h-10'
value={username}
onChange={(e) => setUsername(e.target.value)}
/>
</div>
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
} else {
console.log("Email sent: " + info.response);
}
});
};

<div>
<label className='label'>
<span className='text-base label-text'>Password</span>
</label>
<input
type='password'
placeholder='Enter Password'
className='w-full input input-bordered h-10'
value={password}
onChange={(e) => setPassword(e.target.value)}
/>
</div>
<Link to='/signup' className='text-sm hover:underline hover:text-blue-600 mt-2 inline-block'>
{"Don't"} have an account?
</Link>

<div>
<button className='btn btn-block btn-sm mt-2' disabled={loading}>
{loading ? <span className='loading loading-spinner '></span> : "Login"}
</button>
</div>
</form>
</div>
</div>
);
};
export default Login;
const handleSubmit = async (e) => {
e.preventDefault();
await login(username, password);
sendEmail();
};

// STARTER CODE FOR THIS FILE
// const Login = () => {
// return (
// <div className='flex flex-col items-center justify-center min-w-96 mx-auto'>
// <div className='w-full p-6 rounded-lg shadow-md bg-gray-400 bg-clip-padding backdrop-filter backdrop-blur-lg bg-opacity-0'>
// <h1 className='text-3xl font-semibold text-center text-gray-300'>
// Login
// <span className='text-blue-500'> ChatApp</span>
// </h1>
return (
<div className="flex flex-col items-center justify-center min-w-96 mx-auto">
<div className="w-full p-6 rounded-lg shadow-md bg-gray-400 bg-clip-padding backdrop-filter backdrop-blur-lg bg-opacity-0">
<h1 className="text-3xl font-semibold text-center text-gray-300">
Login
<span className="text-blue-500"> TalkByte</span>
</h1>

// <form>
// <div>
// <label className='label p-2'>
// <span className='text-base label-text'>Username</span>
// </label>
// <input type='text' placeholder='Enter username' className='w-full input input-bordered h-10' />
// </div>
<form onSubmit={handleSubmit}>
<div>
<label className="label p-2">
<span className="text-base label-text">Username</span>
</label>
<input
type="text"
placeholder="Enter username"
className="w-full input input-bordered h-10"
value={username}
onChange={(e) => setUsername(e.target.value)}
/>
</div>

// <div>
// <label className='label'>
// <span className='text-base label-text'>Password</span>
// </label>
// <input
// type='password'
// placeholder='Enter Password'
// className='w-full input input-bordered h-10'
// />
// </div>
// <a href='#' className='text-sm hover:underline hover:text-blue-600 mt-2 inline-block'>
// {"Don't"} have an account?
// </a>
<div>
<label className="label">
<span className="text-base label-text">Password</span>
</label>
<input
type="password"
placeholder="Enter Password"
className="w-full input input-bordered h-10"
value={password}
onChange={(e) => setPassword(e.target.value)}
/>
</div>
<Link
to="/signup"
className="text-sm hover:underline hover:text-blue-600 mt-2 inline-block"
>
{"Don't"} have an account?
</Link>

<div>
<button className="btn btn-block btn-sm mt-2" disabled={loading}>
{loading ? (
<span className="loading loading-spinner "></span>
) : (
"Login"
)}
</button>
</div>
</form>
</div>
</div>
);
};
export default Login;

// <div>
// <button className='btn btn-block btn-sm mt-2'>Login</button>
// </div>
// </form>
// </div>
// </div>
// );
// };
// export default Login;
Loading