-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
User is logged in, now we need to create the dashboard
- Loading branch information
Christian Martinez
committed
Jul 25, 2023
1 parent
df8aa5b
commit 7bde937
Showing
7 changed files
with
73 additions
and
13 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const router = require('express').Router() | ||
const userRoutes = require('./user-routes') | ||
|
||
router.use('/users', userRoutes) | ||
|
||
module.exports = router |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
const router = require('express').Router() | ||
const { User } = require('../../models') | ||
|
||
router.post('/', async (req, res) => { | ||
try { | ||
const userData = await User.create(req.body) | ||
|
||
req.session.save(() => { | ||
req.session.user_id = userData.id | ||
req.session.logged_in = true | ||
|
||
res.redirect('/login') | ||
}) | ||
} catch (err) { | ||
res.status(400).json(err) | ||
} | ||
}) | ||
|
||
router.post('/login', async (req, res) => { | ||
try { | ||
const userData = await User.findOne({ where: { email: req.body.email } }) | ||
|
||
if (!userData) { | ||
res.status(400).json({ message: 'Incorrect email or password' }) | ||
return | ||
} | ||
|
||
const validPassword = await userData.checkPassword(req.body.password) | ||
|
||
if (!validPassword) { | ||
res | ||
.status(400) | ||
.json({ message: 'Incorrect email or password, please try again' }) | ||
return | ||
} | ||
|
||
req.session.save(() => { | ||
req.session.user_id = userData.id | ||
req.session.logged_in = true | ||
res.redirect('/dashboard') | ||
res.render('dashboard', { userData, logged_in: req.session.logged_in }) | ||
}) | ||
} catch (err) { | ||
res.status(400).json(err) | ||
} | ||
}) | ||
|
||
router.post('/logout', (req, res) => { | ||
if (req.session.logged_in) { | ||
req.session.destroy(() => { | ||
res.status(204).end() | ||
}) | ||
} else { | ||
res.status(404).end() | ||
} | ||
}) | ||
|
||
module.exports = router |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
const router = require('express').Router() | ||
const homeRoutes = require('./home-routes') | ||
const apiRoutes = require('./api') | ||
//const dashboardRoutes = require('./dashboard-routes') | ||
|
||
router.use('/', homeRoutes) | ||
router.use('/api', apiRoutes) | ||
//router.use('/dashboard', dashboardRoutes) | ||
|
||
module.exports = router |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,7 +35,7 @@ User.init( | |
type: DataTypes.STRING, | ||
allowNull: false, | ||
validate: { | ||
len: [4], | ||
len: [6], | ||
}, | ||
}, | ||
}, | ||
|
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
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