Fix localStorage usage

This commit is contained in:
Andrew Bastin
2021-06-21 00:27:45 -04:00
parent 257e2db651
commit f4f29b8520
10 changed files with 81 additions and 38 deletions

View File

@@ -49,14 +49,14 @@ module.exports = {
name: "localStorage", name: "localStorage",
message: message:
"Do not use 'localStorage' directly. Please use localpersistence.ts functions or stores", "Do not use 'localStorage' directly. Please use localpersistence.ts functions or stores",
} },
], ],
// window.localStorage block // window.localStorage block
"no-restricted-syntax": [ "no-restricted-syntax": [
"error", "error",
{ {
selector: "CallExpression[callee.object.property.name='localStorage']", selector: "CallExpression[callee.object.property.name='localStorage']",
message: message:
"Do not use 'localStorage' directly. Please use localpersistence.ts functions or stores", "Do not use 'localStorage' directly. Please use localpersistence.ts functions or stores",
}, },
], ],

View File

@@ -167,6 +167,7 @@
<script> <script>
import intializePwa from "~/helpers/pwa" import intializePwa from "~/helpers/pwa"
import { currentUser$ } from "~/helpers/fb/auth" import { currentUser$ } from "~/helpers/fb/auth"
import { getLocalConfig, setLocalConfig } from "~/newstore/localpersistence"
// import { hasExtensionInstalled } from "~/helpers/strategies/ExtensionStrategy" // import { hasExtensionInstalled } from "~/helpers/strategies/ExtensionStrategy"
export default { export default {
@@ -192,7 +193,7 @@ export default {
// Initializes the PWA code - checks if the app is installed, // Initializes the PWA code - checks if the app is installed,
// etc. // etc.
this.showInstallPrompt = await intializePwa() this.showInstallPrompt = await intializePwa()
const cookiesAllowed = localStorage.getItem("cookiesAllowed") === "yes" const cookiesAllowed = getLocalConfig("cookiesAllowed") === "yes"
if (!cookiesAllowed) { if (!cookiesAllowed) {
this.$toast.show(this.$t("we_use_cookies"), { this.$toast.show(this.$t("we_use_cookies"), {
icon: "info", icon: "info",
@@ -202,7 +203,7 @@ export default {
{ {
text: this.$t("dismiss"), text: this.$t("dismiss"),
onClick: (_, toastObject) => { onClick: (_, toastObject) => {
localStorage.setItem("cookiesAllowed", "yes") setLocalConfig("cookiesAllowed", "yes")
toastObject.goAway(0) toastObject.goAway(0)
}, },
}, },

View File

@@ -70,6 +70,7 @@
<script> <script>
import { currentUser$, signInWithEmail } from "~/helpers/fb/auth" import { currentUser$, signInWithEmail } from "~/helpers/fb/auth"
import { setLocalConfig } from "~/newstore/localpersistence"
export default { export default {
props: { props: {
@@ -101,7 +102,7 @@ export default {
await signInWithEmail(this.form.email, actionCodeSettings) await signInWithEmail(this.form.email, actionCodeSettings)
.then(() => { .then(() => {
this.mode = "email" this.mode = "email"
window.localStorage.setItem("emailForSignIn", this.form.email) setLocalConfig("emailForSignIn", this.form.email)
}) })
.catch((error) => { .catch((error) => {
this.$toast.error(error.message, { this.$toast.error(error.message, {

View File

@@ -39,10 +39,12 @@
</template> </template>
<script> <script>
import { getLocalConfig, setLocalConfig } from "~/newstore/localpersistence"
export default { export default {
data() { data() {
return { return {
active: localStorage.getItem("THEME_COLOR") || "green", active: getLocalConfig("THEME_COLOR") || "green",
accentColors: [ accentColors: [
"blue", "blue",
"green", "green",
@@ -58,7 +60,7 @@ export default {
}, },
watch: { watch: {
active(color) { active(color) {
localStorage.setItem("THEME_COLOR", color) setLocalConfig("THEME_COLOR", color)
}, },
}, },

View File

@@ -1,3 +1,9 @@
import {
getLocalConfig,
setLocalConfig,
removeLocalConfig,
} from "~/newstore/localpersistence"
const redirectUri = `${window.location.origin}/` const redirectUri = `${window.location.origin}/`
// GENERAL HELPER FUNCTIONS // GENERAL HELPER FUNCTIONS
@@ -155,16 +161,16 @@ const tokenRequest = async ({
} }
// Store oauth information // Store oauth information
localStorage.setItem("tokenEndpoint", accessTokenUrl) setLocalConfig("tokenEndpoint", accessTokenUrl)
localStorage.setItem("client_id", clientId) setLocalConfig("client_id", clientId)
// Create and store a random state value // Create and store a random state value
const state = generateRandomString() const state = generateRandomString()
localStorage.setItem("pkce_state", state) setLocalConfig("pkce_state", state)
// Create and store a new PKCE codeVerifier (the plaintext random secret) // Create and store a new PKCE codeVerifier (the plaintext random secret)
const codeVerifier = generateRandomString() const codeVerifier = generateRandomString()
localStorage.setItem("pkce_codeVerifier", codeVerifier) setLocalConfig("pkce_codeVerifier", codeVerifier)
// Hash and base64-urlencode the secret to use as the challenge // Hash and base64-urlencode the secret to use as the challenge
const codeChallenge = await pkceChallengeFromVerifier(codeVerifier) const codeChallenge = await pkceChallengeFromVerifier(codeVerifier)
@@ -194,7 +200,7 @@ const tokenRequest = async ({
* @returns {Object} * @returns {Object}
*/ */
const oauthRedirect = async () => { const oauthRedirect = () => {
let tokenResponse = "" let tokenResponse = ""
const q = parseQueryString(window.location.search.substring(1)) const q = parseQueryString(window.location.search.substring(1))
// Check if the server returned an error string // Check if the server returned an error string
@@ -204,30 +210,27 @@ const oauthRedirect = async () => {
// If the server returned an authorization code, attempt to exchange it for an access token // If the server returned an authorization code, attempt to exchange it for an access token
if (q.code) { if (q.code) {
// Verify state matches what we set at the beginning // Verify state matches what we set at the beginning
if (localStorage.getItem("pkce_state") !== q.state) { if (getLocalConfig("pkce_state") !== q.state) {
alert("Invalid state") alert("Invalid state")
} else { } else {
try { try {
// Exchange the authorization code for an access token // Exchange the authorization code for an access token
tokenResponse = await sendPostRequest( tokenResponse = sendPostRequest(getLocalConfig("tokenEndpoint"), {
localStorage.getItem("tokenEndpoint"), grant_type: "authorization_code",
{ code: q.code,
grant_type: "authorization_code", client_id: getLocalConfig("client_id"),
code: q.code, redirect_uri: redirectUri,
client_id: localStorage.getItem("client_id"), codeVerifier: getLocalConfig("pkce_codeVerifier"),
redirect_uri: redirectUri, })
codeVerifier: localStorage.getItem("pkce_codeVerifier"),
}
)
} catch (err) { } catch (err) {
console.log(`${error.error}\n\n${error.error_description}`) console.log(`${error.error}\n\n${error.error_description}`)
} }
} }
// Clean these up since we don't need them anymore // Clean these up since we don't need them anymore
localStorage.removeItem("pkce_state") removeLocalConfig("pkce_state")
localStorage.removeItem("pkce_codeVerifier") removeLocalConfig("pkce_codeVerifier")
localStorage.removeItem("tokenEndpoint") removeLocalConfig("tokenEndpoint")
localStorage.removeItem("client_id") removeLocalConfig("client_id")
return tokenResponse return tokenResponse
} }
return tokenResponse return tokenResponse

View File

@@ -1,21 +1,23 @@
import { getLocalConfig, setLocalConfig } from "~/newstore/localpersistence"
export default () => { export default () => {
//* ** Determine whether or not the PWA has been installed. ***// //* ** Determine whether or not the PWA has been installed. ***//
// Step 1: Check local storage // Step 1: Check local storage
let pwaInstalled = localStorage.getItem("pwaInstalled") === "yes" let pwaInstalled = getLocalConfig("pwaInstalled") === "yes"
// Step 2: Check if the display-mode is standalone. (Only permitted for PWAs.) // Step 2: Check if the display-mode is standalone. (Only permitted for PWAs.)
if ( if (
!pwaInstalled && !pwaInstalled &&
window.matchMedia("(display-mode: standalone)").matches window.matchMedia("(display-mode: standalone)").matches
) { ) {
localStorage.setItem("pwaInstalled", "yes") setLocalConfig("pwaInstalled", "yes")
pwaInstalled = true pwaInstalled = true
} }
// Step 3: Check if the navigator is in standalone mode. (Again, only permitted for PWAs.) // Step 3: Check if the navigator is in standalone mode. (Again, only permitted for PWAs.)
if (!pwaInstalled && window.navigator.standalone === true) { if (!pwaInstalled && window.navigator.standalone === true) {
localStorage.setItem("pwaInstalled", "yes") setLocalConfig("pwaInstalled", "yes")
pwaInstalled = true pwaInstalled = true
} }
@@ -32,7 +34,7 @@ export default () => {
// When the app is installed, remove install prompts. // When the app is installed, remove install prompts.
window.addEventListener("appinstalled", () => { window.addEventListener("appinstalled", () => {
localStorage.setItem("pwaInstalled", "yes") setLocalConfig("pwaInstalled", "yes")
pwaInstalled = true pwaInstalled = true
document.getElementById("installPWA").style.display = "none" document.getElementById("installPWA").style.display = "none"
}) })

View File

@@ -14,7 +14,10 @@
</template> </template>
<script> <script>
import { setupLocalPersistence } from "~/newstore/localpersistence" import {
setupLocalPersistence,
getLocalConfig,
} from "~/newstore/localpersistence"
import { performMigrations } from "~/helpers/migrations" import { performMigrations } from "~/helpers/migrations"
import { initUserInfo } from "~/helpers/teams/BackendUserInfo" import { initUserInfo } from "~/helpers/teams/BackendUserInfo"
import { registerApolloAuthUpdate } from "~/helpers/apollo" import { registerApolloAuthUpdate } from "~/helpers/apollo"
@@ -24,7 +27,7 @@ export default {
beforeMount() { beforeMount() {
registerApolloAuthUpdate() registerApolloAuthUpdate()
const color = localStorage.getItem("THEME_COLOR") || "green" const color = getLocalConfig("THEME_COLOR") || "green"
document.documentElement.setAttribute("data-accent", color) document.documentElement.setAttribute("data-accent", color)
}, },
async mounted() { async mounted() {

View File

@@ -39,9 +39,6 @@ export default {
}, },
} }
}, },
beforeMount() {
initializeFirebase()
},
computed: { computed: {
statusCode() { statusCode() {
return (this.error && this.error.statusCode) || 500 return (this.error && this.error.statusCode) || 500
@@ -50,6 +47,9 @@ export default {
return this.error.message || this.$t("something_went_wrong") return this.error.message || this.$t("something_went_wrong")
}, },
}, },
beforeMount() {
initializeFirebase()
},
methods: { methods: {
reloadApplication() { reloadApplication() {

View File

@@ -1,3 +1,5 @@
/* eslint-disable no-restricted-globals, no-restricted-syntax */
import clone from "lodash/clone" import clone from "lodash/clone"
import assign from "lodash/assign" import assign from "lodash/assign"
import eq from "lodash/eq" import eq from "lodash/eq"
@@ -131,3 +133,31 @@ export function setupLocalPersistence() {
setupCollectionsPersistence() setupCollectionsPersistence()
setupEnvironmentsPersistence() setupEnvironmentsPersistence()
} }
/**
* Gets a value in LocalStorage.
*
* NOTE: Use LocalStorage to only store non-reactive simple data
* For more complex data, use stores and connect it to localpersistence
*/
export function getLocalConfig(name: string) {
return window.localStorage.getItem(name)
}
/**
* Sets a value in LocalStorage.
*
* NOTE: Use LocalStorage to only store non-reactive simple data
* For more complex data, use stores and connect it to localpersistence
*/
export function setLocalConfig(key: string, value: string) {
window.localStorage.setItem(key, value)
}
/**
* Clear config value in LocalStorage.
* @param key Key to be cleared
*/
export function removeLocalConfig(key: string) {
window.localStorage.removeItem(key)
}

View File

@@ -10,6 +10,7 @@
import Vue from "vue" import Vue from "vue"
import { initializeFirebase } from "~/helpers/fb" import { initializeFirebase } from "~/helpers/fb"
import { isSignInWithEmailLink, signInWithEmailLink } from "~/helpers/fb/auth" import { isSignInWithEmailLink, signInWithEmailLink } from "~/helpers/fb/auth"
import { getLocalConfig, removeLocalConfig } from "~/newstore/localpersistence"
export default Vue.extend({ export default Vue.extend({
data() { data() {
@@ -25,7 +26,7 @@ export default Vue.extend({
if (isSignInWithEmailLink(window.location.href)) { if (isSignInWithEmailLink(window.location.href)) {
this.signingInWithEmail = true this.signingInWithEmail = true
let email = window.localStorage.getItem("emailForSignIn") let email = getLocalConfig("emailForSignIn")
if (!email) { if (!email) {
email = window.prompt( email = window.prompt(
@@ -35,7 +36,7 @@ export default Vue.extend({
await signInWithEmailLink(email, window.location.href) await signInWithEmailLink(email, window.location.href)
.then(() => { .then(() => {
window.localStorage.removeItem("emailForSignIn") removeLocalConfig("emailForSignIn")
this.$router.push({ path: "/" }) this.$router.push({ path: "/" })
}) })
.catch((error) => { .catch((error) => {