Skip to content
Snippets Groups Projects
Commit 6c058628 authored by Jakob Grønhaug's avatar Jakob Grønhaug
Browse files

Merge branch 'opprett-profil' of...

Merge branch 'opprett-profil' of gitlab.stud.idi.ntnu.no:idatt2106-scrum-team-02/frontend into opprett-profil
parents 9bcdc0df 0b841795
No related branches found
No related tags found
1 merge request!15all funksjonalitet og filopplasting på plass
Pipeline #224183 passed with stages
in 1 minute and 56 seconds
...@@ -5,128 +5,152 @@ import router from "@/router/index"; ...@@ -5,128 +5,152 @@ import router from "@/router/index";
export const API = { export const API = {
/** /**
* API method to send a login request. * API method to send a login request.
* If login succeeds, the logged in User and their token * If login succeeds, the logged in User and their token
* is saved to the Pinia AuthStore * is saved to the Pinia AuthStore
* *
* @param email email address of the user to log in as * @param email email address of the user to log in as
* @param password password to log in with * @param password password to log in with
* @returns a Result with whether the login attempt succeeded * @returns a Result with whether the login attempt succeeded
*/ */
login: async (request) => { login: async (request) => {
const authStore = useAuthStore(); const authStore = useAuthStore();
let token; let token;
authStore.logout(); //in case someone for some reason is logged in 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;
return API.getAccount(id, token)
.then((user) => {
authStore.setAccount(user);
authStore.setToken(token);
API.getProfiles()
.then(response => {authStore.setProfiles(response)})
.catch(() => {throw new Error()})
return;
})
.catch(() => {
throw new Error();
});
})
.catch(() => {
throw new Error();
});
},
return axios.post(
`${import.meta.env.VITE_BACKEND_URL}/login`,
request,
)
.then(async (response) => {
token = response.data;
const id = (jwt_decode(token)).id;
/** return API.getAccount(id, token)
* API method to get a account by their ID .then((user) => {
* @param id ID number of the account to retrieve authStore.setAccount(user);
* @returns A promise that resolves to a User if the API call succeeds, authStore.setToken(token);
* or is rejected if the API call fails API.getProfiles()
*/ .then(response => { authStore.setProfiles(response) })
getAccount: async (id, token) => { .catch(() => { throw new Error() })
return axios.get(`${import.meta.env.VITE_BACKEND_URL}/account/${id}`, { return;
headers: { Authorization: `Bearer ${token}` },
})
.then((response) => {
return response.data;
}) })
.catch(() => { .catch(() => {
throw new Error("Account not found or not accessible"); throw new Error();
}); });
}, })
.catch(() => {
throw new Error();
});
},
// 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(() => { * API method to get a account by their ID
throw new Error("Profile not found or not accessible") * @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 * Upload profile image
* *
* @typedef {{name: string, profileImageUrl: string, isRestricted: boolean}} ProfileType * @param {Blob} image - the image file contents to upload. Must be a JPEG no bigger than 512kB
* @param {ProfileType} profile * @param {Number} profileId - the ID of the profile to upload this image to
* @returns * @returns {Promise<String>} A Promise that resolves to the URL of the uploaded image
*/ */
addProfile: async (profile) => { uploadProfileImage: async (image, profileId) => {
const authStore = useAuthStore(); const authStore = useAuthStore();
if (!authStore.isLoggedIn) {
throw new Error(); let fd = new FormData();
} fd.append("file", image);
fd.append("profileId", profileId);
return axios.post(import.meta.env.VITE_BACKEND_URL + '/profile', { return axios.post(`${import.meta.env.VITE_BACKEND_URL}/img`, fd, {
headers: { Authorization: "Bearer " + authStore.token }, headers: {
body: profile Authorization: `Bearer ${authStore.token}`,
}
})
.then((response) => {
return response.data;
})
.catch(() => {
throw new Error();
}) })
},
/**
* Sends a request to create a new profile on the currently logged in account
*
* @typedef {{name: string, id?: number, accountId?: number, profileImageUrl: string, isRestricted: boolean}} ProfileType
* @param {ProfileType} profile - the partial data of profile to create
* @returns {Promise<ProfileType>} the full profile after saving, with id and account ID set
*/
addProfile: async (profile) => {
const authStore = useAuthStore();
if (!authStore.isLoggedIn) {
throw new Error();
}
return axios.post(import.meta.env.VITE_BACKEND_URL + '/profile', profile, {
headers: { Authorization: "Bearer " + authStore.token },
})
.then((response) => { .then((response) => {
return response.data; return response.data;
}).catch(() => { }).catch(() => {
throw new Error(); throw new Error();
}) })
}, },
// Returns all profiles to the logged in user // Returns all profiles to the logged in user
getProfiles: async () => { getProfiles: async () => {
const authStore = useAuthStore(); const authStore = useAuthStore();
if (!authStore.isLoggedIn) { if (!authStore.isLoggedIn) {
throw new Error(); throw new Error();
} }
return axios.get(import.meta.env.VITE_BACKEND_URL + '/profile', { return axios.get(import.meta.env.VITE_BACKEND_URL + '/profile', {
headers: { Authorization: "Bearer " + authStore.token }, headers: { Authorization: "Bearer " + authStore.token },
},
)
.then(response => {
return response.data
}).catch(() => {
throw new Error();
});
}, },
)
.then(response => {
return response.data
}).catch(() => {
throw new Error();
});
},
// Registers a new account and logs into it // Registers a new account and logs into it
addAccount: async (request) => { addAccount: async (request) => {
const authStore = useAuthStore(); const authStore = useAuthStore();
axios.post(import.meta.env.VITE_BACKEND_URL + '/account', request) axios.post(import.meta.env.VITE_BACKEND_URL + '/account', request)
.then(() => { .then(() => {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment