Make registration more flexible

This commit is contained in:
Kyle Corbelli 2017-09-03 17:20:23 -07:00
parent c1bbe9eb68
commit ba162911d7
7 changed files with 120 additions and 40 deletions

78
dist/actions.js vendored
View File

@ -89,36 +89,70 @@ exports.signOutRequestFailed = function () { return ({
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Async Redux Thunk actions:
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var generateAuthActions = function (authUrl) {
// 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'
// },
// }
// extract this service somewhere:
var invertHash = function (hash) {
var newHash = {};
for (var key in hash) {
var val = hash[key];
newHash[val] = key;
}
return newHash;
};
var generateAuthActions = function (config) {
var authUrl = config.authUrl, userAttributes = config.userAttributes, userRegistrationAttributes = config.userRegistrationAttributes;
var registerUser = function (userRegistrationDetails) { return function (dispatch) {
return __awaiter(this, void 0, void 0, function () {
var firstName, email, password, passwordConfirmation, response, userAttributes, error_1;
var email, password, passwordConfirmation, data, response_1, invertedUserAttributes_1, userAttributesBackendKeys_1, userAttributesToSave_1, error_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
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;
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, axios_1.default({
method: 'POST',
url: authUrl,
data: {
email: email,
name: firstName,
password: password,
password_confirmation: passwordConfirmation,
},
data: data,
})];
case 2:
response = _a.sent();
auth_1.setAuthHeaders(response.headers);
auth_1.persistAuthHeadersInLocalStorage(response.headers);
userAttributes = {
firstName: firstName,
};
dispatch(exports.registrationRequestSucceeded(userAttributes));
response_1 = _a.sent();
auth_1.setAuthHeaders(response_1.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_1.headers);
invertedUserAttributes_1 = invertHash(userAttributes);
userAttributesBackendKeys_1 = Object.keys(invertedUserAttributes_1);
userAttributesToSave_1 = {};
Object.keys(response_1.data.data).forEach(function (key) {
if (userAttributesBackendKeys_1.indexOf(key) !== -1) {
userAttributesToSave_1[invertedUserAttributes_1[key]] = response_1.data.data[key];
}
});
console.log('userAttributesToSave');
console.log(userAttributesToSave_1);
dispatch(exports.registrationRequestSucceeded(userAttributesToSave_1)); // <- need to make this reducer more flexible
return [3 /*break*/, 4];
case 3:
error_1 = _a.sent();
@ -131,7 +165,7 @@ var generateAuthActions = function (authUrl) {
}; };
var verifyToken = function (verificationParams) { return function (dispatch) {
return __awaiter(this, void 0, void 0, function () {
var response, name_1, userAttributes, error_2;
var response, name_1, userAttributes_1, error_2;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
@ -149,10 +183,10 @@ var generateAuthActions = function (authUrl) {
name_1 = response.data.data.name;
auth_1.setAuthHeaders(response.headers);
auth_1.persistAuthHeadersInLocalStorage(response.headers);
userAttributes = {
userAttributes_1 = {
firstName: name_1,
};
dispatch(exports.verifyTokenRequestSucceeded(userAttributes));
dispatch(exports.verifyTokenRequestSucceeded(userAttributes_1));
return [3 /*break*/, 4];
case 3:
error_2 = _a.sent();
@ -165,7 +199,7 @@ var generateAuthActions = function (authUrl) {
}; };
var signInUser = function (userSignInCredentials) { return function (dispatch) {
return __awaiter(this, void 0, void 0, function () {
var email, password, response, name_2, userAttributes, error_3;
var email, password, response, name_2, userAttributes_2, error_3;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
@ -187,10 +221,10 @@ var generateAuthActions = function (authUrl) {
auth_1.setAuthHeaders(response.headers);
auth_1.persistAuthHeadersInLocalStorage(response.headers);
name_2 = response.data.data.name;
userAttributes = {
userAttributes_2 = {
firstName: name_2,
};
dispatch(exports.signInRequestSucceeded(userAttributes));
dispatch(exports.signInRequestSucceeded(userAttributes_2));
return [3 /*break*/, 4];
case 3:
error_3 = _a.sent();

2
dist/actions.js.map vendored

File diff suppressed because one or more lines are too long

2
dist/index.js.map vendored
View File

@ -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/types.js.map vendored
View File

@ -1 +1 @@
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";;AAkCa,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":";;AAqCa,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"}

View File

@ -1,6 +1,6 @@
{
"name": "redux-token-auth",
"version": "0.4.0",
"version": "0.6.0",
"description": "Redux actions and reducers to integrate with Devise Token Auth",
"main": "dist/index.js",
"types": "index.d.ts",

View File

@ -105,35 +105,78 @@ export const signOutRequestFailed = (): SignOutRequestFailedAction => ({
// Async Redux Thunk actions:
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const generateAuthActions = (authUrl: string): ActionsExport => {
// 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'
// },
// }
// extract this service somewhere:
const invertHash = (hash: { [key: string]: any }) => {
const newHash = {}
for (let key in hash) {
const val = hash[key]
newHash[val] = key
}
return newHash
}
const generateAuthActions = (config: { [key: string]: any }): ActionsExport => {
const {
authUrl,
userAttributes,
userRegistrationAttributes,
} = config
const registerUser = (
userRegistrationDetails: UserRegistrationDetails,
) => async function (dispatch: Dispatch<{}>): Promise<void> {
dispatch(registrationRequestSent())
const {
firstName,
email,
password,
passwordConfirmation,
} = userRegistrationDetails
const data = {
email,
password,
password_confirmation: passwordConfirmation,
}
Object.keys(userRegistrationAttributes).forEach((key: string) => {
const backendKey = userRegistrationAttributes[key]
data[backendKey] = userRegistrationDetails[key]
})
try {
const response: AuthResponse = await axios({
method: 'POST',
url: authUrl,
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,
},
data,
})
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)
// Gonna need to refer to the passed-in User model configuration from the package user
const userAttributes: UserAttributes = {
firstName,
}
dispatch(registrationRequestSucceeded(userAttributes))
// const userAttributes: UserAttributes = {
// firstName,
// }
const invertedUserAttributes = invertHash(userAttributes)
const userAttributesBackendKeys = Object.keys(invertedUserAttributes)
const userAttributesToSave = {}
Object.keys(response.data.data).forEach((key: string) => {
if (userAttributesBackendKeys.indexOf(key) !== -1) {
userAttributesToSave[invertedUserAttributes[key]] = response.data.data[key]
}
})
console.log('userAttributesToSave')
console.log(userAttributesToSave)
dispatch(registrationRequestSucceeded(userAttributesToSave)) // <- need to make this reducer more flexible
} catch (error) {
dispatch(registrationRequestFailed())
throw error

View File

@ -23,6 +23,9 @@ export interface AuthHeaders {
export interface AuthResponse {
readonly headers: AuthHeaders
readonly data: {
readonly data: { [key: string]: any }
}
}
export interface VerificationParams {
@ -68,10 +71,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 interface UserRegistrationDetails {
readonly firstName: string
readonly email: string
readonly password: string
readonly passwordConfirmation: string
readonly [key: string]: any
}
export interface UserSignInCredentials {
@ -164,4 +167,4 @@ export interface ActionsExport {
readonly signOutUser: ReduxAsyncAction
}
export type ActionsGeneratorExport = (authUrl: string) => ActionsExport
export type ActionsGeneratorExport = (config: { [key: string]: any }) => ActionsExport