Skip to content

Commit 9df76e6

Browse files
authored
Merge pull request #2625 from semaphoreui/httponly
feat(auth): httponly
2 parents 5e88746 + 07fcaba commit 9df76e6

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

web/src/App.vue

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -894,10 +894,6 @@ export default {
894894
return this.projects.find((x) => x.id === this.projectId);
895895
},
896896
897-
isAuthenticated() {
898-
return document.cookie.includes('semaphore=');
899-
},
900-
901897
templatesUrl() {
902898
let viewId = localStorage.getItem(`project${this.projectId}__lastVisitedViewId`);
903899
if (viewId) {
@@ -911,14 +907,6 @@ export default {
911907
},
912908
913909
async created() {
914-
if (!this.isAuthenticated) {
915-
if (this.$route.path !== '/auth/login') {
916-
await this.$router.push({ path: '/auth/login' });
917-
}
918-
this.state = 'success';
919-
return;
920-
}
921-
922910
if (localStorage.getItem('darkMode') === '1') {
923911
this.darkMode = true;
924912
}
@@ -927,6 +915,14 @@ export default {
927915
await this.loadData();
928916
this.state = 'success';
929917
} catch (err) {
918+
if (err.response && err.response.status === 401) {
919+
if (this.$route.path !== '/auth/login') {
920+
await this.$router.push({ path: '/auth/login' });
921+
}
922+
this.state = 'success';
923+
return;
924+
}
925+
930926
EventBus.$emit('i-snackbar', {
931927
color: 'error',
932928
text: getErrorMessage(err),
@@ -1063,6 +1059,11 @@ export default {
10631059
},
10641060
10651061
methods: {
1062+
1063+
async isAuthenticated() {
1064+
return document.cookie.includes('semaphore=');
1065+
},
1066+
10661067
async onSubscriptionKeyUpdates() {
10671068
EventBus.$emit('i-snackbar', {
10681069
color: 'success',
@@ -1158,10 +1159,6 @@ export default {
11581159
},
11591160
11601161
async loadUserInfo() {
1161-
if (!this.isAuthenticated) {
1162-
return;
1163-
}
1164-
11651162
this.user = (await axios({
11661163
method: 'get',
11671164
url: '/api/user',

web/src/views/Auth.vue

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,9 @@ export default {
183183
},
184184
185185
async created() {
186-
if (this.isAuthenticated()) {
186+
if (await this.isAuthenticated()) {
187187
document.location = document.baseURI;
188+
return;
188189
}
189190
await axios({
190191
method: 'get',
@@ -207,8 +208,21 @@ export default {
207208
return pwd;
208209
},
209210
210-
isAuthenticated() {
211-
return document.cookie.includes('semaphore=');
211+
async isAuthenticated() {
212+
try {
213+
await axios({
214+
method: 'get',
215+
url: '/api/user',
216+
responseType: 'json',
217+
});
218+
} catch (err) {
219+
if (err.response.status === 401) {
220+
return false;
221+
}
222+
throw err;
223+
}
224+
225+
return true;
212226
},
213227
214228
async signIn() {

0 commit comments

Comments
 (0)