Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. app.post('/session/login', (req, res) => {
  2. let userId = req.body.loginUsername
  3. let password = req.body.loginPassword
  4. let userObj = {
  5. userId: req.body.loginUsername,
  6. password: req.body.loginPassword
  7. }
  8.  
  9. // Login function to contact Work 10 API
  10. function w10APISignIn (userObj) {
  11. const postData = JSON.stringify({
  12. 'user_id': `${userObj.userId}`,
  13. 'password': `${userObj.password}`,
  14. 'persona': 'user',
  15. 'application_name': 'TEST'
  16. })
  17. let requestOptions = {
  18. host: serverName,
  19. port: 443,
  20. path: '/api/v1/session/login',
  21. rejectUnauthorized: false,
  22. method: 'PUT',
  23. headers: {
  24. 'Accept': '*/*',
  25. 'Content-Type': 'application/json',
  26. 'Content-length': postData.length
  27. }
  28. }
  29.  
  30. let loginRequest = https.request(requestOptions, function (response) {
  31. console.log(`Server Status: ${res.statusCode}`)
  32. let body = ''
  33. response.setEncoding('UTF-8')
  34. JSON.stringify(res.headers)
  35. response.on('data', function (chunk) {
  36. body += chunk
  37. })
  38.  
  39. response.on('end', function () {
  40. console.log(body)
  41. // return JSON.parse(body)
  42. })
  43. })
  44.  
  45. loginRequest.on('error', (err) => {
  46. console.error(err.message)
  47. return err.message
  48. })
  49.  
  50. loginRequest.write(postData)
  51. loginRequest.end()
  52.  
  53. }
  54.  
  55. // This returns undefined (wsApiLoginObj)
  56. let wsApiLoginObj = w10APISignIn(userObj)
  57. console.log(wsApiLoginObj)
  58. if (wsApiLoginObj) {
  59. return res.redirect(302, `/session/home/${wsApiLoginObj.user.email}`)
  60. } else {
  61. console.log('There was an issue logging in.')
  62. res.redirect(302, '/session/loginError')
  63. }
  64.  
  65. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement