Skip to content

Instantly share code, notes, and snippets.

@kirandash
Created February 3, 2021 07:02
Show Gist options
  • Save kirandash/d28ce3b0f105f723e66b4bc9a166d16d to your computer and use it in GitHub Desktop.
Save kirandash/d28ce3b0f105f723e66b4bc9a166d16d to your computer and use it in GitHub Desktop.
AWS Amplify Custom LocalStorage
import Amplify, { Auth } from "aws-amplify";
const MEMORY_KEY_PREFIX = "@kdAuth:";
let dataMemory = {};
/**
* This is used to set a specific item in storage
*/
function setItem(key, value) {
localStorage.setItem(MEMORY_KEY_PREFIX + key, value);
dataMemory[key] = value;
return dataMemory[key];
}
/**
* This is used to get a specific key from storage
*/
function getItem(key) {
return Object.prototype.hasOwnProperty.call(dataMemory, key)
? dataMemory[key]
: undefined;
}
/**
* This is used to remove an item from storage
*/
function removeItem(key) {
localStorage.removeItem(MEMORY_KEY_PREFIX + key);
return delete dataMemory[key];
}
/**
* This is used to clear the storage
*/
function clear() {
dataMemory = {};
return dataMemory;
}
const AuthStorage = {
setItem,
getItem,
removeItem,
clear,
};
Amplify.configure({
Auth: {
userPoolId: process.env.REACT_APP_USER_POOL_ID,
userPoolWebClientId: process.env.REACT_APP_CLIENT_ID,
cookieStorage: {
domain: process.env.REACT_APP_DOMAIN,
secure: true,
},
},
Storage: {
AWSS3: {
bucket: "", //REQUIRED - Amazon S3 bucket name
region: "XX-XXXX-X", //OPTIONAL - Amazon service region
},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment