-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Conflicts: # src/resources/datarequest/datarequest.controller.js
- Loading branch information
Showing
50 changed files
with
5,900 additions
and
1,285 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 |
---|---|---|
|
@@ -9,6 +9,8 @@ | |
# production | ||
/build | ||
|
||
/tmp | ||
|
||
# misc | ||
.DS_Store | ||
npm-debug.log* | ||
|
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
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,72 +1,99 @@ | ||
import express from 'express' | ||
import { to } from 'await-to-js' | ||
import { verifyPassword } from '../auth/utils' | ||
import { login } from '../auth/strategies/jwt' | ||
import { getUserByEmail } from '../user/user.repository' | ||
import { getRedirectUrl } from '../auth/utils' | ||
import passport from "passport"; | ||
import express from 'express'; | ||
import { to } from 'await-to-js'; | ||
import { verifyPassword } from '../auth/utils'; | ||
import { login } from '../auth/strategies/jwt'; | ||
import { getUserByEmail } from '../user/user.repository'; | ||
import { getRedirectUrl } from '../auth/utils'; | ||
import passport from 'passport'; | ||
|
||
const router = express.Router(); | ||
|
||
// @router POST /api/auth/login | ||
// @desc login user | ||
// @access Public | ||
router.post('/login', async (req, res) => { | ||
const { email, password } = req.body | ||
const { email, password } = req.body; | ||
|
||
const [err, user] = await to(getUserByEmail(email)) | ||
const [err, user] = await to(getUserByEmail(email)); | ||
|
||
const authenticationError = () => { | ||
return res | ||
.status(500) | ||
.json({ success: false, data: "Authentication error!" }) | ||
} | ||
|
||
if (!(await verifyPassword(password, user.password))) { | ||
console.error('Passwords do not match') | ||
return authenticationError() | ||
} | ||
const authenticationError = () => { | ||
return res | ||
.status(500) | ||
.json({ success: false, data: 'Authentication error!' }); | ||
}; | ||
|
||
const [loginErr, token] = await to(login(req, user)) | ||
if (!(await verifyPassword(password, user.password))) { | ||
console.error('Passwords do not match'); | ||
return authenticationError(); | ||
} | ||
|
||
if (loginErr) { | ||
console.error('Log in error', loginErr) | ||
return authenticationError() | ||
} | ||
const [loginErr, token] = await to(login(req, user)); | ||
|
||
return res | ||
.status(200) | ||
.cookie('jwt', token, { | ||
httpOnly: true | ||
}) | ||
.json({ | ||
success: true, | ||
data: getRedirectUrl(req.user.role) | ||
}) | ||
if (loginErr) { | ||
console.error('Log in error', loginErr); | ||
return authenticationError(); | ||
} | ||
|
||
return res | ||
.status(200) | ||
.cookie('jwt', token, { | ||
httpOnly: true, | ||
}) | ||
.json({ | ||
success: true, | ||
data: getRedirectUrl(req.user.role), | ||
}); | ||
}); | ||
|
||
// @router POST /api/auth/logout | ||
// @desc logout user | ||
// @access Private | ||
router.get('/logout', function (req, res) { | ||
req.logout(); | ||
res.clearCookie('jwt'); | ||
return res.json({ success: true }); | ||
for (var prop in req.cookies) { | ||
res.clearCookie(prop); | ||
} | ||
return res.json({ success: true }); | ||
}); | ||
|
||
// @router GET /api/auth/status | ||
// @desc Return the logged in status of the user and their role. | ||
// @access Private | ||
router.get('/status', function (req, res, next) { | ||
passport.authenticate('jwt', function (err, user, info) { | ||
if (err || !user) { | ||
return res.json({ success: true, data: [{ role: "Reader", id: null, name: null, loggedIn: false }] }); | ||
} | ||
else { | ||
return res.json({ success: true, data: [{ role: req.user.role, id: req.user.id, name: req.user.firstname + " " + req.user.lastname, loggedIn: true, teams: req.user.teams }] }); | ||
} | ||
})(req, res, next); | ||
passport.authenticate('jwt', function (err, user, info) { | ||
if (err || !user) { | ||
return res.json({ | ||
success: true, | ||
data: [{ role: 'Reader', id: null, name: null, loggedIn: false }], | ||
}); | ||
} else { | ||
// 1. Reformat teams array for frontend | ||
let { teams } = req.user; | ||
if(teams) { | ||
teams = teams.map((team) => { | ||
let { publisher, type, members } = team; | ||
let member = members.find(member => { | ||
return member.memberid.toString() === req.user._id.toString(); | ||
}); | ||
let { roles } = member; | ||
return { ...publisher.toObject(), type, roles }; | ||
}); | ||
} | ||
// 2. Return user info | ||
return res.json({ | ||
success: true, | ||
data: [ | ||
{ | ||
role: req.user.role, | ||
id: req.user.id, | ||
name: req.user.firstname + ' ' + req.user.lastname, | ||
loggedIn: true, | ||
teams, | ||
}, | ||
], | ||
}); | ||
} | ||
})(req, res, next); | ||
}); | ||
module.exports = router | ||
|
||
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
Oops, something went wrong.