Skip to content
This repository was archived by the owner on Jan 4, 2019. It is now read-only.

Commit

Permalink
[KFI]fix(Login): Fix action order and subscribing in the login process
Browse files Browse the repository at this point in the history
  • Loading branch information
herflis committed Sep 22, 2017
1 parent 61a38d0 commit 387f687
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
15 changes: 12 additions & 3 deletions src/Actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -633,21 +633,30 @@ 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) => ({
type: 'USER_LOGIN_REQUEST',
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
})
Expand Down
38 changes: 31 additions & 7 deletions src/Epics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -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 });
})
Expand All @@ -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.
Expand All @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion src/Reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (<any>Object).assign({}, state, action.response.entities.entities);
}
switch (action.type) {
Expand Down

0 comments on commit 387f687

Please sign in to comment.