Make other auth actions flexible too and add verifyCredentials

This commit is contained in:
Kyle Corbelli committed 2017-09-03 18:03:46 -07:00
1 parent ba162911d7
commit c45677c3d2
6 files changed
+89 -60

No files matched your search

+40 -28
View File
@@ -101,7 +101,7 @@ exports.signOutRequestFailed = function () { return ({
// firstName: 'name'
// },
// }
// extract this service somewhere:
// extract this service somewhere and unit test it:
var invertHash = function (hash) {
var newHash = {};
for (var key in hash) {
@@ -110,11 +110,23 @@ var invertHash = function (hash) {
}
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) {
return __awaiter(this, void 0, void 0, function () {
var email, password, passwordConfirmation, data, response_1, invertedUserAttributes_1, userAttributesBackendKeys_1, userAttributesToSave_1, error_1;
var email, password, passwordConfirmation, data, response, userAttributesToSave, error_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
@@ -138,21 +150,12 @@ var generateAuthActions = function (config) {
data: data,
})];
case 2:
response_1 = _a.sent();
auth_1.setAuthHeaders(response_1.headers);
response = _a.sent();
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_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
auth_1.persistAuthHeadersInLocalStorage(response.headers);
userAttributesToSave = getUserAttributesFromResponse(userAttributes, response);
dispatch(exports.registrationRequestSucceeded(userAttributesToSave)); // <- need to make this reducer more flexible
return [3 /*break*/, 4];
case 3:
error_1 = _a.sent();
@@ -165,7 +168,7 @@ var generateAuthActions = function (config) {
}; };
var verifyToken = function (verificationParams) { return function (dispatch) {
return __awaiter(this, void 0, void 0, function () {
var response, name_1, userAttributes_1, error_2;
var response, userAttributesToSave, error_2;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
@@ -180,13 +183,11 @@ var generateAuthActions = function (config) {
})];
case 2:
response = _a.sent();
name_1 = response.data.data.name;
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);
userAttributes_1 = {
firstName: name_1,
};
dispatch(exports.verifyTokenRequestSucceeded(userAttributes_1));
userAttributesToSave = getUserAttributesFromResponse(userAttributes, response);
dispatch(exports.verifyTokenRequestSucceeded(userAttributesToSave));
return [3 /*break*/, 4];
case 3:
error_2 = _a.sent();
@@ -199,7 +200,7 @@ var generateAuthActions = function (config) {
}; };
var signInUser = function (userSignInCredentials) { return function (dispatch) {
return __awaiter(this, void 0, void 0, function () {
var email, password, response, name_2, userAttributes_2, error_3;
var email, password, response, userAttributesToSave, error_3;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
@@ -219,12 +220,10 @@ var generateAuthActions = function (config) {
case 2:
response = _a.sent();
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);
name_2 = response.data.data.name;
userAttributes_2 = {
firstName: name_2,
};
dispatch(exports.signInRequestSucceeded(userAttributes_2));
userAttributesToSave = getUserAttributesFromResponse(userAttributes, response);
dispatch(exports.signInRequestSucceeded(userAttributesToSave));
return [3 /*break*/, 4];
case 3:
error_3 = _a.sent();
@@ -253,6 +252,7 @@ var generateAuthActions = function (config) {
case 2:
_a.sent();
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();
dispatch(exports.signOutRequestSucceeded());
return [3 /*break*/, 4];
@@ -265,11 +265,23 @@ var generateAuthActions = function (config) {
});
});
}; };
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 {
registerUser: registerUser,
verifyToken: verifyToken,
signInUser: signInUser,
signOutUser: signOutUser,
verifyCredentials: verifyCredentials,
};
};
exports.default = generateAuthActions;
+1 -1
View File
@@ -1 +1 @@
{"version":3,"file":"actions.js","sourceRoot":"","sources":["../src/actions.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,+BAAyB;AAEzB,iCAgCgB;AAChB,wCAKwB,CAAC,kHAAkH;AAE3I,wHAAwH;AACxH,sBAAsB;AACtB,wHAAwH;AAE3G,QAAA,uBAAuB,GAAG,cAAqC,OAAA,CAAC;IAC3E,IAAI,EAAE,iCAAyB;CAChC,CAAC,EAF0E,CAE1E,CAAA;AAEW,QAAA,4BAA4B,GAAG,UAAC,cAA8B,IAAyC,OAAA,CAAC;IACnH,IAAI,EAAE,sCAA8B;IACpC,OAAO,EAAE;QACP,cAAc,gBAAA;KACf;CACF,CAAC,EALkH,CAKlH,CAAA;AAEW,QAAA,yBAAyB,GAAG,cAAuC,OAAA,CAAC;IAC/E,IAAI,EAAE,mCAA2B;C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AAA;;;;;KAEd,EAhBI,CAgBJ,CAAA;IAED,IAAM,iBAAiB,GAAG,UALine truncated
+1 -1
View File
@@ -1 +1 @@
{"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"}
{"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"}
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "redux-token-auth",
"version": "0.6.0",
"version": "0.7.0",
"description": "Redux actions and reducers to integrate with Devise Token Auth",
"main": "dist/index.js",
"types": "index.d.ts",
+39 -28
View File
@@ -1,5 +1,8 @@
import axios from 'axios'
import { Dispatch } from 'redux'
import {
Dispatch,
Store,
} from 'redux'
import {
AuthResponse,
VerificationParams,
@@ -118,7 +121,7 @@ export const signOutRequestFailed = (): SignOutRequestFailedAction => ({
// },
// }
// extract this service somewhere:
// extract this service somewhere and unit test it:
const invertHash = (hash: { [key: string]: any }) => {
const newHash = {}
for (let key in hash) {
@@ -128,6 +131,19 @@ const invertHash = (hash: { [key: string]: any }) => {
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,
@@ -162,20 +178,7 @@ const generateAuthActions = (config: { [key: string]: any }): ActionsExport => {
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,
// }
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)
const userAttributesToSave = getUserAttributesFromResponse(userAttributes, response)
dispatch(registrationRequestSucceeded(userAttributesToSave)) // <- need to make this reducer more flexible
} catch (error) {
dispatch(registrationRequestFailed())
@@ -193,14 +196,11 @@ const generateAuthActions = (config: { [key: string]: any }): ActionsExport => {
url: `${authUrl}/validate_token`,
params: verificationParams,
})
const { name } = response.data.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: name,
}
dispatch(verifyTokenRequestSucceeded(userAttributes))
const userAttributesToSave = getUserAttributesFromResponse(userAttributes, response)
dispatch(verifyTokenRequestSucceeded(userAttributesToSave))
} catch (error) {
dispatch(verifyTokenRequestFailed())
}
@@ -224,13 +224,10 @@ const generateAuthActions = (config: { [key: string]: any }): ActionsExport => {
},
})
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 { name } = response.data.data
const userAttributes: UserAttributes = {
firstName: name,
}
dispatch(signInRequestSucceeded(userAttributes))
const userAttributesToSave = getUserAttributesFromResponse(userAttributes, response)
dispatch(signInRequestSucceeded(userAttributesToSave))
} catch (error) {
dispatch(signInRequestFailed())
throw error
@@ -248,6 +245,7 @@ const generateAuthActions = (config: { [key: string]: any }): ActionsExport => {
data: userSignOutCredentials,
})
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()
dispatch(signOutRequestSucceeded())
} catch (error) {
@@ -256,11 +254,24 @@ const generateAuthActions = (config: { [key: string]: any }): ActionsExport => {
}
}
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 {
registerUser,
verifyToken,
signInUser,
signOutUser,
verifyCredentials,
}
}
+7 -1
View File
@@ -1,4 +1,7 @@
import { Dispatch } from 'redux'
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:
export interface UserAttributes {}
@@ -160,11 +163,14 @@ export type ReduxAction = RegistrationRequestSentAction
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