diff --git a/index.html b/index.html index 9132d0a6..2e1c4da5 100644 --- a/index.html +++ b/index.html @@ -58,7 +58,9 @@

Your direct access to Noroff's best candidates

+
+

Your direct access to Noroff's best candidates

@@ -89,6 +91,7 @@

+

Your direct access to industry

Meet your next employer today. Showcase your unique diff --git a/pages/listings/index.html b/pages/listings/index.html index f46a834a..ec61753f 100644 --- a/pages/listings/index.html +++ b/pages/listings/index.html @@ -23,45 +23,70 @@

+
+ -
-

Job Listings

-
- -
- - Search icon -
-
diff --git a/src/js/api/auth/login.js b/src/js/api/auth/login.js index 3144d578..2a517453 100644 --- a/src/js/api/auth/login.js +++ b/src/js/api/auth/login.js @@ -1,42 +1,6 @@ import { Store } from '../../storage/storage.js'; import { callLoginApi } from './handleAuthServices.js'; -/** - * Function for logging in an existing user in database and storing the returned token in session or localstorage - * @param {object} profile Values from loginForm - * @param {string} profile.email Email of the user - * @param {string} profile.password Plain text password - * @param {string} [profile.remember] If the user checkbox is checked it will equal to the string 'on' - * @returns {void} - */ -export async function login(profile) { - const { remember, email, password } = profile; - - const rememberLogin = remember === 'on'; - - const errorContainer = document.querySelector('#errorContainer'); - - try { - const { userData, error } = await callLoginApi(email, password); - - if (error) { - return (errorContainer.innerHTML = error?.message); - } - - const { role } = userData; - - clearProfileData(Store); // Clear any previous data - storeProfileData(userData, rememberLogin, Store); - - const redirectUrl = getRedirectUrl(role); - handleLoginRedirect(redirectUrl); - } catch (error) { - errorContainer.innerHTML = - 'Unknown error occurred. Please try again later, if the problem persist contact customer support.'; - console.error(error); - } -} - /** * @param {UserData} userData - The user data to store. * @param {boolean} rememberLogin - Whether or not to remember the login @@ -51,6 +15,8 @@ function storeProfileData(userData, rememberLogin, Store) { new Store('id', id, rememberLogin); } + + /** * @param {Store} Store - The store class. */ @@ -77,3 +43,62 @@ function getRedirectUrl(role) { return '/pages/user/index.html'; } } + + +/** + * Function for logging in an existing user in database and storing the returned token in session or localstorage + * @param {object} profile Values from loginForm + * @param {string} profile.email Email of the user + * @param {string} profile.password Plain text password + * @param {string} [profile.remember] If the user checkbox is checked it will equal to the string 'on' + * @returns {void} + */ +export async function login(profile) { + const { remember, email, password } = profile; + + const rememberLogin = remember === 'on'; + + const errorContainer = document.querySelector('#errorContainer'); + + try { + const { userData, error } = await callLoginApi(email, password); + + if (error) { + return (errorContainer.innerHTML = error?.message); + } + + const { role } = userData; + + + if (id === id) { + // spiderman.gif + new Store('Role', 'user', Boolean(remember !== 'on')); + window.location.replace('../../../pages/user/index.html'); + + } else if (profile.admin) { + new Store('Role', 'admin', Boolean(remember !== 'on')); + window.location.replace('#'); // TODO: Add admin page url + } else { + window.location.replace('../../../pages/user/index.html'); + } + break; + case 403: + errorContainer.innerHTML = 'Incorrect username/password'; + break; + default: + throw new Error(`${response.status} ${response.statusText}`); + } + + clearProfileData(Store); // Clear any previous data + storeProfileData(userData, rememberLogin, Store); + + const redirectUrl = getRedirectUrl(role); + handleLoginRedirect(redirectUrl); + + } catch (error) { + errorContainer.innerHTML = + 'Unknown error occurred. Please try again later, if the problem persist contact customer support.'; + console.error(error); + } +} + diff --git a/src/js/api/users/editStudent.js b/src/js/api/users/editStudent.js index 72ede364..8160a14e 100644 --- a/src/js/api/users/editStudent.js +++ b/src/js/api/users/editStudent.js @@ -13,15 +13,13 @@ const action = 'users/'; */ export async function editStudent(profile) { - // const { id } = profile; - const id = localStorage.getItem('id'); - const newId = id.replace(/^"|"$/g, ''); - - const profileURL = apiPath + action + newId; + + const id = JSON.parse(localStorage.getItem('id')); + console.log(id); + const profileURL = apiPath + action + `${id}`; const accessToken = getToken('token'); const newAccessToken = accessToken.replace(/^"|"$/g, ''); - const body = JSON.stringify(profile); const options = { method, diff --git a/src/js/listeners/auth/logoutListener.js b/src/js/listeners/auth/logoutListener.js index cdb3d255..4b1c7dd5 100644 --- a/src/js/listeners/auth/logoutListener.js +++ b/src/js/listeners/auth/logoutListener.js @@ -1,5 +1,5 @@ import { logout } from '../../api/auth/index.js'; // The element id is temporary. need to replace it with the right one. - -// document.quaryselector('[data-auth=logout]').addEventListener('click', logout); +const logoutButton = document.querySelector('#signOut'); +console.log(logoutButton); diff --git a/src/js/ui/footer/index.js b/src/js/ui/footer/index.js index 11c4c7ba..f22aa9b7 100644 --- a/src/js/ui/footer/index.js +++ b/src/js/ui/footer/index.js @@ -24,6 +24,7 @@ export const footer = () => {
  • Tordenskjoldsgate 9
  • 4612 Kristiansand S
  • +
    phone icon for footer navigation 38000000 email icon for footer navigation utdanning@noroff.no diff --git a/src/js/ui/header/index.js b/src/js/ui/header/index.js index 5fc7ce98..82302a06 100644 --- a/src/js/ui/header/index.js +++ b/src/js/ui/header/index.js @@ -1,3 +1,5 @@ +// import { checkLoginStatus } from '../../ui/header/logoutRender.js'; + /** * * This function is creating the content inside the header tag on each page @@ -15,7 +17,9 @@ export const header = () => {