From 0e05f12dce708fe4d166a86cc25046dffd541380 Mon Sep 17 00:00:00 2001 From: Ebenezer Date: Sat, 10 Nov 2018 12:26:13 +0100 Subject: [PATCH] Add js file for login Add cors to app.js --- .eslintrc.json | 7 ++++++- UI/assets/js/login.js | 45 +++++++++++++++++++++++++++++++++++++++++++ UI/index.html | 19 ++++-------------- app.js | 2 ++ 4 files changed, 57 insertions(+), 16 deletions(-) create mode 100644 UI/assets/js/login.js diff --git a/.eslintrc.json b/.eslintrc.json index f07cb20..2976fef 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -2,6 +2,7 @@ "root": true, "extends": "airbnb-base", "env": { + "browser": true, "node": true, "es6": true, "mocha": true @@ -12,5 +13,9 @@ }, "rules": { "linebreak-style": ["error", "windows"] - } + }, + "globals": { + "localStorage": true, + "fetch": true +} } diff --git a/UI/assets/js/login.js b/UI/assets/js/login.js new file mode 100644 index 0000000..88c8f0b --- /dev/null +++ b/UI/assets/js/login.js @@ -0,0 +1,45 @@ +// const loginUser = () => { +// const emailaddress = document.getElementById('emailaddress').value; +// const password = document.getElementById('password').value; +// fetch('https://newstoremanager.herokuapp.com/api/v1/auth/login', { +// method: 'POST', +// headers: { +// Accept: 'application/json, text/plain, */*', +// 'content-Type': 'application/json', +// }, +// body: JSON.stringify({ +// emailaddress: emailaddress, +// password: password, +// }), +// }).then(res => res.json()) +// .then((data) => { +// console.log(data); +// if (data.status === 200) { +// localStorage.setItem('accesstoken', `${data.data}`); +// } +// }); +// }; + +// document.getElementById('login').addEventListener('submit', loginUser); +const loginDetails = { + emailaddress: document.getElementById('emailaddress').value, + password: document.getElementById('password').value, +}; + +const options = { + method: 'POST', + body: JSON.stringify(loginDetails), + headers: new Headers({ + 'Content-Type': 'application/json' + }), +}; + +const loginUser = (e) => { + e.preventDefault(); + fetch('https://newstoremanager.herokuapp.com/api/v1/auth/login', options) + .then(res => res.json()) + .then(data => console.log(data)) + .catch(err => console.log(err)); +}; + +document.getElementById('login').addEventListener('submit', loginUser); \ No newline at end of file diff --git a/UI/index.html b/UI/index.html index 94ec62d..1dff679 100644 --- a/UI/index.html +++ b/UI/index.html @@ -8,7 +8,6 @@ - @@ -21,18 +20,7 @@ - - -
+ + \ No newline at end of file diff --git a/app.js b/app.js index 8beb6d2..41af207 100644 --- a/app.js +++ b/app.js @@ -2,6 +2,7 @@ import express from 'express'; import bodyParser from 'body-parser'; import cookieParser from 'cookie-parser'; import logger from 'morgan'; +import cors from 'cors'; import router from './server/routes/index'; const app = express(); @@ -13,6 +14,7 @@ app.use(logger('dev')); app.use(express.json()); app.use(express.urlencoded({ extended: false })); app.use(cookieParser()); +app.use(cors()); // use body parser to parse request app.use(bodyParser.urlencoded({ extended: true }));