Make other auth actions flexible too and add verifyCredentials

This commit is contained in:
Kyle Corbelli 2017-09-03 18:03:46 -07:00
parent ba162911d7
commit c45677c3d2
6 changed files with 89 additions and 60 deletions

68
dist/actions.js vendored
View File

@ -101,7 +101,7 @@ exports.signOutRequestFailed = function () { return ({
// firstName: 'name' // firstName: 'name'
// }, // },
// } // }
// extract this service somewhere: // extract this service somewhere and unit test it:
var invertHash = function (hash) { var invertHash = function (hash) {
var newHash = {}; var newHash = {};
for (var key in hash) { for (var key in hash) {
@ -110,11 +110,23 @@ var invertHash = function (hash) {
} }
return newHash; 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 generateAuthActions = function (config) {
var authUrl = config.authUrl, userAttributes = config.userAttributes, userRegistrationAttributes = config.userRegistrationAttributes; 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 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) { return __generator(this, function (_a) {
switch (_a.label) { switch (_a.label) {
case 0: case 0:
@ -138,21 +150,12 @@ var generateAuthActions = function (config) {
data: data, data: data,
})]; })];
case 2: case 2:
response_1 = _a.sent(); response = _a.sent();
auth_1.setAuthHeaders(response_1.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_1.headers); auth_1.persistAuthHeadersInLocalStorage(response.headers);
invertedUserAttributes_1 = invertHash(userAttributes); userAttributesToSave = getUserAttributesFromResponse(userAttributes, response);
userAttributesBackendKeys_1 = Object.keys(invertedUserAttributes_1); dispatch(exports.registrationRequestSucceeded(userAttributesToSave)); // <- need to make this reducer more flexible
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]; return [3 /*break*/, 4];
case 3: case 3:
error_1 = _a.sent(); error_1 = _a.sent();
@ -165,7 +168,7 @@ var generateAuthActions = function (config) {
}; }; }; };
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_1, 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:
@ -180,13 +183,11 @@ var generateAuthActions = function (config) {
})]; })];
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_1 = { userAttributesToSave = getUserAttributesFromResponse(userAttributes, response);
firstName: name_1, dispatch(exports.verifyTokenRequestSucceeded(userAttributesToSave));
};
dispatch(exports.verifyTokenRequestSucceeded(userAttributes_1));
return [3 /*break*/, 4]; return [3 /*break*/, 4];
case 3: case 3:
error_2 = _a.sent(); error_2 = _a.sent();
@ -199,7 +200,7 @@ var generateAuthActions = function (config) {
}; }; }; };
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_2, 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:
@ -219,12 +220,10 @@ var generateAuthActions = function (config) {
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_2 = { dispatch(exports.signInRequestSucceeded(userAttributesToSave));
firstName: name_2,
};
dispatch(exports.signInRequestSucceeded(userAttributes_2));
return [3 /*break*/, 4]; return [3 /*break*/, 4];
case 3: case 3:
error_3 = _a.sent(); error_3 = _a.sent();
@ -253,6 +252,7 @@ var generateAuthActions = function (config) {
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];
@ -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 { return {
registerUser: registerUser, registerUser: registerUser,
verifyToken: verifyToken, verifyToken: verifyToken,
signInUser: signInUser, signInUser: signInUser,
signOutUser: signOutUser, signOutUser: signOutUser,
verifyCredentials: verifyCredentials,
}; };
}; };
exports.default = generateAuthActions; exports.default = generateAuthActions;

2
dist/actions.js.map vendored

File diff suppressed because one or more lines are too long

2
dist/types.js.map vendored
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"}

View File

@ -1,6 +1,6 @@
{ {
"name": "redux-token-auth", "name": "redux-token-auth",
"version": "0.6.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", "types": "index.d.ts",

View File

@ -1,5 +1,8 @@
import axios from 'axios' import axios from 'axios'
import { Dispatch } from 'redux' import {
Dispatch,
Store,
} from 'redux'
import { import {
AuthResponse, AuthResponse,
VerificationParams, 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 invertHash = (hash: { [key: string]: any }) => {
const newHash = {} const newHash = {}
for (let key in hash) { for (let key in hash) {
@ -128,6 +131,19 @@ const invertHash = (hash: { [key: string]: any }) => {
return newHash 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 generateAuthActions = (config: { [key: string]: any }): ActionsExport => {
const { const {
authUrl, authUrl,
@ -162,20 +178,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) 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 = {
// 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 dispatch(registrationRequestSucceeded(userAttributesToSave)) // <- need to make this reducer more flexible
} catch (error) { } catch (error) {
dispatch(registrationRequestFailed()) dispatch(registrationRequestFailed())
@ -193,14 +196,11 @@ const generateAuthActions = (config: { [key: string]: any }): ActionsExport => {
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())
} }
@ -224,13 +224,10 @@ 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.:
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
@ -248,6 +245,7 @@ const generateAuthActions = (config: { [key: string]: any }): ActionsExport => {
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) {
@ -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 { return {
registerUser, registerUser,
verifyToken, verifyToken,
signInUser, signInUser,
signOutUser, signOutUser,
verifyCredentials,
} }
} }

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: // 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 {}
@ -160,11 +163,14 @@ export type ReduxAction = RegistrationRequestSentAction
export type ReduxAsyncAction = (input: any) => (dispatch: Dispatch<{}>) => Promise<void> export type ReduxAsyncAction = (input: any) => (dispatch: Dispatch<{}>) => Promise<void>
export type VerifyCredentialsFunction = (store: Store<{}>) => void
export interface ActionsExport { export interface ActionsExport {
readonly registerUser: ReduxAsyncAction readonly registerUser: ReduxAsyncAction
readonly verifyToken: ReduxAsyncAction readonly verifyToken: ReduxAsyncAction
readonly signInUser: ReduxAsyncAction readonly signInUser: ReduxAsyncAction
readonly signOutUser: ReduxAsyncAction readonly signOutUser: ReduxAsyncAction
readonly verifyCredentials: VerifyCredentialsFunction
} }
export type ActionsGeneratorExport = (config: { [key: string]: any }) => ActionsExport export type ActionsGeneratorExport = (config: { [key: string]: any }) => ActionsExport