Newer
Older
import { useAuthStore } from "@/stores/authStore.js";
import jwt_decode from "jwt-decode";
import router from "@/router/index";
* API method to send a login request.
* If login succeeds, the logged in User and their token
* is saved to the Pinia AuthStore
*
* @param email email address of the user to log in as
* @param password password to log in with
* @returns a Result with whether the login attempt succeeded
login: async (request) => {
const authStore = useAuthStore();
let token;
authStore.logout(); //in case someone for some reason is logged in
return axios.post(
`${import.meta.env.VITE_BACKEND_URL}/login`,
request,
)
.then(async (response) => {
token = response.data;
const id = (jwt_decode(token)).id;
API.getProfiles()
.then(response => {authStore.setProfiles(response)})
.catch(() => {throw new Error()})
return;
})
.catch(() => {
throw new Error();
});
})
.catch(() => {
throw new Error();
},
/**
* API method to get a account by their ID
* @param id ID number of the account to retrieve
* @returns A promise that resolves to a User if the API call succeeds,
* or is rejected if the API call fails
*/
getAccount: async (id, token) => {
return axios.get(`${import.meta.env.VITE_BACKEND_URL}/account/${id}`, {
headers: { Authorization: `Bearer ${token}` },
})
.then((response) => {
return response.data;
})
.catch(() => {
throw new Error("Account not found or not accessible");
});
},
// Sends the user into the home page logged in as the profile they clicked on
selectProfile: async (id) => {
const authStore = useAuthStore()
return axios.get(`${import.meta.env.VITE_BACKEND_URL}/profile/${id}`, {
headers: { Authorization: `Bearer ${authStore.token}` },
})
.then((response) => {
authStore.setProfile(response.data)
router.push("/")
})
.catch(() => {
throw new Error("Profile not found or not accessible")
})
},
/**
* Sends a request to create a new profile on the currently logged in account
* @typedef {{name: string, profileImageUrl: string, isRestricted: boolean}} ProfileType
addProfile: async (profile) => {
const authStore = useAuthStore();
if (!authStore.isLoggedIn) {
throw new Error();
}
return axios.post(import.meta.env.VITE_BACKEND_URL + '/profile', {
headers: { Authorization: "Bearer " + authStore.token },
body: profile
})
.then((response) => {
return response.data;
}).catch(() => {
throw new Error();
})
},
// Returns all profiles to the logged in user
getProfiles: async () => {
const authStore = useAuthStore();
if (!authStore.isLoggedIn) {
throw new Error();
}
return axios.get(import.meta.env.VITE_BACKEND_URL + '/profile', {
headers: { Authorization: "Bearer " + authStore.token },
},
)
.then(response => {
return response.data
}).catch(() => {
throw new Error();
});
// Registers a new account and logs into it
addAccount: async (request) => {
const authStore = useAuthStore();
axios.post(import.meta.env.VITE_BACKEND_URL + '/account', request)
.then(() => {
API.login({email: request.email, password: request.password})
.catch(err => {console.log(err)})
})
.catch(() => {throw new Error()})
* fetches all fridge items belonging to the user that is currently logged in
getFridgeItems: async () =>{
const authStore = useAuthStore();
return axios.get(`${import.meta.env.VITE_BACKEND_URL}/fridge`, {
headers: { Authorization: `Bearer ${authStore.token}` },
})
.then((response) => {
return response.data;
}).catch(() => {
throw new Error("Could not fetch fridge items");
});
},
/**
* Adds item(s) to the fridge
* @param request List<Ingredient> listOfIngredients
* @returns {Promise<void>}
*/
addToFridge: async(request) =>{
axios.post(`${import.meta.env.VITE_BACKEND_URL}/fridge/items`, request,{
headers: { Authorization: `Bearer ${authStore.token}` },
}).then((response) => {
}).catch(()=> {
throw new Error("Could not add item to fridge: ");
})
},
/**
* Searches for registered items.
* @param searchPhrase Name of the item that one is looking for (e.g: "purre")
* @returns {Promise<*>} list containing items that match the search phrase
searchItems: async(searchPhrase)=> {
return axios.get(`${import.meta.env.VITE_BACKEND_URL}/item/search?name=${searchPhrase}`, {
}).then((response) => {
console.log(response.data.content);
return response.data.content;
}).catch(()=> {
throw new Error("Error when searching for item ");
})
},
/**
* Removes an amount of an ingredient from the frodge, and the action is then logged
* @param request Log.Action action, Map<Integer, Amount>
* Action available: CONSUMED, DISCARDED,ADDED_TO_FRIDGE
* @returns {Promise<void>}
*/
Ingrid Martinsheimen Egge
committed
updateFridge: async(request) => {
const authStore = useAuthStore();
Ingrid Martinsheimen Egge
committed
axios.put(`${import.meta.env.VITE_BACKEND_URL}/fridge/ingredientsAmount`, request,{
headers: { Authorization: `Bearer ${authStore.token}` },
}).then((response) => {
return response.data;
}).catch(()=> {
throw new Error("Could modify ingredient. ");
})
},
/**
* Changes the expiration date of an ingredient.
Ingrid Martinsheimen Egge
committed
* @param request ingredientId: id of the ingredient, newExpDate: the new expiration date of the ingredient
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
* @returns {Promise<void>}
*/
changeExpirationOfIngredient: async(request) => {
const authStore = useAuthStore();
axios.put(`${import.meta.env.VITE_BACKEND_URL}/fridge/ingredient`, request,{
headers: { Authorization: `Bearer ${authStore.token}` },
}).then((response) => {
return response.data;
}).catch(()=> {
throw new Error("Could modify ingredient. ");
})
},
//returns fridgeItem of specific id
getFridgeItem: async (id) =>{
const authStore = useAuthStore();
axios.get(`${import.meta.env.VITE_BACKEND_URL}/fridge/${id}`, {
headers: { Authorization: `Bearer ${authStore.token}` },
})
.then((response) => {
return response.data;
})
.catch(() => {
throw new Error("Could not fetch fridge item");
});
},
* Deletes account from the
* @param id
* @param token
* @returns {Promise<*>}
deleteAccount: async (id) => {
const authStore = useAuthStore();
if (!authStore.isLoggedIn) {
throw new Error();
}
return axios.delete(`${import.meta.env.VITE_BACKEND_URL}/account/${id}`, {
headers: { Authorization: `Bearer ${authStore.token}` },
.then(() => {
authStore.logout()
router.push('/login')
.catch((error) => {
throw new Error(error);
*
* @param id account id
* @param request password and firstname
* @returns {Promise<*>}
updateAccount: async (id, request) => {
const authStore = useAuthStore();
if (!authStore.isLoggedIn) {
throw new Error();
}
return axios.put(`${import.meta.env.VITE_BACKEND_URL}/account/${id}`,request, {
headers: { Authorization: `Bearer ${authStore.token}` },
})
authStore.setAccount(response.data)
throw new Error("Error when updating account: ");
/**
* Updates the profile name, restriction and profile image
* @param id profile id
* @param request
* @returns {Promise<*>}
*/
updateProfile: async (id, request) => {
const authStore = useAuthStore();
if (!authStore.isLoggedIn) {
throw new Error();
}
return axios.put(`${import.meta.env.VITE_BACKEND_URL}/profile/${id}`,request, {
headers: { Authorization: `Bearer ${authStore.token}` },
authStore.setProfile(response.data)
.catch((error) => {
throw new Error("Error when updating profile: " + error);
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/**
* Deletes a profile from an account
* @param id
* @param request
* @returns {Promise<*>}
*/
deleteProfile: async (id) => {
const authStore = useAuthStore();
if (!authStore.isLoggedIn) {
throw new Error();
}
return axios.delete(`${import.meta.env.VITE_BACKEND_URL}/profile/${id}`, {
headers: { Authorization: `Bearer ${authStore.token}` },
})
.then(() => {
router.push('/selectProfile')
})
.catch(() => {
throw new Error("Kan ikke slette profil");
});
},