Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions backend/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#SESSION_SECRET=your_secret_here
MONGO_URI=mongodb://localhost:27017/github_tracker
PORT=5000
8 changes: 8 additions & 0 deletions backend/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,12 @@ UserSchema.methods.comparePassword = async function (enteredPassword) {
return bcrypt.compare(enteredPassword, this.password);
};

UserSchema.methods.toSafeObject = function () {
return {
id: this._id,
username: this.username,
email: this.email,
};
};

module.exports = mongoose.model("User", UserSchema);
2 changes: 1 addition & 1 deletion backend/routes/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ router.post("/signup", validateRequest(signupSchema), async (req, res) => {

// Login route
router.post("/login", validateRequest(loginSchema), passport.authenticate('local'), (req, res) => {
res.status(200).json( { message: 'Login successful', user: req.user } );
res.status(200).json({ message: 'Login successful', user: req.user.toSafeObject() });
});

// Logout route
Expand Down
9 changes: 9 additions & 0 deletions backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ const app = express();
// CORS configuration
app.use(cors('*'));

const REQUIRED_ENV = ['SESSION_SECRET', 'MONGO_URI'];

for (const key of REQUIRED_ENV) {
if (!process.env[key]) {
console.error(`[startup] Missing required environment variable: ${key}`);
process.exit(1);
}
}

// Middleware
app.use(bodyParser.json());
app.use(session({
Expand Down
Loading