Login changes

This commit is contained in:
jableader 2024-05-12 16:32:08 +10:00
parent 50acb423a3
commit a4a4497dc4
2 changed files with 14 additions and 5 deletions

View file

@ -28,7 +28,7 @@ export default {
}
},
async mounted() {
if (!await data.isLoggedIn()) {
if (!await data.currentUser()) {
this.$router.push('/login')
}
}

View file

@ -90,7 +90,11 @@ export default {
return await response.json();
},
async isLoggedIn() {
async currentUser() {
if (user) {
return user;
}
var cookie = decodeURIComponent(document.cookie).split(";").find(cookie => cookie.trimStart().startsWith("user_id="));
if (cookie) {
const response = await fetch(BASE_URL + "/auth/refresh", {
@ -98,10 +102,12 @@ export default {
credentials: "include",
});
if (response.ok) {
user = await response.json();
}
}
return user !== null;
return user;
},
async login(username) {
const response = await fetch(BASE_URL + "/auth/login", {
@ -113,7 +119,10 @@ export default {
body: JSON.stringify({ username }),
});
if (response.ok) {
user = await response.json();
}
return user;
},
async deleteRecipe(id) {