Make other auth actions flexible too and add verifyCredentials
This commit is contained in:
1 parent
ba162911d7
commit
c45677c3d2
6 files changed
+89
-60
No files matched your search
+39
-28
@@ -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
@@ -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
|
||||
Reference in new issue
Block a user