Skip to content

Commit

Permalink
Merge branch 'Bitbox-Connect:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Trumilnasit authored Oct 10, 2024
2 parents a8391c3 + a39f73f commit f699f24
Show file tree
Hide file tree
Showing 14 changed files with 312 additions and 117 deletions.
44 changes: 37 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# BitBox

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![GitHub issues](https://img.shields.io/github/issues/Anuj3553/codecontri.svg)](https://github.com/yourusername/codecontri/issues)
[![GitHub stars](https://img.shields.io/github/stars/Anuj3553/codecontri.svg)](https://github.com/yourusername/codecontri/stargazers)
[![GitHub forks](https://img.shields.io/github/forks/Anuj3553/codecontri.svg)](https://github.com/yourusername/codecontri/network)
[![GitHub issues](https://img.shields.io/github/issues/Anuj3553/BitBox.svg)](https://github.com/Anuj3553/BitBox/issues)
[![GitHub stars](https://img.shields.io/github/stars/Anuj3553/BitBox.svg)](https://github.com/Anuj3553/BitBox/stargazers)
[![GitHub forks](https://img.shields.io/github/forks/Anuj3553/BitBox.svg)](https://github.com/Anuj3553/BitBox/network)

## 🎯 Overview

Expand Down Expand Up @@ -41,15 +41,37 @@ npm install npm@latest -g
1. **Clone the repository**:

```sh
git clone https://github.com/your_username/BitBox.git
git clone https://github.com/Anuj3553/BitBox.git
```

2. **Install NPM packages**:
2. **Install NPM packages** for both client and server:

```sh
cd client
npm install
```

```sh
cd server
npm install
```

### Running the Project

1. **Open the first terminal** and start the client:

```sh
cd client
npm run dev
```

2. **Open the second terminal** and start the server:

```sh
cd server
nodemon index.js
```

---

## 🤝 Contributing
Expand Down Expand Up @@ -92,6 +114,14 @@ For any inquiries, feel free to reach out:

## 🏆 Main Contributors

- **Harshit7492**
- **Anuj Verma**
- **Jitendra Kumar**
Email: [[email protected]](mailto:[email protected])

**Highlighted Main Contributors:**

```diff
+ 🌟 Harshit7492: Backend API development and database optimizations.
+ 🌟 Jitendra Kumar: Significant contributions in frontend development.
```

---
2 changes: 2 additions & 0 deletions client/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import Discussion from './component/Discussion';
import { useAtom } from 'jotai';
import { modeAtom } from './atom/Atom';
import ForgotPassword from './component/forgotpass';
import VerifyEmail from './component/Verify';
// Main Layout Component
const Layout = ({ children, mode, setProgress, toggleMode, showAlert }) => {
const location = useLocation(); // Use location inside Router
Expand Down Expand Up @@ -121,6 +122,7 @@ function App() {
<Route exact path="/contactus" element={<ContactUs mode={mode} setProgress={setProgress} showAlert={showAlert} />} />
<Route exact path="/privacypolicy" element={<PrivacyPolicy mode={mode} setProgress={setProgress} showAlert={showAlert} />} />
<Route exact path="/termofuse" element={<TermOfUse mode={mode} setProgress={setProgress} showAlert={showAlert} />} />
<Route exact path="/verify/:token" element={<VerifyEmail/>} />
</Routes>
</Layout>
</Router>
Expand Down
71 changes: 71 additions & 0 deletions client/src/component/Verify.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import React, { useEffect, useState } from 'react';
import { useParams, useNavigate } from 'react-router-dom';

const host = "http://localhost:5000";

const VerifyEmail = () => {
const { token } = useParams();
const [isLoading, setIsLoading] = useState(true);
const [isVerified, setIsVerified] = useState(null);
const [modalMessage, setModalMessage] = useState('');
const [showModal, setShowModal] = useState(false);
const navigate = useNavigate();

const handleVerification = () => {
fetch(`${host}/api/auth/verify/${token}`, {
method: 'POST',
})
.then(response => {
if (response.status === 200) {
setIsVerified(true);
setModalMessage('Email verified! You can now log in.');
} else if (response.status === 400) {
setIsVerified(false);
setModalMessage('Invalid or expired verification link');
}
setShowModal(true);
})
.finally(() => {
setIsLoading(false);
});
};

useEffect(() => {
setIsLoading(true);
handleVerification();
}, [token]);

const handleModalClose = () => {
setShowModal(false);
if (isVerified) {
navigate('/login');
}else{
navigate('/signup')
}
};

return (
<div>
<h2>Verify Your Email</h2>
{isLoading ? (
<p>Verifying your email...</p>
) : (
<button onClick={handleVerification} disabled={isLoading}>
{isLoading ? 'Verifying...' : 'Verify Email'}
</button>
)}

{/* Modal Implementation */}
{showModal && (
<div className="modal-overlay">
<div className="modal-content">
<h3>{modalMessage}</h3>
<button onClick={handleModalClose}>Close</button>
</div>
</div>
)}
</div>
);
};

export default VerifyEmail;
2 changes: 1 addition & 1 deletion client/src/component/forgotpass.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const handleSubmit = async (e) => {

if (validateForm()) {
console.log("inforgot")
const response = await fetch(`${host}/api/auth/forgot`, {
const response = await fetch(`${host}/api/auth/forget`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Expand Down
43 changes: 42 additions & 1 deletion client/src/index.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,44 @@
@tailwind base;
@tailwind components;
@tailwind utilities;
@tailwind utilities;


.modal-overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5); /* Semi-transparent background */
display: flex;
justify-content: center;
align-items: center;
z-index: 1000;
}

.modal-content {
width: 400px;
background-color: white;
padding: 40px;
border-radius: 10px;
box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.1);
text-align: center;
}

.modal-content h3 {
margin-bottom: 15px;
}

.modal-content button {
background-color: #007bff;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
}

.modal-content button:hover {
background-color: #0056b3;
}

2 changes: 2 additions & 0 deletions server/.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
EMAIL_USER=""
EMAIL_PASS=""
4 changes: 3 additions & 1 deletion server/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ env.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
yarn-error.log*


118 changes: 118 additions & 0 deletions server/Controllers/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
const User = require("../Models/User");
const bcrypt = require("bcrypt");


const nodemailer = require('nodemailer');
const crypto = require('crypto');

// Signup route
const createUser= async (req, res) => {
const { name, email, password } = req.body;

// Create a new user (save in your database)
const user = new User({ name,email, password, verified: false });
await user.save();

// Generate verification token
const verificationToken = crypto.randomBytes(32).toString('hex');

// Save the verification token to the user (or in another collection)
user.verificationToken = verificationToken;
await user.save();

// Send verification email
const transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: process.env.EMAIL_USER, // Use the environment variable
pass: process.env.EMAIL_PASS, // Use the environment variable
},
});

const verificationLink = `http://localhost:5173/verify/${verificationToken}`;
const mailOptions = {
from: process.env.EMAIL_USER,
to: email,
subject: 'Email Verification',
text: `Click this link to verify your email: ${verificationLink}`,
};

transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return res.status(500).send('Error sending verification email');
}
res.status(200).send('Signup successful! Please check your email for verification link.');
});
};


const verifyToken = async (req, res) => {
const { token } = req.params;

try {
// Find the user by verification token
const user = await User.findOne({ verificationToken: token });
if (!user) {
return res.status(400).json({ success: false, message: 'Invalid or expired verification link' });
}
console.log(user)

// Mark user as verified
user.verified = true;
user.verificationToken = undefined; // Clear the token after verification
await user.save();

// Optionally send a success message to the client
res.status(200).json({ success: true, message: 'Email verified successfully!' });

// Redirect to the frontend's home page or login page
// You can choose where to redirect the user after verification
res.redirect('http://localhost:5173/'); // Replace with your frontend home URL
} catch (err) {
console.error(err);
res.status(500).json({ success: false, message: 'Server error. Please try again later.' });
}
};



const forgetpassword = async (req, res) => {

try {
const { email, password } = req.body;
// Find the user by email
const user = await User.findOne({ email }); // Use findOne instead of find
if (!user) {
return res
.status(404)
.json({ success: false, message: "User not found" });
}

// Hash the new password
const salt = await bcrypt.genSalt(10);
const secPass = await bcrypt.hash(password, salt);

// Update the user's password
user.password = secPass;

// Save the updated user
await user.save();

return res.json({
success: true,
message: "Password updated successfully",
});
} catch (err) {
console.error(err);
return res
.status(500)
.json({ success: false, message: "Internal server error" });
}
};


module.exports = {
forgetpassword,
createUser,
verifyToken,
};
2 changes: 2 additions & 0 deletions server/Controllers/profiles.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const Profile = require("../Models/Profile");

const createProfile =async (req, res) => {
const { name, phone, college, address } = req.body;
const profileFields = {
Expand Down
7 changes: 7 additions & 0 deletions server/Models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ const UserSchema = new Schema({
date: {
type: Date,
default: Date.now
},
verificationToken:{
type:String,
},
verified:{
type:Boolean,
default:false
}
});

Expand Down
Loading

0 comments on commit f699f24

Please sign in to comment.