diff options
author | 2021-08-09 21:36:03 +0200 | |
---|---|---|
committer | 2021-08-09 21:37:03 +0200 | |
commit | 464e470441287572cfda8d95484f781236b9db35 (patch) | |
tree | 87177837cb6ee6ee000f0d39fa5ba7ee6bb2943e /pages/api/login.js | |
download | my_apps-464e470441287572cfda8d95484f781236b9db35.tar.gz my_apps-464e470441287572cfda8d95484f781236b9db35.tar.bz2 my_apps-464e470441287572cfda8d95484f781236b9db35.zip |
init commit
Diffstat (limited to 'pages/api/login.js')
-rw-r--r-- | pages/api/login.js | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/pages/api/login.js b/pages/api/login.js new file mode 100644 index 0000000..30edda1 --- /dev/null +++ b/pages/api/login.js @@ -0,0 +1,29 @@ +import withSession from 'lib/withSession' +import dbConnect from 'lib/dbConnect' +import User from 'models/User' + +export default withSession(async (req, res) => { + const {method} = req + + await dbConnect() + + switch (method) { + case 'POST': + try { + const {_id, email, isVerified, noteList} = await User.findByCredentials(req.body.email, req.body.password); + if (!email) { throw new Error('Something went wrong') } + + const user = {_id, email, isVerified, isLoggedIn: true, noteList} + req.session.set('user', user) + await req.session.save() + res.status(201).json(user) + } catch (error) { + res.status(400).json({isLoggedIn: false}) + } + break + default: + res.status(400).send({isLoggedIn: false}) + break + } +}) + |