1
2
3
4
5
6
7
8
9
10
11
12
13
14
| user-service/
├── controllers/
│ └── auth.controller.js
├── models/
│ └── user.model.js
├── routes/
│ └── auth.routes.js
├── middlewares/
│ └── validateToken.js
├── utils/
│ └── jwt.js
├── .env
├── server.js
├── package.json
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| // models/user.model.js
const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');
const userSchema = new mongoose.Schema({
name: String,
email: { type: String, unique: true },
password: String,
role: { type: String, default: 'customer' }
});
userSchema.pre('save', async function(next) {
if (this.isModified('password')) {
this.password = await bcrypt.hash(this.password, 10);
}
next();
});
module.exports = mongoose.model('User', userSchema);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
| // controllers/auth.controller.js
const User = require('../models/user.model');
const jwt = require('jsonwebtoken');
const bcrypt = require('bcryptjs');
exports.register = async (req, res) => {
const { name, email, password } = req.body;
try {
const user = new User({ name, email, password });
await user.save();
res.status(201).json({ message: 'User registered successfully.' });
} catch (err) {
res.status(400).json({ error: 'Registration failed.' });
}
};
exports.login = async (req, res) => {
const { email, password } = req.body;
try {
const user = await User.findOne({ email });
if (!user || !await bcrypt.compare(password, user.password))
return res.status(401).json({ error: 'Invalid credentials' });
const token = jwt.sign({ userId: user._id, role: user.role }, process.env.JWT_SECRET);
res.json({ token });
} catch (err) {
res.status(500).json({ error: 'Login failed.' });
}
};
|