Merge pull request #11 from kylecorbelli/improve-signoutuser

Improve signOutUser
This commit is contained in:
Kyle Corbelli 2017-09-15 20:06:14 -07:00 committed by GitHub
commit c078f46628
5 changed files with 24 additions and 9 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "redux-token-auth", "name": "redux-token-auth",
"version": "0.11.0", "version": "0.12.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

@ -212,9 +212,12 @@ const generateAuthActions = (config: { [key: string]: any }): ActionsExport => {
} }
} }
const signOutUser = ( const signOutUser = () => async function (dispatch: Dispatch<{}>): Promise<void> {
userSignOutCredentials: UserSignOutCredentials, const userSignOutCredentials: UserSignOutCredentials = {
) => async function (dispatch: Dispatch<{}>): Promise<void> { 'access-token': localStorage.getItem('access-token') as string,
client: localStorage.getItem('client') as string,
uid: localStorage.getItem('uid') as string,
}
dispatch(signOutRequestSent()) dispatch(signOutRequestSent())
try { try {
await axios({ await axios({

View File

@ -42,6 +42,7 @@ describe('currentUser', () => {
const loggedInUser: User = { const loggedInUser: User = {
attributes: { attributes: {
firstName: 'Snowball', firstName: 'Snowball',
imageUrl: 'http://some.url',
}, },
isLoading: false, isLoading: false,
isLoggedIn: true, isLoggedIn: true,
@ -169,6 +170,7 @@ describe('currentUser', () => {
const expectedNewState: User = { const expectedNewState: User = {
attributes: { attributes: {
firstName: null, firstName: null,
imageUrl: null,
}, },
isLoading: false, isLoading: false,
isLoggedIn: false, isLoggedIn: false,

View File

@ -1,5 +1,6 @@
import { import {
User, User,
UserAttributes,
ReduxAction, ReduxAction,
REGISTRATION_REQUEST_SENT, REGISTRATION_REQUEST_SENT,
REGISTRATION_REQUEST_SUCCEEDED, REGISTRATION_REQUEST_SUCCEEDED,
@ -49,12 +50,19 @@ const currentUser = (state: User = initialUser, action: ReduxAction): User => {
isLoggedIn: false, isLoggedIn: false,
} }
case SIGNOUT_REQUEST_SUCCEEDED: case SIGNOUT_REQUEST_SUCCEEDED:
const userAttributeKeys: string[] = Object.keys(state.attributes)
const allNullUserAttributes: UserAttributes = userAttributeKeys.reduce(
(accumulatedNullUserAttributes: UserAttributes, currentUserAttributeKey: string): UserAttributes => {
return {
...accumulatedNullUserAttributes,
[currentUserAttributeKey]: null,
}
},
{},
)
return { return {
...state, ...state,
attributes: { attributes: allNullUserAttributes,
...state.attributes,
firstName: null,
},
isLoading: false, isLoading: false,
isLoggedIn: false, isLoggedIn: false,
} }

View File

@ -3,7 +3,9 @@ import {
Store, Store,
} from 'redux' } from 'redux'
export interface UserAttributes {} export interface UserAttributes {
[key: string]: string | number | null
}
export interface User { export interface User {
readonly isLoggedIn: boolean readonly isLoggedIn: boolean