From 387f6874e220b5549140fb31976f988381451285 Mon Sep 17 00:00:00 2001 From: Aniko Litvanyi Date: Fri, 22 Sep 2017 13:07:35 +0200 Subject: [PATCH] [KFI]fix(Login): Fix action order and subscribing in the login process --- src/Actions.ts | 15 ++++++++++++--- src/Epics.ts | 38 +++++++++++++++++++++++++++++++------- src/Reducers.ts | 6 +++++- 3 files changed, 48 insertions(+), 11 deletions(-) diff --git a/src/Actions.ts b/src/Actions.ts index 4e2626e..83d3014 100644 --- a/src/Actions.ts +++ b/src/Actions.ts @@ -633,8 +633,8 @@ export module Actions { /** * Action creator for login a user to a sensenet portal. - * @param userName {string} Login name of the user. - * @param password {string} Password of the user. + * @param {string} userName Login name of the user. + * @param {string} password Password of the user. * @returns {Object} Returns a redux action with the properties userName and password. */ export const UserLogin = (userName: string, password: string) => ({ @@ -642,12 +642,21 @@ export module Actions { userName, password }) + /** + * Action creator for handling a user login success response without a loggedin user. + * @param {boolean} response Response of the login request + * @returns {Object} Returns a redux action with the properties userName and password. + */ + export const UserLoginBuffer = (response: boolean) => ({ + type: 'USER_LOGIN_BUFFER', + response + }) /** * Action creator for the step when a User is logged in successfully. * @param response {any} JSON response of the ajax request. * @returns {Object} Returns a redux action with the user as a response. */ - export const UserLoginSuccess = (response: any) => ({ + export const UserLoginSuccess = (response: Content) => ({ type: 'USER_LOGIN_SUCCESS', response: response }) diff --git a/src/Epics.ts b/src/Epics.ts index b59beb1..e90f45b 100644 --- a/src/Epics.ts +++ b/src/Epics.ts @@ -43,11 +43,22 @@ export module Epics { export const initSensenetStoreEpic = (action$, store, dependencies?: { repository: Repository.BaseRepository }) => { return action$.ofType('INIT_SENSENET_STORE') .mergeMap(action => { + + store.dispatch(Actions.LoadRepository(dependencies.repository.Config)) + // dependencies.repository.Authentication.State.skipWhile(state => state === Authentication.LoginState.Pending) + // .first() + // .map(result => { + dependencies.repository.GetCurrentUser().subscribe(user => { - store.dispatch(Actions.UserChanged(user)) + if (user.Name === 'Visitor'){ + store.dispatch(Actions.UserLoginFailure({ message: null })) + } + else{ + store.dispatch(Actions.UserChanged(user)) + store.dispatch(Actions.UserLoginSuccess(user)) + } }) - store.dispatch(Actions.CheckLoginState()) - store.dispatch(Actions.LoadRepository(dependencies.repository.Config)) + return dependencies.repository.Load(action.path, action.options) .map((response) => { return Actions.ReceiveLoadedContent(response, action.options) @@ -82,7 +93,7 @@ export module Epics { .mergeMap(action => { return dependencies.repository.Load(action.id, action.options) .map((response) => { - store.dispatch(Actions.LoadContentActions(response, action.scenario)) + //store.dispatch(Actions.LoadContentActions(response, action.scenario)) return Actions.ReceiveLoadedContent(response, action.options) }) .catch(error => { @@ -299,7 +310,7 @@ export module Epics { .first() .map(result => { return result === Authentication.LoginState.Authenticated ? - Actions.UserLoginSuccess(result) + Actions.UserLoginBuffer(true) : Actions.UserLoginFailure({ message: null }); }) @@ -314,15 +325,28 @@ export module Epics { return action$.ofType('USER_LOGIN_REQUEST') .mergeMap(action => { return dependencies.repository.Authentication.Login(action.userName, action.password) + // .combineLatest(dependencies.repository.GetCurrentUser().skipWhile(u => u.Name === 'Visitor')) + // .skipWhile(u => u instanceof ContentTypes.User) + // .first() .map(result => { return result ? - Actions.UserLoginSuccess(result) + Actions.UserLoginBuffer(result) : Actions.UserLoginFailure({ message: 'Failed to log in.' }); }) .catch(error => Observable.of(Actions.UserLoginFailure(error))) }) } + export const userLoginBufferEpic = (action$, store, dependencies?: { repository: Repository.BaseRepository }) => { + return action$.ofType('USER_LOGIN_BUFFER') + .mergeMap(action => { + return dependencies.repository.GetCurrentUser().skipWhile(u => u.Name === 'Visitor') + .map(result => { + Actions.UserLoginSuccess(result) + }) + .catch(error => Observable.of(Actions.UserLoginFailure(error))) + }) + } /** * Epic to logout a user from a sensenet portal. It is related to three redux actions, returns ```LogoutUser``` action and sends the response to the * ```LogoutUserSuccess``` action if the ajax request ended successfully or catches the error if the request failed and sends the error message to the ```LogoutUserFailure``` action. @@ -336,7 +360,7 @@ export module Epics { }) } export const getContentActions = (action$, store, dependencies?: { repository: Repository.BaseRepository }) => { - return action$.ofType('REQUEST_CONTENT_ACTIONS') + return action$.ofType('REQUEST_CONTENT_ACTIONS') .mergeMap(action => { let c = dependencies.repository.HandleLoadedContent(action.content, ContentTypes.GenericContent); return c.Actions(action.scenario) diff --git a/src/Reducers.ts b/src/Reducers.ts index cefda67..daea077 100644 --- a/src/Reducers.ts +++ b/src/Reducers.ts @@ -194,7 +194,11 @@ export module Reducers { * @returns {Object} state. Returns the next state based on the action. */ export const entities = (state = {}, action) => { - if (action.response && (action.type !== 'USER_LOGIN_SUCCESS' && action.type !== 'LOAD_CONTENT_SUCCESS' && action.type !== 'REQUEST_CONTENT_ACTIONS_SUCCESS')) { + if (action.response && ( + action.type !== 'USER_LOGIN_SUCCESS' && + action.type !== 'USER_LOGIN_BUFFER' && + action.type !== 'LOAD_CONTENT_SUCCESS' && + action.type !== 'REQUEST_CONTENT_ACTIONS_SUCCESS')) { return (Object).assign({}, state, action.response.entities.entities); } switch (action.type) {