How to detect authentication cookie and toggle Sign In / Sign Up buttons in JavaScript [duplicate]

4 weeks ago 33
ARTICLE AD BOX
document.querySelector('#loginForm').addEventListener('submit', () => { setCookie('user', document.querySelector('#uname').value, '/') setCookie('pass', document.querySelector('#pwd').value, '/') }) if(!getCookie('user')||!getCookie('pass')) if(location.href != 'https://somelocation.example/index.html/') location.replace('https://somelocation.example/index.html/') // Cookies setting and getting functions function setCookie(name, value, path, options = {}) { options = { path: path, ...options }; if (options.expires instanceof Date) { options.expires = options.expires.toUTCString(); } let updatedCookie = encodeURIComponent(name) + "=" + encodeURIComponent(value) for (let optionKey in options) { updatedCookie += "; " + optionKey let optionValue = options[optionKey] if (optionValue !== true) { updatedCookie += "=" + optionValue } } document.cookie = updatedCookie; } function getCookie(name) { let matches = document.cookie.match(new RegExp( "(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)" )) return matches ? decodeURIComponent(matches[1]) : undefined }

Explaining:

1.0 Event:

There is event to set values in cookie when using submitting form (functions explaining at 1.2)

1.1 Checking cookies:

Then there is checking if cookie "user" and "pass" do not exist, then you are being redirected

1.2 Functions:

1.2.0 setCookie:

First we are getting path and options that user set, then checking if expires function is in Date format (e.g. 1653420565221), and if it's true then converting to UTC string. (Skipped part with for...in loop because not in use) After all, cookies setting to new one.
1.2.1 getCookie:
Just getting and encoding cookie property using match(), and if it's not exist, returning undefined.

answered May 24, 2022 at 19:33

NNL993's user avatar

In my experience, you need to use PHP and a database because if you just use javascript, the users won't be able to access their accounts if they simply clear their cookies.

Sorry for not being very insightful on how to answer your question, but PHP would be the first step, someone else can elaborate on how to get the PHP set up, because it is not my specialty.

Edit:

<!DOCTYPE html> <html> <head> <script> function setCookie(cname,cvalue,exdays) { const d = new Date(); d.setTime(d.getTime() + (exdays*24*60*60*1000)); let expires = "expires=" + d.toUTCString(); document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/"; } function getCookie(cname) { let name = cname + "="; let decodedCookie = decodeURIComponent(document.cookie); let ca = decodedCookie.split(';'); for(let i = 0; i < ca.length; i++) { let c = ca[i]; while (c.charAt(0) == ' ') { c = c.substring(1); } if (c.indexOf(name) == 0) { return c.substring(name.length, c.length); } } return ""; } function checkCookie() { let user = getCookie("username"); if (user != "") { alert("Welcome again " + user); } else { user = prompt("Please enter your name:",""); if (user != "" && user != null) { setCookie("username", user, 30); } } } </script> </head> <body onload="checkCookie()"></body> </html>

source of code: w3schools

answered May 24, 2022 at 19:07

Altify's user avatar

2 Comments

Disagreeing with you, one thing you said right, that author of question needs database, but other things you can realize with JavaScript.

2022-05-24T19:11:30.323Z+00:00

It isn't supposed to be saved in a database, just some sort of a showcase of how to use a cookie where it remembers the username and password and prints it on every page, but when you press logout it deletes it from the cookie and moves you back to the login page.

2022-05-24T19:12:42.103Z+00:00

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Read Entire Article