Stop checking users once match is found.

This commit is contained in:
kristian 2022-09-10 22:04:17 -07:00
parent 5a3fbe5672
commit 46420d4f15
1 changed files with 7 additions and 7 deletions

View File

@ -54,20 +54,20 @@ const generateUserToken = (user) => {
*/ */
export const isLoggedIn = () => { export const isLoggedIn = () => {
const users = getUsers(); const users = getUsers();
let userAuthenticated = false; let userAuthenticated = document.cookie.split(';').some((cookie) => {
document.cookie.split(';').forEach((cookie) => {
if (cookie && cookie.split('=').length > 1) { if (cookie && cookie.split('=').length > 1) {
const cookieKey = cookie.split('=')[0].trim(); const cookieKey = cookie.split('=')[0].trim();
const cookieValue = cookie.split('=')[1].trim(); const cookieValue = cookie.split('=')[1].trim();
if (cookieKey === cookieKeys.AUTH_TOKEN) { if (cookieKey === cookieKeys.AUTH_TOKEN) {
users.forEach((user) => { userAuthenticated = users.some((user) => {
if (generateUserToken(user) === cookieValue) { if (generateUserToken(user) === cookieValue) {
userAuthenticated = true;
localStorage.setItem(localStorageKeys.USERNAME, user.user); localStorage.setItem(localStorageKeys.USERNAME, user.user);
} return true;
} else return false;
}); });
} return userAuthenticated;
} } else return false;
} else return false;
}); });
return userAuthenticated; return userAuthenticated;
}; };