Untitled
unknown
plain_text
2 years ago
907 B
6
Indexable
const User = require('./models/user');
app.post('/signup', async (req, res) => {
try {
const { username, password } = req.body;
const user = new User({ username, password });
await user.save();
res.status(201).send('User created successfully');
} catch (error) {
console.error(error);
res.status(500).send('Internal Server Error');
}
});
app.post('/login', async (req, res) => {
try {
const { username, password } = req.body;
const user = await User.findOne({ username });
if (!user || !(await bcrypt.compare(password, user.password))) {
return res.status(401).send('Invalid username or password');
}
const token = jwt.sign({ username: user.username }, 'your-secret-key');
res.status(200).json({ token });
} catch (error) {
console.error(error);
res.status(500).send('Internal Server Error');
}
});
Editor is loading...
Leave a Comment