Skip to content

Commit

Permalink
fix: add comment to the login code
Browse files Browse the repository at this point in the history
  • Loading branch information
Valimp committed Feb 1, 2024
1 parent cbcf2b7 commit 169588c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
5 changes: 5 additions & 0 deletions interface/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ export default function App() {
const refresh = useCallback(async () => {
// Get the session cookie
const sessionCookie = off.getCookie("session");
// If the session cookie is the same as the last seen cookie, return the current login state
if (sessionCookie === lastSeenCookie.current) {
return userState.isLoggedIn;
}
// If the session cookie is null, the user is not logged in
if (!sessionCookie) {
setUserState({
userName: "",
Expand All @@ -38,10 +40,12 @@ export default function App() {
lastSeenCookie.current = sessionCookie;
return false;
}
// If the session cookie is not null, send a request to the server to check if the user is logged in
const isLoggedIn = axios
.get("https://world.openfoodfacts.org/cgi/session.pl", {
withCredentials: true,
})
// If the request is successful, set the user state to logged in
.then(() => {
const cookieUserName = off.getUsername();
setUserState({
Expand All @@ -51,6 +55,7 @@ export default function App() {
lastSeenCookie.current = sessionCookie;
return true;
})
// If the request is not successful, set the user state to logged out
.catch(() => {
setUserState({
userName: "",
Expand Down
32 changes: 13 additions & 19 deletions interface/src/off.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
const offService = {
getCookie(name: any) {
const cookies = document.cookie
.split(";")
.filter((item) => item.trim().startsWith(`${name}=`));
if (cookies.length) {
const cookie = cookies[0];
return cookie.split("=", 2)[1];
}
return "";
},
// Get cookie by name return empty string if not found
getCookie(name: any) {
const cookies = document.cookie
.split(";")
.filter((item) => item.trim().startsWith(`${name}=`));
if (cookies.length) {
const cookie = cookies[0];
return cookie.split("=", 2)[1];
}
return "";
},

// Get user id from cookie return empty string if not found
getUsername() {
const sessionCookie = this.getCookie("session");

Expand All @@ -31,12 +33,4 @@ getCookie(name: any) {
},
};

export default offService;

// Fetching products to annotate:

// https://world.openfoodfacts.org/cgi/search.pl?page=0&page_size=25&json=true&action=process&fields=code,lang,image_ingredients_url,product_name,ingredient,images&tagtype_0=states&tag_contains_0=contains&tag_0=en%3Aingredients-to-be-completed&tagtype_1=states&tag_contains_1=contains&tag_1=en%3Aingredients-photo-selected

// Getting prediction:
// https://robotoff.openfoodfacts.org/api/v1/predict/ingredient_list?ocr_url=https://images.openfoodfacts.org/images/products/505/382/713/9229/41.json
// https://images.openfoodfacts.org/images/products/505/382/713/9229/41.json
export default offService;

0 comments on commit 169588c

Please sign in to comment.