Skip to content

Commit 4336efc

Browse files
committed
Improve the Login,signup with Mongodb
1 parent db142dc commit 4336efc

49 files changed

Lines changed: 547 additions & 202 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.env.example

Lines changed: 0 additions & 1 deletion
This file was deleted.

README.md

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# 🌟 **GitHub Tracker** 🌟
2+
23
<!-- top -->
34

45
**Track Activity of Users on GitHub**
56

67
Welcome to **GitHub Tracker**, a web app designed to help you monitor and analyze the activity of GitHub users. Whether you’re a developer, a project manager, or just curious, this tool simplifies tracking contributions and activity across repositories! 🚀👩‍💻
78

89
<p align="center">
9-
<img src="public/crl.png" height="60px" alt="github-tracker">
10+
<img src="frontend/public/crl.png" height="60px" alt="github-tracker">
1011
</p>
1112
<table align="center">
1213
<thead align="center">
@@ -41,47 +42,93 @@ Welcome to **GitHub Tracker**, a web app designed to help you monitor and analyz
4142
---
4243

4344
## 🚀 Setup Guide
45+
4446
1. Clone the repository to your local machine:
47+
4548
```bash
4649
$ git clone https://github.com/yourusername/github-tracker.git
4750
```
4851

4952
2. Navigate to the project directory:
53+
5054
```bash
5155
$ cd github-tracker
5256
```
5357

5458
3. Run the frontend
59+
5560
```bash
61+
$ cd frontend
5662
$ npm i
5763
$ npm run dev
5864
```
5965

6066
4. Run the backend
67+
6168
```bash
69+
$ cd backend
6270
$ npm i
6371
$ npm start
6472
```
6573

74+
## 🔐 MongoDB & Authentication Setup
75+
76+
The application uses MongoDB for user authentication (Sign up, Login, Logout).
77+
78+
### Frontend Environment Setup
79+
80+
Create a `.env` file in the `frontend/` folder:
81+
82+
```
83+
VITE_BACKEND_URL=http://localhost:5000
84+
```
85+
86+
See `frontend/.env.example` for reference.
87+
88+
### Backend Environment Setup
89+
90+
Create a `.env` file in the `backend/` folder:
91+
92+
```
93+
MONGO_URI=mongodb+srv://alokhacs222729_db_user:IvORbhhzQg71OF2q@cluster0.gevcwty.mongodb.net/?appName=Cluster0
94+
PORT=5000
95+
SESSION_SECRET=your_session_secret_key_here
96+
```
97+
98+
See `backend/.env.example` for reference.
99+
100+
**Key Points:**
101+
102+
- The `MONGO_URI` connects to MongoDB Atlas for user database storage
103+
- Users can sign up with email and password
104+
- Passwords are securely hashed using bcryptjs
105+
- Sessions are maintained via express-session and Passport.js
106+
- Both frontend and backend are required for authentication to work
107+
66108
## 🧪 Backend Unit & Integration Testing with Jasmine
67109

68110
This project uses the Jasmine framework for backend unit and integration tests. The tests cover:
111+
69112
- User model (password hashing, schema, password comparison)
70113
- Authentication routes (signup, login, logout)
71114
- Passport authentication logic (via integration tests)
72115

73116
### Prerequisites
117+
74118
- **Node.js** and **npm** installed
75119
- **MongoDB** running locally (default: `mongodb://127.0.0.1:27017`)
76120

77121
### Installation
122+
78123
Install all required dependencies:
124+
79125
```sh
80126
npm install
81127
npm install --save-dev jasmine @types/jasmine supertest express-session passport passport-local bcryptjs
82128
```
83129

84130
### Running the Tests
131+
85132
1. **Start MongoDB** (if not already running):
86133
```sh
87134
mongod
@@ -92,24 +139,26 @@ npm install --save-dev jasmine @types/jasmine supertest express-session passport
92139
```
93140

94141
### Test Files
142+
95143
- `spec/user.model.spec.cjs` — Unit tests for the User model
96144
- `spec/auth.routes.spec.cjs` — Integration tests for authentication routes
97145

98146
### Jasmine Configuration
147+
99148
The Jasmine config (`spec/support/jasmine.mjs`) is set to recognize `.cjs`, `.js`, and `.mjs` test files:
149+
100150
```js
101-
spec_files: [
102-
"**/*[sS]pec.?(m)js",
103-
"**/*[sS]pec.cjs"
104-
]
151+
spec_files: ["**/*[sS]pec.?(m)js", "**/*[sS]pec.cjs"];
105152
```
106153

107154
### Troubleshooting
155+
108156
- **No specs found:** Ensure your test files have the correct extension and are in the `spec/` directory.
109157
- **MongoDB connection errors:** Make sure MongoDB is running and accessible.
110158
- **Missing modules:** Install any missing dev dependencies with `npm install --save-dev <module>`.
111159

112160
### What Was Covered
161+
113162
- Jasmine is set up and configured for backend testing.
114163
- All major backend modules are covered by unit/integration tests.
115164
- Tests are passing and verified.
@@ -131,8 +180,6 @@ spec_files: [
131180
</a>
132181
</div>
133182

134-
135-
136183
---
137184

138185
<p align="center">

backend/.env.sample

Lines changed: 0 additions & 3 deletions
This file was deleted.

backend/models/User.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ const UserSchema = new mongoose.Schema({
1616
type: String,
1717
required: true,
1818
},
19+
trackerHistory: {
20+
type: Array,
21+
default: [],
22+
},
23+
lastTrackedAt: {
24+
type: Date,
25+
default: Date.now,
26+
},
1927
});
2028

2129
UserSchema.pre('save', async function (next) {

backend/routes/auth.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,48 @@ router.get("/logout", (req, res) => {
3939
});
4040
});
4141

42+
// Get user tracker history
43+
router.get("/tracker-history", async (req, res) => {
44+
if (!req.user) {
45+
return res.status(401).json({ message: 'Not authenticated' });
46+
}
47+
try {
48+
const user = await User.findById(req.user.id).select('trackerHistory');
49+
res.status(200).json({ trackerHistory: user?.trackerHistory || [] });
50+
} catch (err) {
51+
res.status(500).json({ message: 'Error fetching tracker history', error: err.message });
52+
}
53+
});
54+
55+
// Save tracker search to history
56+
router.post("/tracker-history", async (req, res) => {
57+
if (!req.user) {
58+
return res.status(401).json({ message: 'Not authenticated' });
59+
}
60+
const { username, searchedAt } = req.body;
61+
try {
62+
const user = await User.findById(req.user.id);
63+
if (!user) {
64+
return res.status(404).json({ message: 'User not found' });
65+
}
66+
67+
// Remove duplicate if exists (keep only unique searches)
68+
user.trackerHistory = user.trackerHistory.filter(
69+
item => item.username.toLowerCase() !== username.toLowerCase()
70+
);
71+
72+
// Add new search at the beginning
73+
user.trackerHistory.unshift({ username, searchedAt: new Date(searchedAt) });
74+
75+
// Keep only last 10 searches
76+
user.trackerHistory = user.trackerHistory.slice(0, 10);
77+
user.lastTrackedAt = new Date();
78+
79+
await user.save();
80+
res.status(200).json({ message: 'Tracker history saved', trackerHistory: user.trackerHistory });
81+
} catch (err) {
82+
res.status(500).json({ message: 'Error saving tracker history', error: err.message });
83+
}
84+
});
85+
4286
module.exports = router;

backend/server.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,22 @@ require('./config/passportConfig');
1212
const app = express();
1313

1414
// CORS configuration
15-
app.use(cors('*'));
15+
app.use(cors({
16+
origin: process.env.FRONTEND_URL || 'http://localhost:5173',
17+
credentials: true
18+
}));
1619

1720
// Middleware
1821
app.use(bodyParser.json());
1922
app.use(session({
2023
secret: process.env.SESSION_SECRET,
2124
resave: false,
2225
saveUninitialized: false,
26+
cookie: {
27+
httpOnly: true,
28+
secure: false, // set to true in production with HTTPS
29+
sameSite: 'lax'
30+
}
2331
}));
2432
app.use(passport.initialize());
2533
app.use(passport.session());

docker-compose.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ services:
33
image: frontend
44
container_name: frontend-container
55
build:
6-
context: .
6+
context: ./frontend
77
dockerfile: Dockerfile.dev
88
ports:
99
- "5173:5173"
1010
env_file:
1111
- .env
1212
volumes:
13-
- .:/app
13+
- ./frontend:/app
1414
- /app/node_modules
1515
depends_on:
1616
- backend
@@ -39,7 +39,7 @@ services:
3939
image: frontend-prod
4040
container_name: frontend-prod-container
4141
build:
42-
context: .
42+
context: ./frontend
4343
dockerfile: Dockerfile.prod
4444
ports:
4545
- "3000:3000"
File renamed without changes.

0 commit comments

Comments
 (0)