Created
May 28, 2017 19:57
-
-
Save marco-mendes/e2171216d8cceeb456b8b93b9901b684 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
router.get('/', async (req, res) => { | |
const users = await User.find(); | |
return res.send({ users }); | |
}); | |
router.post('/login', async (req, res, next) => { | |
const { name, password } = req.body; | |
try { | |
const user = await User.findOne({ | |
name, | |
password: sha1(password) | |
}); | |
if (user) { | |
return res.send(user); | |
} | |
next({ msg: 'wrong username or password', status: 401 }); | |
} catch (err) { | |
next(err); | |
} | |
}); | |
router.post('/create', async (req, res, next) => { | |
const { name, password } = req.body; | |
try { | |
const token = `Token ${randomstring.generate(20)}${Date.now()}${randomstring.generate(20)}`; | |
let user = await User.findOne({ name }); | |
if (user) { | |
return next({ msg: 'user already existed', status: 403 }); | |
} | |
user = new User({ | |
name, | |
password: sha1(password), | |
role: 'user', | |
token, | |
}); | |
user = await user.save(); | |
return res.send(user); | |
} catch (err) { | |
next(err); | |
} | |
}); | |
export default router; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment