Login changes
This commit is contained in:
parent
50acb423a3
commit
a4a4497dc4
2 changed files with 14 additions and 5 deletions
|
|
@ -28,7 +28,7 @@ export default {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
async mounted() {
|
async mounted() {
|
||||||
if (!await data.isLoggedIn()) {
|
if (!await data.currentUser()) {
|
||||||
this.$router.push('/login')
|
this.$router.push('/login')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
17
src/data.js
17
src/data.js
|
|
@ -90,7 +90,11 @@ export default {
|
||||||
|
|
||||||
return await response.json();
|
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="));
|
var cookie = decodeURIComponent(document.cookie).split(";").find(cookie => cookie.trimStart().startsWith("user_id="));
|
||||||
if (cookie) {
|
if (cookie) {
|
||||||
const response = await fetch(BASE_URL + "/auth/refresh", {
|
const response = await fetch(BASE_URL + "/auth/refresh", {
|
||||||
|
|
@ -98,10 +102,12 @@ export default {
|
||||||
credentials: "include",
|
credentials: "include",
|
||||||
});
|
});
|
||||||
|
|
||||||
user = await response.json();
|
if (response.ok) {
|
||||||
|
user = await response.json();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return user !== null;
|
return user;
|
||||||
},
|
},
|
||||||
async login(username) {
|
async login(username) {
|
||||||
const response = await fetch(BASE_URL + "/auth/login", {
|
const response = await fetch(BASE_URL + "/auth/login", {
|
||||||
|
|
@ -113,7 +119,10 @@ export default {
|
||||||
body: JSON.stringify({ username }),
|
body: JSON.stringify({ username }),
|
||||||
});
|
});
|
||||||
|
|
||||||
user = await response.json();
|
if (response.ok) {
|
||||||
|
user = await response.json();
|
||||||
|
}
|
||||||
|
|
||||||
return user;
|
return user;
|
||||||
},
|
},
|
||||||
async deleteRecipe(id) {
|
async deleteRecipe(id) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue