Merge pull request #3 from kylecorbelli/add-types
Make Actions Flexible Based on Developer Configuration
This commit is contained in:
commit
21bbda22a5
99
dist/actions.js
vendored
99
dist/actions.js
vendored
|
@ -89,37 +89,73 @@ exports.signOutRequestFailed = function () { return ({
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
// Async Redux Thunk actions:
|
// Async Redux Thunk actions:
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
// Maybe type this even:
|
// 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
|
||||||
var theActionsExportThatShouldBeRenamed = function (authUrl) {
|
// 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'
|
||||||
|
// },
|
||||||
|
// }
|
||||||
|
// extract this service somewhere and unit test it:
|
||||||
|
var invertHash = function (hash) {
|
||||||
|
var newHash = {};
|
||||||
|
for (var key in hash) {
|
||||||
|
var val = hash[key];
|
||||||
|
newHash[val] = key;
|
||||||
|
}
|
||||||
|
return newHash;
|
||||||
|
};
|
||||||
|
// extract this service somewhere and unit test it:
|
||||||
|
var getUserAttributesFromResponse = function (userAttributes, response) {
|
||||||
|
var invertedUserAttributes = invertHash(userAttributes);
|
||||||
|
var userAttributesBackendKeys = Object.keys(invertedUserAttributes);
|
||||||
|
var userAttributesToReturn = {};
|
||||||
|
Object.keys(response.data.data).forEach(function (key) {
|
||||||
|
if (userAttributesBackendKeys.indexOf(key) !== -1) {
|
||||||
|
userAttributesToReturn[invertedUserAttributes[key]] = response.data.data[key];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return userAttributesToReturn;
|
||||||
|
};
|
||||||
|
var generateAuthActions = function (config) {
|
||||||
|
var authUrl = config.authUrl, userAttributes = config.userAttributes, userRegistrationAttributes = config.userRegistrationAttributes;
|
||||||
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 firstName, email, password, passwordConfirmation, response, userAttributes, error_1;
|
var email, password, passwordConfirmation, data, response, userAttributesToSave, error_1;
|
||||||
return __generator(this, function (_a) {
|
return __generator(this, function (_a) {
|
||||||
switch (_a.label) {
|
switch (_a.label) {
|
||||||
case 0:
|
case 0:
|
||||||
dispatch(exports.registrationRequestSent());
|
dispatch(exports.registrationRequestSent());
|
||||||
firstName = userRegistrationDetails.firstName, email = userRegistrationDetails.email, password = userRegistrationDetails.password, passwordConfirmation = userRegistrationDetails.passwordConfirmation;
|
email = userRegistrationDetails.email, password = userRegistrationDetails.password, passwordConfirmation = userRegistrationDetails.passwordConfirmation;
|
||||||
|
data = {
|
||||||
|
email: email,
|
||||||
|
password: password,
|
||||||
|
password_confirmation: passwordConfirmation,
|
||||||
|
};
|
||||||
|
Object.keys(userRegistrationAttributes).forEach(function (key) {
|
||||||
|
var backendKey = userRegistrationAttributes[key];
|
||||||
|
data[backendKey] = userRegistrationDetails[key];
|
||||||
|
});
|
||||||
_a.label = 1;
|
_a.label = 1;
|
||||||
case 1:
|
case 1:
|
||||||
_a.trys.push([1, 3, , 4]);
|
_a.trys.push([1, 3, , 4]);
|
||||||
return [4 /*yield*/, axios_1.default({
|
return [4 /*yield*/, axios_1.default({
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: authUrl,
|
url: authUrl,
|
||||||
data: {
|
data: data,
|
||||||
email: email,
|
|
||||||
name: firstName,
|
|
||||||
password: password,
|
|
||||||
password_confirmation: passwordConfirmation,
|
|
||||||
},
|
|
||||||
})];
|
})];
|
||||||
case 2:
|
case 2:
|
||||||
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.:
|
||||||
auth_1.persistAuthHeadersInLocalStorage(response.headers);
|
auth_1.persistAuthHeadersInLocalStorage(response.headers);
|
||||||
userAttributes = {
|
userAttributesToSave = getUserAttributesFromResponse(userAttributes, response);
|
||||||
firstName: firstName,
|
dispatch(exports.registrationRequestSucceeded(userAttributesToSave)); // <- need to make this reducer more flexible
|
||||||
};
|
|
||||||
dispatch(exports.registrationRequestSucceeded(userAttributes));
|
|
||||||
return [3 /*break*/, 4];
|
return [3 /*break*/, 4];
|
||||||
case 3:
|
case 3:
|
||||||
error_1 = _a.sent();
|
error_1 = _a.sent();
|
||||||
|
@ -132,7 +168,7 @@ var theActionsExportThatShouldBeRenamed = function (authUrl) {
|
||||||
}; };
|
}; };
|
||||||
var verifyToken = function (verificationParams) { return function (dispatch) {
|
var verifyToken = function (verificationParams) { return function (dispatch) {
|
||||||
return __awaiter(this, void 0, void 0, function () {
|
return __awaiter(this, void 0, void 0, function () {
|
||||||
var response, name_1, userAttributes, error_2;
|
var response, userAttributesToSave, error_2;
|
||||||
return __generator(this, function (_a) {
|
return __generator(this, function (_a) {
|
||||||
switch (_a.label) {
|
switch (_a.label) {
|
||||||
case 0:
|
case 0:
|
||||||
|
@ -147,13 +183,11 @@ var theActionsExportThatShouldBeRenamed = function (authUrl) {
|
||||||
})];
|
})];
|
||||||
case 2:
|
case 2:
|
||||||
response = _a.sent();
|
response = _a.sent();
|
||||||
name_1 = response.data.data.name;
|
|
||||||
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.:
|
||||||
auth_1.persistAuthHeadersInLocalStorage(response.headers);
|
auth_1.persistAuthHeadersInLocalStorage(response.headers);
|
||||||
userAttributes = {
|
userAttributesToSave = getUserAttributesFromResponse(userAttributes, response);
|
||||||
firstName: name_1,
|
dispatch(exports.verifyTokenRequestSucceeded(userAttributesToSave));
|
||||||
};
|
|
||||||
dispatch(exports.verifyTokenRequestSucceeded(userAttributes));
|
|
||||||
return [3 /*break*/, 4];
|
return [3 /*break*/, 4];
|
||||||
case 3:
|
case 3:
|
||||||
error_2 = _a.sent();
|
error_2 = _a.sent();
|
||||||
|
@ -166,7 +200,7 @@ var theActionsExportThatShouldBeRenamed = function (authUrl) {
|
||||||
}; };
|
}; };
|
||||||
var signInUser = function (userSignInCredentials) { return function (dispatch) {
|
var signInUser = function (userSignInCredentials) { return function (dispatch) {
|
||||||
return __awaiter(this, void 0, void 0, function () {
|
return __awaiter(this, void 0, void 0, function () {
|
||||||
var email, password, response, name_2, userAttributes, error_3;
|
var email, password, response, userAttributesToSave, error_3;
|
||||||
return __generator(this, function (_a) {
|
return __generator(this, function (_a) {
|
||||||
switch (_a.label) {
|
switch (_a.label) {
|
||||||
case 0:
|
case 0:
|
||||||
|
@ -186,12 +220,10 @@ var theActionsExportThatShouldBeRenamed = function (authUrl) {
|
||||||
case 2:
|
case 2:
|
||||||
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.:
|
||||||
auth_1.persistAuthHeadersInLocalStorage(response.headers);
|
auth_1.persistAuthHeadersInLocalStorage(response.headers);
|
||||||
name_2 = response.data.data.name;
|
userAttributesToSave = getUserAttributesFromResponse(userAttributes, response);
|
||||||
userAttributes = {
|
dispatch(exports.signInRequestSucceeded(userAttributesToSave));
|
||||||
firstName: name_2,
|
|
||||||
};
|
|
||||||
dispatch(exports.signInRequestSucceeded(userAttributes));
|
|
||||||
return [3 /*break*/, 4];
|
return [3 /*break*/, 4];
|
||||||
case 3:
|
case 3:
|
||||||
error_3 = _a.sent();
|
error_3 = _a.sent();
|
||||||
|
@ -220,6 +252,7 @@ var theActionsExportThatShouldBeRenamed = function (authUrl) {
|
||||||
case 2:
|
case 2:
|
||||||
_a.sent();
|
_a.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.:
|
||||||
auth_1.deleteAuthHeadersFromLocalStorage();
|
auth_1.deleteAuthHeadersFromLocalStorage();
|
||||||
dispatch(exports.signOutRequestSucceeded());
|
dispatch(exports.signOutRequestSucceeded());
|
||||||
return [3 /*break*/, 4];
|
return [3 /*break*/, 4];
|
||||||
|
@ -232,12 +265,24 @@ var theActionsExportThatShouldBeRenamed = function (authUrl) {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}; };
|
}; };
|
||||||
|
var verifyCredentials = function (store) {
|
||||||
|
// Gotta check what the platform is:
|
||||||
|
if (localStorage.getItem('access-token')) {
|
||||||
|
var verificationParams = {
|
||||||
|
'access-token': localStorage.getItem('access-token'),
|
||||||
|
client: localStorage.getItem('client'),
|
||||||
|
uid: localStorage.getItem('uid'),
|
||||||
|
};
|
||||||
|
store.dispatch(verifyToken(verificationParams));
|
||||||
|
}
|
||||||
|
};
|
||||||
return {
|
return {
|
||||||
registerUser: registerUser,
|
registerUser: registerUser,
|
||||||
verifyToken: verifyToken,
|
verifyToken: verifyToken,
|
||||||
signInUser: signInUser,
|
signInUser: signInUser,
|
||||||
signOutUser: signOutUser,
|
signOutUser: signOutUser,
|
||||||
|
verifyCredentials: verifyCredentials,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
exports.default = theActionsExportThatShouldBeRenamed;
|
exports.default = generateAuthActions;
|
||||||
//# sourceMappingURL=actions.js.map
|
//# sourceMappingURL=actions.js.map
|
2
dist/actions.js.map
vendored
2
dist/actions.js.map
vendored
File diff suppressed because one or more lines are too long
2
dist/index.js
vendored
2
dist/index.js
vendored
|
@ -1,7 +1,7 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
var actions_1 = require("./actions");
|
var actions_1 = require("./actions");
|
||||||
exports.theActionsExportThatShouldBeRenamed = actions_1.default;
|
exports.generateAuthActions = actions_1.default;
|
||||||
var reducers_1 = require("./reducers");
|
var reducers_1 = require("./reducers");
|
||||||
exports.reduxTokenAuthReducer = reducers_1.default;
|
exports.reduxTokenAuthReducer = reducers_1.default;
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
2
dist/index.js.map
vendored
2
dist/index.js.map
vendored
|
@ -1 +1 @@
|
||||||
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAAA,qCAA2D;AAIzD,8CAJK,iBAAmC,CAIL;AAHrC,uCAA8C;AAI5C,gCAJK,kBAAqB,CAIL;AAGvB,wHAAwH;AACxH,sDAAsD;AACtD,wHAAwH;AAExH,WAAW;AACX,2BAA2B;AAC3B,4BAA4B;AAC5B,EAAE;AACF,wDAAwD;AACxD,2BAA2B;AAC3B,uBAAuB;AACvB,2BAA2B;AAC3B,IAAI;AACJ,UAAU;AACV,kBAAkB;AAClB,iBAAiB;AACjB,gBAAgB;AAChB,iBAAiB;AACjB,qDAAqD"}
|
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAAA,qCAA2C;AAIzC,8BAJK,iBAAmB,CAIL;AAHrB,uCAA8C;AAI5C,gCAJK,kBAAqB,CAIL;AAGvB,wHAAwH;AACxH,sDAAsD;AACtD,wHAAwH;AAExH,WAAW;AACX,2BAA2B;AAC3B,4BAA4B;AAC5B,EAAE;AACF,wDAAwD;AACxD,2BAA2B;AAC3B,uBAAuB;AACvB,2BAA2B;AAC3B,IAAI;AACJ,UAAU;AACV,kBAAkB;AAClB,iBAAiB;AACjB,gBAAgB;AAChB,iBAAiB;AACjB,qDAAqD"}
|
2
dist/reducers/index.js.map
vendored
2
dist/reducers/index.js.map
vendored
|
@ -1 +1 @@
|
||||||
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/reducers/index.ts"],"names":[],"mappings":";;AAAA,+BAAuC;AACvC,+CAAwC;AAExC,IAAM,qBAAqB,GAAG,uBAAe,CAAC;IAC5C,WAAW,wBAAA;CACZ,CAAC,CAAA;AAEF,kBAAe,qBAAqB,CAAA;AAEpC,wHAAwH;AACxH,4EAA4E;AAC5E,wHAAwH;AAExH,0CAA0C;AAC1C,2DAA2D;AAC3D,wDAAwD;AACxD,EAAE;AACF,wCAAwC;AACxC,2CAA2C;AAC3C,qBAAqB;AACrB,KAAK;AAEL,4EAA4E"}
|
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/reducers/index.ts"],"names":[],"mappings":";;AAAA,+BAGc;AACd,+CAAwC;AAExC,IAAM,qBAAqB,GAAgB,uBAAe,CAAC;IACzD,WAAW,wBAAA;CACZ,CAAC,CAAA;AAEF,kBAAe,qBAAqB,CAAA;AAEpC,wHAAwH;AACxH,4EAA4E;AAC5E,wHAAwH;AAExH,0CAA0C;AAC1C,2DAA2D;AAC3D,wDAAwD;AACxD,EAAE;AACF,wCAAwC;AACxC,2CAA2C;AAC3C,qBAAqB;AACrB,KAAK;AAEL,4EAA4E"}
|
1
dist/types.js
vendored
1
dist/types.js
vendored
|
@ -1,5 +1,4 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
// Maybe make this the index.ts of a types directory so you can split things up
|
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
exports.REGISTRATION_REQUEST_SENT = 'redux-token-auth/REGISTRATION_REQUEST_SENT';
|
exports.REGISTRATION_REQUEST_SENT = 'redux-token-auth/REGISTRATION_REQUEST_SENT';
|
||||||
exports.REGISTRATION_REQUEST_SUCCEEDED = 'redux-token-auth/REGISTRATION_REQUEST_SUCCEEDED';
|
exports.REGISTRATION_REQUEST_SUCCEEDED = 'redux-token-auth/REGISTRATION_REQUEST_SUCCEEDED';
|
||||||
|
|
2
dist/types.js.map
vendored
2
dist/types.js.map
vendored
|
@ -1 +1 @@
|
||||||
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";AAAA,+EAA+E;;AAkClE,QAAA,yBAAyB,GAA8B,4CAA4C,CAAA;AAGnG,QAAA,8BAA8B,GAAmC,iDAAiD,CAAA;AAGlH,QAAA,2BAA2B,GAAgC,8CAA8C,CAAA;AAGzG,QAAA,yBAAyB,GAA8B,4CAA4C,CAAA;AAGnG,QAAA,8BAA8B,GAAmC,iDAAiD,CAAA;AAGlH,QAAA,2BAA2B,GAAgC,8CAA8C,CAAA;AAGzG,QAAA,mBAAmB,GAAwB,sCAAsC,CAAA;AAGjF,QAAA,wBAAwB,GAA6B,2CAA2C,CAAA;AAGhG,QAAA,qBAAqB,GAA0B,wCAAwC,CAAA;AAGvF,QAAA,oBAAoB,GAAyB,uCAAuC,CAAA;AAGpF,QAAA,yBAAyB,GAA8B,4CAA4C,CAAA;AAGnG,QAAA,sBAAsB,GAA2B,yCAAyC,CAAA"}
|
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";;AAwCa,QAAA,yBAAyB,GAA8B,4CAA4C,CAAA;AAGnG,QAAA,8BAA8B,GAAmC,iDAAiD,CAAA;AAGlH,QAAA,2BAA2B,GAAgC,8CAA8C,CAAA;AAGzG,QAAA,yBAAyB,GAA8B,4CAA4C,CAAA;AAGnG,QAAA,8BAA8B,GAAmC,iDAAiD,CAAA;AAGlH,QAAA,2BAA2B,GAAgC,8CAA8C,CAAA;AAGzG,QAAA,mBAAmB,GAAwB,sCAAsC,CAAA;AAGjF,QAAA,wBAAwB,GAA6B,2CAA2C,CAAA;AAGhG,QAAA,qBAAqB,GAA0B,wCAAwC,CAAA;AAGvF,QAAA,oBAAoB,GAAyB,uCAAuC,CAAA;AAGpF,QAAA,yBAAyB,GAA8B,4CAA4C,CAAA;AAGnG,QAAA,sBAAsB,GAA2B,yCAAyC,CAAA"}
|
6
index.d.ts
vendored
Normal file
6
index.d.ts
vendored
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
import { Reducer } from 'redux'
|
||||||
|
import { ActionsGeneratorExport } from './src/types'
|
||||||
|
|
||||||
|
export const reduxTokenAuthReducer: Reducer<{}>
|
||||||
|
|
||||||
|
export const generateAuthActions: ActionsGeneratorExport
|
2
package-lock.json
generated
2
package-lock.json
generated
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "redux-token-auth",
|
"name": "redux-token-auth",
|
||||||
"version": "0.1.0",
|
"version": "0.2.0",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
{
|
{
|
||||||
"name": "redux-token-auth",
|
"name": "redux-token-auth",
|
||||||
"version": "0.2.0",
|
"version": "0.7.0",
|
||||||
"description": "Redux actions and reducers to integrate with Devise Token Auth",
|
"description": "Redux actions and reducers to integrate with Devise Token Auth",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
|
"types": "index.d.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "echo \"Error: no test specified\" && exit 1",
|
"test": "echo \"Error: no test specified\" && exit 1",
|
||||||
"build": "tsc"
|
"build": "tsc"
|
||||||
|
|
111
src/actions.ts
111
src/actions.ts
|
@ -1,6 +1,8 @@
|
||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
// import { authUrl } from '../../constants' // this has to be passed in by the package user
|
import {
|
||||||
import { Dispatch } from 'redux'
|
Dispatch,
|
||||||
|
Store,
|
||||||
|
} from 'redux'
|
||||||
import {
|
import {
|
||||||
AuthResponse,
|
AuthResponse,
|
||||||
VerificationParams,
|
VerificationParams,
|
||||||
|
@ -8,6 +10,7 @@ import {
|
||||||
UserRegistrationDetails,
|
UserRegistrationDetails,
|
||||||
UserSignInCredentials,
|
UserSignInCredentials,
|
||||||
UserSignOutCredentials,
|
UserSignOutCredentials,
|
||||||
|
ActionsExport,
|
||||||
REGISTRATION_REQUEST_SENT,
|
REGISTRATION_REQUEST_SENT,
|
||||||
REGISTRATION_REQUEST_SUCCEEDED,
|
REGISTRATION_REQUEST_SUCCEEDED,
|
||||||
REGISTRATION_REQUEST_FAILED,
|
REGISTRATION_REQUEST_FAILED,
|
||||||
|
@ -105,36 +108,78 @@ export const signOutRequestFailed = (): SignOutRequestFailedAction => ({
|
||||||
// Async Redux Thunk actions:
|
// Async Redux Thunk actions:
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
// Maybe type this even:
|
// 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
|
||||||
const theActionsExportThatShouldBeRenamed = (authUrl: string) => {
|
// 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'
|
||||||
|
// },
|
||||||
|
// }
|
||||||
|
|
||||||
|
// extract this service somewhere and unit test it:
|
||||||
|
const invertHash = (hash: { [key: string]: any }) => {
|
||||||
|
const newHash = {}
|
||||||
|
for (let key in hash) {
|
||||||
|
const val = hash[key]
|
||||||
|
newHash[val] = key
|
||||||
|
}
|
||||||
|
return newHash
|
||||||
|
}
|
||||||
|
|
||||||
|
// extract this service somewhere and unit test it:
|
||||||
|
const getUserAttributesFromResponse = (userAttributes: any, response: any) => {
|
||||||
|
const invertedUserAttributes = invertHash(userAttributes)
|
||||||
|
const userAttributesBackendKeys = Object.keys(invertedUserAttributes)
|
||||||
|
const userAttributesToReturn = {}
|
||||||
|
Object.keys(response.data.data).forEach((key: string) => {
|
||||||
|
if (userAttributesBackendKeys.indexOf(key) !== -1) {
|
||||||
|
userAttributesToReturn[invertedUserAttributes[key]] = response.data.data[key]
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return userAttributesToReturn
|
||||||
|
}
|
||||||
|
|
||||||
|
const generateAuthActions = (config: { [key: string]: any }): ActionsExport => {
|
||||||
|
const {
|
||||||
|
authUrl,
|
||||||
|
userAttributes,
|
||||||
|
userRegistrationAttributes,
|
||||||
|
} = config
|
||||||
|
|
||||||
const registerUser = (
|
const registerUser = (
|
||||||
userRegistrationDetails: UserRegistrationDetails,
|
userRegistrationDetails: UserRegistrationDetails,
|
||||||
) => async function (dispatch: Dispatch<{}>): Promise<void> {
|
) => async function (dispatch: Dispatch<{}>): Promise<void> {
|
||||||
dispatch(registrationRequestSent())
|
dispatch(registrationRequestSent())
|
||||||
const {
|
const {
|
||||||
firstName,
|
|
||||||
email,
|
email,
|
||||||
password,
|
password,
|
||||||
passwordConfirmation,
|
passwordConfirmation,
|
||||||
} = userRegistrationDetails
|
} = userRegistrationDetails
|
||||||
|
const data = {
|
||||||
|
email,
|
||||||
|
password,
|
||||||
|
password_confirmation: passwordConfirmation,
|
||||||
|
}
|
||||||
|
Object.keys(userRegistrationAttributes).forEach((key: string) => {
|
||||||
|
const backendKey = userRegistrationAttributes[key]
|
||||||
|
data[backendKey] = userRegistrationDetails[key]
|
||||||
|
})
|
||||||
try {
|
try {
|
||||||
const response: AuthResponse = await axios({
|
const response: AuthResponse = await axios({
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: authUrl,
|
url: authUrl,
|
||||||
data: {
|
data,
|
||||||
email,
|
|
||||||
name: firstName, // even this is tricky because it requires the user's devise configuration to allow the "name" attribute
|
|
||||||
password,
|
|
||||||
password_confirmation: passwordConfirmation,
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
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.:
|
||||||
persistAuthHeadersInLocalStorage(response.headers)
|
persistAuthHeadersInLocalStorage(response.headers)
|
||||||
// Gonna need to refer to the passed-in User model configuration from the package user
|
const userAttributesToSave = getUserAttributesFromResponse(userAttributes, response)
|
||||||
const userAttributes: UserAttributes = {
|
dispatch(registrationRequestSucceeded(userAttributesToSave)) // <- need to make this reducer more flexible
|
||||||
firstName,
|
|
||||||
}
|
|
||||||
dispatch(registrationRequestSucceeded(userAttributes))
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
dispatch(registrationRequestFailed())
|
dispatch(registrationRequestFailed())
|
||||||
throw error
|
throw error
|
||||||
|
@ -151,14 +196,11 @@ const theActionsExportThatShouldBeRenamed = (authUrl: string) => {
|
||||||
url: `${authUrl}/validate_token`,
|
url: `${authUrl}/validate_token`,
|
||||||
params: verificationParams,
|
params: verificationParams,
|
||||||
})
|
})
|
||||||
const { name } = response.data.data
|
|
||||||
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.:
|
||||||
persistAuthHeadersInLocalStorage(response.headers)
|
persistAuthHeadersInLocalStorage(response.headers)
|
||||||
// Gonna need to refer to the passed-in User model configuration from the package user
|
const userAttributesToSave = getUserAttributesFromResponse(userAttributes, response)
|
||||||
const userAttributes: UserAttributes = {
|
dispatch(verifyTokenRequestSucceeded(userAttributesToSave))
|
||||||
firstName: name,
|
|
||||||
}
|
|
||||||
dispatch(verifyTokenRequestSucceeded(userAttributes))
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
dispatch(verifyTokenRequestFailed())
|
dispatch(verifyTokenRequestFailed())
|
||||||
}
|
}
|
||||||
|
@ -182,13 +224,10 @@ const theActionsExportThatShouldBeRenamed = (authUrl: string) => {
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
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.:
|
||||||
persistAuthHeadersInLocalStorage(response.headers)
|
persistAuthHeadersInLocalStorage(response.headers)
|
||||||
// Gonna need to refer to the passed-in User model configuration from the package user
|
const userAttributesToSave = getUserAttributesFromResponse(userAttributes, response)
|
||||||
const { name } = response.data.data
|
dispatch(signInRequestSucceeded(userAttributesToSave))
|
||||||
const userAttributes: UserAttributes = {
|
|
||||||
firstName: name,
|
|
||||||
}
|
|
||||||
dispatch(signInRequestSucceeded(userAttributes))
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
dispatch(signInRequestFailed())
|
dispatch(signInRequestFailed())
|
||||||
throw error
|
throw error
|
||||||
|
@ -206,6 +245,7 @@ const theActionsExportThatShouldBeRenamed = (authUrl: string) => {
|
||||||
data: userSignOutCredentials,
|
data: userSignOutCredentials,
|
||||||
})
|
})
|
||||||
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.:
|
||||||
deleteAuthHeadersFromLocalStorage()
|
deleteAuthHeadersFromLocalStorage()
|
||||||
dispatch(signOutRequestSucceeded())
|
dispatch(signOutRequestSucceeded())
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
@ -214,12 +254,25 @@ const theActionsExportThatShouldBeRenamed = (authUrl: string) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const verifyCredentials = (store: Store<{}>): void => {
|
||||||
|
// Gotta check what the platform is:
|
||||||
|
if (localStorage.getItem('access-token')) {
|
||||||
|
const verificationParams: VerificationParams = {
|
||||||
|
'access-token': localStorage.getItem('access-token') as string,
|
||||||
|
client: localStorage.getItem('client') as string,
|
||||||
|
uid: localStorage.getItem('uid') as string,
|
||||||
|
}
|
||||||
|
store.dispatch<any>(verifyToken(verificationParams))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
registerUser,
|
registerUser,
|
||||||
verifyToken,
|
verifyToken,
|
||||||
signInUser,
|
signInUser,
|
||||||
signOutUser,
|
signOutUser,
|
||||||
|
verifyCredentials,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default theActionsExportThatShouldBeRenamed
|
export default generateAuthActions
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
import theActionsExportThatShouldBeRenamed from './actions'
|
import generateAuthActions from './actions'
|
||||||
import reduxTokenAuthReducer from './reducers'
|
import reduxTokenAuthReducer from './reducers'
|
||||||
|
|
||||||
export {
|
export {
|
||||||
theActionsExportThatShouldBeRenamed,
|
generateAuthActions,
|
||||||
reduxTokenAuthReducer,
|
reduxTokenAuthReducer,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
import { combineReducers } from 'redux'
|
import {
|
||||||
|
combineReducers,
|
||||||
|
Reducer,
|
||||||
|
} from 'redux'
|
||||||
import currentUser from './current-user'
|
import currentUser from './current-user'
|
||||||
|
|
||||||
const reduxTokenAuthReducer = combineReducers({
|
const reduxTokenAuthReducer: Reducer<{}> = combineReducers({
|
||||||
currentUser,
|
currentUser,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
24
src/types.ts
24
src/types.ts
|
@ -1,4 +1,7 @@
|
||||||
// Maybe make this the index.ts of a types directory so you can split things up
|
import {
|
||||||
|
Dispatch,
|
||||||
|
Store,
|
||||||
|
} from 'redux'
|
||||||
|
|
||||||
// This one in particular will be a little tough because we don't know what the package user's "User" model looks like:
|
// This one in particular will be a little tough because we don't know what the package user's "User" model looks like:
|
||||||
export interface UserAttributes {}
|
export interface UserAttributes {}
|
||||||
|
@ -23,6 +26,9 @@ export interface AuthHeaders {
|
||||||
|
|
||||||
export interface AuthResponse {
|
export interface AuthResponse {
|
||||||
readonly headers: AuthHeaders
|
readonly headers: AuthHeaders
|
||||||
|
readonly data: {
|
||||||
|
readonly data: { [key: string]: any }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface VerificationParams {
|
export interface VerificationParams {
|
||||||
|
@ -68,10 +74,10 @@ export type SIGNOUT_REQUEST_FAILED = 'redux-token-auth/SIGNOUT_REQUEST_FAILED'
|
||||||
export const SIGNOUT_REQUEST_FAILED: SIGNOUT_REQUEST_FAILED = 'redux-token-auth/SIGNOUT_REQUEST_FAILED'
|
export const SIGNOUT_REQUEST_FAILED: SIGNOUT_REQUEST_FAILED = 'redux-token-auth/SIGNOUT_REQUEST_FAILED'
|
||||||
|
|
||||||
export interface UserRegistrationDetails {
|
export interface UserRegistrationDetails {
|
||||||
readonly firstName: string
|
|
||||||
readonly email: string
|
readonly email: string
|
||||||
readonly password: string
|
readonly password: string
|
||||||
readonly passwordConfirmation: string
|
readonly passwordConfirmation: string
|
||||||
|
readonly [key: string]: any
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface UserSignInCredentials {
|
export interface UserSignInCredentials {
|
||||||
|
@ -154,3 +160,17 @@ export type ReduxAction = RegistrationRequestSentAction
|
||||||
| SignOutRequestSentAction
|
| SignOutRequestSentAction
|
||||||
| SignOutRequestSucceededAction
|
| SignOutRequestSucceededAction
|
||||||
| SignOutRequestFailedAction
|
| SignOutRequestFailedAction
|
||||||
|
|
||||||
|
export type ReduxAsyncAction = (input: any) => (dispatch: Dispatch<{}>) => Promise<void>
|
||||||
|
|
||||||
|
export type VerifyCredentialsFunction = (store: Store<{}>) => void
|
||||||
|
|
||||||
|
export interface ActionsExport {
|
||||||
|
readonly registerUser: ReduxAsyncAction
|
||||||
|
readonly verifyToken: ReduxAsyncAction
|
||||||
|
readonly signInUser: ReduxAsyncAction
|
||||||
|
readonly signOutUser: ReduxAsyncAction
|
||||||
|
readonly verifyCredentials: VerifyCredentialsFunction
|
||||||
|
}
|
||||||
|
|
||||||
|
export type ActionsGeneratorExport = (config: { [key: string]: any }) => ActionsExport
|
||||||
|
|
Loading…
Reference in New Issue
Block a user