Skip to content

Commit

Permalink
🥅 Catch session storage error
Browse files Browse the repository at this point in the history
  • Loading branch information
williamchong committed Sep 10, 2024
1 parent efbd3ca commit 65f921a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
14 changes: 5 additions & 9 deletions src/pages/auth/redirect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<script>
import { mapActions, mapGetters } from 'vuex';
import { logTrackerEvent } from '~/util/EventLogger';
import { getFromSessionStorage, removeSessionStorageItem } from '~/util/misc';
export default {
name: 'AuthRedirect',
Expand All @@ -26,12 +27,9 @@ export default {
const { error, method, code } = this.$route.query;
let postAuthRoute = this.localeLocation(this.getHomeRoute);
if (window.sessionStorage) {
const storedRoute = window.sessionStorage.getItem('USER_POST_AUTH_ROUTE');
if (storedRoute) {
postAuthRoute = storedRoute;
window.sessionStorage.removeItem('USER_POST_AUTH_ROUTE');
}
const storedRoute = getFromSessionStorage('USER_POST_AUTH_ROUTE', true);
if (storedRoute) {
postAuthRoute = storedRoute;
}
if (method && code) {
Expand Down Expand Up @@ -81,9 +79,7 @@ export default {
}
} else {
logTrackerEvent(this, 'RedirectLogin', 'RedirectLoginFail', error, 1);
if (window.sessionStorage) {
window.sessionStorage.removeItem('USER_POST_AUTH_ROUTE');
}
removeSessionStorageItem('USER_POST_AUTH_ROUTE');
this.$nuxt.error({
statusCode: 400,
message: error,
Expand Down
4 changes: 2 additions & 2 deletions src/pages/oauth/redirect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ export default {
}
} else {
logTrackerEvent(this, 'Register', 'RegisterFail', error, 1);
if (window.sessionStorage) {
window.sessionStorage.removeItem('USER_POST_AUTH_ROUTE');
if (window.localStorage) {
window.localStorage.removeItem('USER_POST_AUTH_ROUTE');
}
this.$nuxt.error({
statusCode: 400,
Expand Down
30 changes: 22 additions & 8 deletions src/util/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,32 @@ export function getContentUrlType(url) {
return undefined;
}

export function getFromSessionStorage(key) {
if (window.sessionStorage) {
return window.sessionStorage.getItem(key);
}
export function getFromSessionStorage(key, remove = false) {
try {
if (window.sessionStorage) {
const item = window.sessionStorage.getItem(key);
if (remove) window.sessionStorage.removeItem(key);
return item;
}
} catch {}
return null;
}

export function setSessionStorageItem(key, item) {
if (window.sessionStorage) {
return window.sessionStorage.setItem(key, item);
}
return undefined;
try {
if (window.sessionStorage) {
window.sessionStorage.setItem(key, item);
}
} catch {}
}

export function removeSessionStorageItem(key) {
try {
if (window.sessionStorage) {
window.sessionStorage.removeItem(key);
}
} catch {}
return null;
}

export default sleep;

0 comments on commit 65f921a

Please sign in to comment.