Determine if localStorage or AsyncStorage

This commit is contained in:
Kyle Corbelli 2017-09-27 14:57:02 -07:00
parent 63b5077d7a
commit 1bc7520928
10 changed files with 125 additions and 68 deletions

78
dist/AsyncLocalStorage.js vendored Normal file
View File

@ -0,0 +1,78 @@
"use strict";
// import { AsyncStorage } from 'react-native'
Object.defineProperty(exports, "__esModule", { value: true });
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Rip this out into its own npm package:
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var AsyncLocalStorage = {
getItem: function (key) { return new Promise(function (resolve, reject) {
try {
var value = window.localStorage.getItem(key);
resolve(value);
}
catch (error) {
reject(error);
}
}); },
setItem: function (key, value) { return new Promise(function (resolve, reject) {
try {
window.localStorage.setItem(key, value);
resolve(value);
}
catch (error) {
reject(error);
}
}); },
removeItem: function (key) { return new Promise(function (resolve, reject) {
try {
var value = window.localStorage.getItem(key);
window.localStorage.removeItem(key);
resolve(value);
}
catch (error) {
reject(error);
}
}); },
clear: function () { return new Promise(function (resolve, reject) {
try {
window.localStorage.clear();
resolve(true);
}
catch (error) {
reject(error);
}
}); },
getAllKeys: function () { return new Promise(function (resolve, reject) {
try {
var allKeys = Object.keys(window.localStorage);
resolve(allKeys);
}
catch (error) {
reject(error);
}
}); },
multiGet: function (keys) { return new Promise(function (resolve, reject) {
try {
var values = keys.map(function (key) { return [key, window.localStorage.getItem(key)]; });
resolve(values);
}
catch (error) {
reject(error);
}
}); },
multiSet: function (keyValuePairs) { return new Promise(function (resolve, reject) {
try {
keyValuePairs.forEach(function (keyValuePair) { return window.localStorage.setItem(keyValuePair[0], keyValuePair[1]); });
var newKeyValuePairs = keyValuePairs.map(function (keyValuePair) { return [keyValuePair[0], window.localStorage.getItem(keyValuePair[0])]; });
resolve(newKeyValuePairs);
}
catch (error) {
reject(error);
}
}); },
};
exports.default = AsyncLocalStorage;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ^ Rip this out into its own npm package
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//# sourceMappingURL=AsyncLocalStorage.js.map

1
dist/AsyncLocalStorage.js.map vendored Normal file
View File

@ -0,0 +1 @@
{"version":3,"file":"AsyncLocalStorage.js","sourceRoot":"","sources":["../src/AsyncLocalStorage.ts"],"names":[],"mappings":";AAAA,8CAA8C;;AAE9C,sIAAsI;AACtI,yCAAyC;AACzC,sIAAsI;AAEtI,IAAM,iBAAiB,GAAG;IACxB,OAAO,EAAE,UAAC,GAAW,IAAmB,OAAA,IAAI,OAAO,CAAC,UAAC,OAAO,EAAE,MAAM;QAClE,IAAI,CAAC;YACH,IAAM,KAAK,GAAkB,MAAM,CAAC,YAAY,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;YAC7D,OAAO,CAAC,KAAK,CAAC,CAAA;QAChB,CAAC;QAAC,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;YACf,MAAM,CAAC,KAAK,CAAC,CAAA;QACf,CAAC;IACH,CAAC,CAAC,EAPsC,CAOtC;IAEF,OAAO,EAAE,UAAC,GAAW,EAAE,KAAa,IAAK,OAAA,IAAI,OAAO,CAAC,UAAC,OAAO,EAAE,MAAM;QACnE,IAAI,CAAC;YACH,MAAM,CAAC,YAAY,CAAC,OAAO,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA;YACvC,OAAO,CAAC,KAAK,CAAC,CAAA;QAChB,CAAC;QAAC,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;YACf,MAAM,CAAC,KAAK,CAAC,CAAA;QACf,CAAC;IACH,CAAC,CAAC,EAPuC,CAOvC;IAEF,UAAU,EAAE,UAAC,GAAW,IAAmB,OAAA,IAAI,OAAO,CAAC,UAAC,OAAO,EAAE,MAAM;QACrE,IAAI,CAAC;YACH,IAAM,KAAK,GAAkB,MAAM,CAAC,YAAY,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;YAC7D,MAAM,CAAC,YAAY,CAAC,UAAU,CAAC,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CAAC,CAAA;QACf,CAAC;IACH,CAAC,CAAC,EAP4B,CAO5B;IAEF,QAAQ,EAAE,UAAC,aAAyB,IAAK,OAAA,IAAI,OAAO,CAAC,UAAC,OAAO,EAAE,MAAM;QACnE,IAAI,CAAC;YACH,aAAa,CAAC,OAAO,CAAC,UAAA,YAAY,IAAI,OAAA,MAAM,CAAC,YAAY,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC,EAAE,YAAY,CAAC,CAAC,CAAC,CAAC,EAA7D,CAA6D,CAAC,CAAA;YACpG,IAAM,gBAAgB,GAAG,aAAa,CAAC,GAAG,CAAC,UAAA,YAAY,IAAI,OAAA,CAAE,YAAY,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,YAAY,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAE,EAAjE,CAAiE,CAAC,CAAA;YAC7H,OAAO,CAAC,gBAAgB,CAAC,CAAA;QAC3B,CAAC;QAAC,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;YACf,MAAM,CAAC,KAAK,CAAC,CAAA;QACf,CAAC;IACH,CAAC,CAAC,EARuC,CAQvC;CACH,CAAA;AAED,kBAAe,iBAAiB,CAAA;AAEhC,sIAAsI;AACtI,0CAA0C;AAC1C,sIAAsI"}

39
dist/actions.js vendored
View File

@ -38,7 +38,7 @@ var _this = this;
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
var axios_1 = require("axios"); var axios_1 = require("axios");
var types_1 = require("./types"); var types_1 = require("./types");
var Storage_1 = require("./Storage"); var AsyncLocalStorage_1 = require("./AsyncLocalStorage");
var auth_1 = require("./services/auth"); // <- maybe this is where you pass in the platform paramter, specifying if it is for a browser or for React Native var auth_1 = require("./services/auth"); // <- maybe this is where you pass in the platform paramter, specifying if it is for a browser or for React Native
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Pure Redux actions: // Pure Redux actions:
@ -91,20 +91,9 @@ exports.signOutRequestFailed = function () { return ({
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Async Redux Thunk actions: // Async Redux Thunk actions:
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// what is the second argument here? it needs to contain configs for (1) userRegistrationDetails, (2) userAttributes, (3) maybe even the authUrl... just make it a simple one-argument function
// we'll also want the userAttributes to pertain to the end-user's initial state and heaven forbid reducers
// actually, userSignInCredentials, userSignOutCredentials, and verificationParams are always the same as per devise token auth
// const config = {
// authUrl: 'http://url.com',
// userAttributes: {
// firstName: 'name' // <- key is how the frontend knows it, value is how the backend knows it
// },
// userRegistrationAttributes: { <- this is for keys/vals IN ADDITION TO email, password and passwordConfirmation
// firstName: 'name'
// },
// }
var generateAuthActions = function (config) { var generateAuthActions = function (config) {
var authUrl = config.authUrl, userAttributes = config.userAttributes, userRegistrationAttributes = config.userRegistrationAttributes; var authUrl = config.authUrl, storage = config.storage, userAttributes = config.userAttributes, userRegistrationAttributes = config.userRegistrationAttributes;
var Storage = Boolean(storage.flushGetRequests) ? storage : AsyncLocalStorage_1.default;
var registerUser = function (userRegistrationDetails) { return function (dispatch) { var registerUser = function (userRegistrationDetails) { return function (dispatch) {
return __awaiter(this, void 0, void 0, function () { return __awaiter(this, void 0, void 0, function () {
var email, password, passwordConfirmation, data, response, userAttributesToSave, error_1; var email, password, passwordConfirmation, data, response, userAttributesToSave, error_1;
@ -134,7 +123,7 @@ var generateAuthActions = function (config) {
response = _a.sent(); response = _a.sent();
auth_1.setAuthHeaders(response.headers); auth_1.setAuthHeaders(response.headers);
// Have to check what type of platform it is, depending on the key provided by the end-user... like "browser", "iphone", or "android", etc.: // Have to check what type of platform it is, depending on the key provided by the end-user... like "browser", "iphone", or "android", etc.:
auth_1.persistAuthHeadersInLocalStorage(response.headers); auth_1.persistAuthHeadersInDeviceStorage(Storage, response.headers);
userAttributesToSave = auth_1.getUserAttributesFromResponse(userAttributes, response); userAttributesToSave = auth_1.getUserAttributesFromResponse(userAttributes, response);
dispatch(exports.registrationRequestSucceeded(userAttributesToSave)); // <- need to make this reducer more flexible dispatch(exports.registrationRequestSucceeded(userAttributesToSave)); // <- need to make this reducer more flexible
return [3 /*break*/, 4]; return [3 /*break*/, 4];
@ -166,7 +155,7 @@ var generateAuthActions = function (config) {
response = _a.sent(); response = _a.sent();
auth_1.setAuthHeaders(response.headers); auth_1.setAuthHeaders(response.headers);
// Have to check what type of platform it is, depending on the key provided by the end-user... like "browser", "iphone", or "android", etc.: // Have to check what type of platform it is, depending on the key provided by the end-user... like "browser", "iphone", or "android", etc.:
auth_1.persistAuthHeadersInLocalStorage(response.headers); auth_1.persistAuthHeadersInDeviceStorage(Storage, response.headers);
userAttributesToSave = auth_1.getUserAttributesFromResponse(userAttributes, response); userAttributesToSave = auth_1.getUserAttributesFromResponse(userAttributes, response);
dispatch(exports.verifyTokenRequestSucceeded(userAttributesToSave)); dispatch(exports.verifyTokenRequestSucceeded(userAttributesToSave));
return [3 /*break*/, 4]; return [3 /*break*/, 4];
@ -202,7 +191,7 @@ var generateAuthActions = function (config) {
response = _a.sent(); response = _a.sent();
auth_1.setAuthHeaders(response.headers); auth_1.setAuthHeaders(response.headers);
// Have to check what type of platform it is, depending on the key provided by the end-user... like "browser", "iphone", or "android", etc.: // Have to check what type of platform it is, depending on the key provided by the end-user... like "browser", "iphone", or "android", etc.:
auth_1.persistAuthHeadersInLocalStorage(response.headers); auth_1.persistAuthHeadersInDeviceStorage(Storage, response.headers);
userAttributesToSave = auth_1.getUserAttributesFromResponse(userAttributes, response); userAttributesToSave = auth_1.getUserAttributesFromResponse(userAttributes, response);
dispatch(exports.signInRequestSucceeded(userAttributesToSave)); dispatch(exports.signInRequestSucceeded(userAttributesToSave));
return [3 /*break*/, 4]; return [3 /*break*/, 4];
@ -223,13 +212,13 @@ var generateAuthActions = function (config) {
case 0: case 0:
_a = {}; _a = {};
_b = 'access-token'; _b = 'access-token';
return [4 /*yield*/, Storage_1.default.getItem('access-token')]; return [4 /*yield*/, Storage.getItem('access-token')];
case 1: case 1:
_a[_b] = (_c.sent()); _a[_b] = (_c.sent());
return [4 /*yield*/, Storage_1.default.getItem('client')]; return [4 /*yield*/, Storage.getItem('client')];
case 2: case 2:
_a.client = (_c.sent()); _a.client = (_c.sent());
return [4 /*yield*/, Storage_1.default.getItem('uid')]; return [4 /*yield*/, Storage.getItem('uid')];
case 3: case 3:
userSignOutCredentials = (_a.uid = (_c.sent()), userSignOutCredentials = (_a.uid = (_c.sent()),
_a); _a);
@ -246,7 +235,7 @@ var generateAuthActions = function (config) {
_c.sent(); _c.sent();
auth_1.deleteAuthHeaders(); auth_1.deleteAuthHeaders();
// Have to check what type of platform it is, depending on the key provided by the end-user... like "browser", "iphone", or "android", etc.: // Have to check what type of platform it is, depending on the key provided by the end-user... like "browser", "iphone", or "android", etc.:
auth_1.deleteAuthHeadersFromLocalStorage(); auth_1.deleteAuthHeadersFromLocalStorage(Storage);
dispatch(exports.signOutRequestSucceeded()); dispatch(exports.signOutRequestSucceeded());
return [3 /*break*/, 7]; return [3 /*break*/, 7];
case 6: case 6:
@ -262,18 +251,18 @@ var generateAuthActions = function (config) {
var verificationParams, _a, _b; var verificationParams, _a, _b;
return __generator(this, function (_c) { return __generator(this, function (_c) {
switch (_c.label) { switch (_c.label) {
case 0: return [4 /*yield*/, Storage_1.default.getItem('access-token')]; case 0: return [4 /*yield*/, Storage.getItem('access-token')];
case 1: case 1:
if (!_c.sent()) return [3 /*break*/, 5]; if (!_c.sent()) return [3 /*break*/, 5];
_a = {}; _a = {};
_b = 'access-token'; _b = 'access-token';
return [4 /*yield*/, Storage_1.default.getItem('access-token')]; return [4 /*yield*/, Storage.getItem('access-token')];
case 2: case 2:
_a[_b] = (_c.sent()); _a[_b] = (_c.sent());
return [4 /*yield*/, Storage_1.default.getItem('client')]; return [4 /*yield*/, Storage.getItem('client')];
case 3: case 3:
_a.client = (_c.sent()); _a.client = (_c.sent());
return [4 /*yield*/, Storage_1.default.getItem('uid')]; return [4 /*yield*/, Storage.getItem('uid')];
case 4: case 4:
verificationParams = (_a.uid = (_c.sent()), verificationParams = (_a.uid = (_c.sent()),
_a); _a);

2
dist/actions.js.map vendored

File diff suppressed because one or more lines are too long

14
dist/services/auth.js vendored
View File

@ -38,12 +38,6 @@ var _this = this;
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
var axios_1 = require("axios"); var axios_1 = require("axios");
var utility_1 = require("./utility"); var utility_1 = require("./utility");
var Storage_1 = require("../Storage");
// try {
// Storage = AsyncStorage
// } catch (e) {
// Storage = AsyncLocalStorage
// }
var authHeaderKeys = [ var authHeaderKeys = [
'access-token', 'access-token',
'token-type', 'token-type',
@ -57,10 +51,10 @@ exports.setAuthHeaders = function (headers) {
}); });
}; };
// Will have to take a parameter from the package user to determine if this is for a browser or for React Native: // Will have to take a parameter from the package user to determine if this is for a browser or for React Native:
exports.persistAuthHeadersInLocalStorage = function (headers) { exports.persistAuthHeadersInDeviceStorage = function (Storage, headers) {
// use multiSet: // use multiSet:
authHeaderKeys.forEach(function (key) { authHeaderKeys.forEach(function (key) {
Storage_1.default.setItem(key, headers[key]); Storage.setItem(key, headers[key]);
}); });
}; };
exports.deleteAuthHeaders = function () { exports.deleteAuthHeaders = function () {
@ -69,11 +63,11 @@ exports.deleteAuthHeaders = function () {
}); });
}; };
// Will have to take a parameter from the package user to determine if this is for a browser or for React Native: // Will have to take a parameter from the package user to determine if this is for a browser or for React Native:
exports.deleteAuthHeadersFromLocalStorage = function () { return __awaiter(_this, void 0, void 0, function () { exports.deleteAuthHeadersFromLocalStorage = function (Storage) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) { return __generator(this, function (_a) {
// can use multiRemove once you've written it: // can use multiRemove once you've written it:
authHeaderKeys.forEach(function (key) { authHeaderKeys.forEach(function (key) {
Storage_1.default.removeItem(key); Storage.removeItem(key);
// localStorage.removeItem(key) // localStorage.removeItem(key)
}); });
return [2 /*return*/]; return [2 /*return*/];

View File

@ -1 +1 @@
{"version":3,"file":"auth.js","sourceRoot":"","sources":["../../src/services/auth.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iBAkEA;;AAlEA,+BAAyB;AACzB,qCAAkD;AAMlD,sCAAgC;AAEhC,QAAQ;AACR,2BAA2B;AAC3B,gBAAgB;AAChB,gCAAgC;AAChC,IAAI;AAEJ,IAAM,cAAc,GAAkB;IACpC,cAAc;IACd,YAAY;IACZ,QAAQ;IACR,QAAQ;IACR,KAAK;CACN,CAAA;AAEY,QAAA,cAAc,GAAG,UAAC,OAAoB;IACjD,cAAc,CAAC,OAAO,CAAC,UAAC,GAAW;QACjC,eAAK,CAAC,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,OAAO,CAAC,GAAG,CAAC,CAAA;IACnD,CAAC,CAAC,CAAA;AACJ,CAAC,CAAA;AAED,iHAAiH;AACpG,QAAA,gCAAgC,GAAG,UAAC,OAAoB;IACnE,gBAAgB;IAChB,cAAc,CAAC,OAAO,CAAC,UAAC,GAAW;QACjC,iBAAO,CAAC,OAAO,CAAC,GAAG,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC,CAAA;IACpC,CAAC,CAAC,CAAA;AACJ,CAAC,CAAA;AAEY,QAAA,iBAAiB,GAAG;IAC/B,cAAc,CAAC,OAAO,CAAC,UAAC,GAAW;QACjC,OAAO,eAAK,CAAC,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;IAC3C,CAAC,CAAC,CAAA;AACJ,CAAC,CAAA;AAED,iHAAiH;AACpG,QAAA,iCAAiC,GAAG;;QAC/C,8CAA8C;QAC9C,cAAc,CAAC,OAAO,CAAC,UAAC,GAAW;YACjC,iBAAO,CAAC,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-1,4 +1,4 @@
import { AsyncStorage } from 'react-native' // import { AsyncStorage } from 'react-native'
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Rip this out into its own npm package: // Rip this out into its own npm package:
@ -71,9 +71,9 @@ const AsyncLocalStorage = {
}), }),
} }
export default AsyncLocalStorage
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ^ Rip this out into its own npm package // ^ Rip this out into its own npm package
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const Storage = window.localStorage ? AsyncLocalStorage : AsyncStorage
export default Storage

View File

@ -5,6 +5,7 @@ import {
} from 'redux' } from 'redux'
import { import {
AuthResponse, AuthResponse,
DeviceStorage,
VerificationParams, VerificationParams,
UserAttributes, UserAttributes,
UserRegistrationDetails, UserRegistrationDetails,
@ -36,12 +37,12 @@ import {
SignOutRequestSucceededAction, SignOutRequestSucceededAction,
SignOutRequestFailedAction, SignOutRequestFailedAction,
} from './types' } from './types'
import Storage from './Storage' import AsyncLocalStorage from './AsyncLocalStorage'
import { import {
deleteAuthHeaders, deleteAuthHeaders,
deleteAuthHeadersFromLocalStorage, deleteAuthHeadersFromLocalStorage,
getUserAttributesFromResponse, getUserAttributesFromResponse,
persistAuthHeadersInLocalStorage, persistAuthHeadersInDeviceStorage,
setAuthHeaders, setAuthHeaders,
} from './services/auth' // <- maybe this is where you pass in the platform paramter, specifying if it is for a browser or for React Native } from './services/auth' // <- maybe this is where you pass in the platform paramter, specifying if it is for a browser or for React Native
@ -110,26 +111,16 @@ export const signOutRequestFailed = (): SignOutRequestFailedAction => ({
// Async Redux Thunk actions: // Async Redux Thunk actions:
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// what is the second argument here? it needs to contain configs for (1) userRegistrationDetails, (2) userAttributes, (3) maybe even the authUrl... just make it a simple one-argument function
// we'll also want the userAttributes to pertain to the end-user's initial state and heaven forbid reducers
// actually, userSignInCredentials, userSignOutCredentials, and verificationParams are always the same as per devise token auth
// const config = {
// authUrl: 'http://url.com',
// userAttributes: {
// firstName: 'name' // <- key is how the frontend knows it, value is how the backend knows it
// },
// userRegistrationAttributes: { <- this is for keys/vals IN ADDITION TO email, password and passwordConfirmation
// firstName: 'name'
// },
// }
const generateAuthActions = (config: { [key: string]: any }): ActionsExport => { const generateAuthActions = (config: { [key: string]: any }): ActionsExport => {
const { const {
authUrl, authUrl,
storage,
userAttributes, userAttributes,
userRegistrationAttributes, userRegistrationAttributes,
} = config } = config
const Storage: DeviceStorage = Boolean(storage.flushGetRequests) ? storage : AsyncLocalStorage
const registerUser = ( const registerUser = (
userRegistrationDetails: UserRegistrationDetails, userRegistrationDetails: UserRegistrationDetails,
) => async function (dispatch: Dispatch<{}>): Promise<void> { ) => async function (dispatch: Dispatch<{}>): Promise<void> {
@ -156,7 +147,7 @@ const generateAuthActions = (config: { [key: string]: any }): ActionsExport => {
}) })
setAuthHeaders(response.headers) setAuthHeaders(response.headers)
// Have to check what type of platform it is, depending on the key provided by the end-user... like "browser", "iphone", or "android", etc.: // Have to check what type of platform it is, depending on the key provided by the end-user... like "browser", "iphone", or "android", etc.:
persistAuthHeadersInLocalStorage(response.headers) persistAuthHeadersInDeviceStorage(Storage, response.headers)
const userAttributesToSave = getUserAttributesFromResponse(userAttributes, response) const userAttributesToSave = getUserAttributesFromResponse(userAttributes, response)
dispatch(registrationRequestSucceeded(userAttributesToSave)) // <- need to make this reducer more flexible dispatch(registrationRequestSucceeded(userAttributesToSave)) // <- need to make this reducer more flexible
} catch (error) { } catch (error) {
@ -177,7 +168,7 @@ const generateAuthActions = (config: { [key: string]: any }): ActionsExport => {
}) })
setAuthHeaders(response.headers) setAuthHeaders(response.headers)
// Have to check what type of platform it is, depending on the key provided by the end-user... like "browser", "iphone", or "android", etc.: // Have to check what type of platform it is, depending on the key provided by the end-user... like "browser", "iphone", or "android", etc.:
persistAuthHeadersInLocalStorage(response.headers) persistAuthHeadersInDeviceStorage(Storage, response.headers)
const userAttributesToSave = getUserAttributesFromResponse(userAttributes, response) const userAttributesToSave = getUserAttributesFromResponse(userAttributes, response)
dispatch(verifyTokenRequestSucceeded(userAttributesToSave)) dispatch(verifyTokenRequestSucceeded(userAttributesToSave))
} catch (error) { } catch (error) {
@ -204,7 +195,7 @@ const generateAuthActions = (config: { [key: string]: any }): ActionsExport => {
}) })
setAuthHeaders(response.headers) setAuthHeaders(response.headers)
// Have to check what type of platform it is, depending on the key provided by the end-user... like "browser", "iphone", or "android", etc.: // Have to check what type of platform it is, depending on the key provided by the end-user... like "browser", "iphone", or "android", etc.:
persistAuthHeadersInLocalStorage(response.headers) persistAuthHeadersInDeviceStorage(Storage, response.headers)
const userAttributesToSave = getUserAttributesFromResponse(userAttributes, response) const userAttributesToSave = getUserAttributesFromResponse(userAttributes, response)
dispatch(signInRequestSucceeded(userAttributesToSave)) dispatch(signInRequestSucceeded(userAttributesToSave))
} catch (error) { } catch (error) {
@ -228,7 +219,7 @@ const generateAuthActions = (config: { [key: string]: any }): ActionsExport => {
}) })
deleteAuthHeaders() deleteAuthHeaders()
// Have to check what type of platform it is, depending on the key provided by the end-user... like "browser", "iphone", or "android", etc.: // Have to check what type of platform it is, depending on the key provided by the end-user... like "browser", "iphone", or "android", etc.:
deleteAuthHeadersFromLocalStorage() deleteAuthHeadersFromLocalStorage(Storage)
dispatch(signOutRequestSucceeded()) dispatch(signOutRequestSucceeded())
} catch (error) { } catch (error) {
dispatch(signOutRequestFailed()) dispatch(signOutRequestFailed())

View File

@ -3,15 +3,9 @@ import { invertMapKeysAndValues } from './utility'
import { import {
AuthHeaders, AuthHeaders,
AuthResponse, AuthResponse,
DeviceStorage,
SingleLayerStringMap, SingleLayerStringMap,
} from '../types' } from '../types'
import Storage from '../Storage'
// try {
// Storage = AsyncStorage
// } catch (e) {
// Storage = AsyncLocalStorage
// }
const authHeaderKeys: Array<string> = [ const authHeaderKeys: Array<string> = [
'access-token', 'access-token',
@ -28,7 +22,7 @@ export const setAuthHeaders = (headers: AuthHeaders): void => {
} }
// Will have to take a parameter from the package user to determine if this is for a browser or for React Native: // Will have to take a parameter from the package user to determine if this is for a browser or for React Native:
export const persistAuthHeadersInLocalStorage = (headers: AuthHeaders): void => { export const persistAuthHeadersInDeviceStorage = (Storage: DeviceStorage, headers: AuthHeaders): void => {
// use multiSet: // use multiSet:
authHeaderKeys.forEach((key: string) => { authHeaderKeys.forEach((key: string) => {
Storage.setItem(key, headers[key]) Storage.setItem(key, headers[key])
@ -42,7 +36,7 @@ export const deleteAuthHeaders = (): void => {
} }
// Will have to take a parameter from the package user to determine if this is for a browser or for React Native: // Will have to take a parameter from the package user to determine if this is for a browser or for React Native:
export const deleteAuthHeadersFromLocalStorage = async (): Promise<void> => { export const deleteAuthHeadersFromLocalStorage = async (Storage: DeviceStorage): Promise<void> => {
// can use multiRemove once you've written it: // can use multiRemove once you've written it:
authHeaderKeys.forEach((key: string) => { authHeaderKeys.forEach((key: string) => {
Storage.removeItem(key) Storage.removeItem(key)

View File

@ -190,3 +190,13 @@ export interface GenerateRequireSignInWrapperConfig {
} }
export type RequireSignInWrapper = (PageComponent: ComponentClass) => ComponentClass export type RequireSignInWrapper = (PageComponent: ComponentClass) => ComponentClass
export interface DeviceStorage {
readonly getItem: (key: string) => Promise<any>
readonly setItem: (key: string, value: string) => Promise<any>
readonly removeItem: (key: string) => Promise<any>
readonly clear: () => Promise<any>
readonly getAllKeys: () => Promise<any>
readonly multiGet: (keys: string[]) => Promise<any>
readonly multiSet: (keyValuePairs: string[][]) => Promise<any>
}