Files
SCM-Manager/scm-ui/src/modules/auth.js

260 lines
5.6 KiB
JavaScript
Raw Normal View History

2018-07-13 10:57:11 +02:00
// @flow
2018-11-05 16:03:54 +01:00
import type { Me } from "@scm-manager/ui-types";
import * as types from "./types";
2018-07-05 16:48:56 +02:00
2018-11-15 21:39:08 +01:00
import { apiClient, UnauthorizedError } from "@scm-manager/ui-components";
import { isPending } from "./pending";
import { getFailure } from "./failure";
import {
callFetchIndexResources,
2018-10-29 13:42:56 +01:00
fetchIndexResources,
fetchIndexResourcesPending,
fetchIndexResourcesSuccess
} from "./indexResource";
import type { History } from "history";
2018-07-10 16:52:23 +02:00
2018-07-13 10:57:11 +02:00
// Action
export const LOGIN = "scm/auth/LOGIN";
export const LOGIN_PENDING = `${LOGIN}_${types.PENDING_SUFFIX}`;
export const LOGIN_SUCCESS = `${LOGIN}_${types.SUCCESS_SUFFIX}`;
export const LOGIN_FAILURE = `${LOGIN}_${types.FAILURE_SUFFIX}`;
2018-07-13 10:57:11 +02:00
export const FETCH_ME = "scm/auth/FETCH_ME";
export const FETCH_ME_PENDING = `${FETCH_ME}_${types.PENDING_SUFFIX}`;
export const FETCH_ME_SUCCESS = `${FETCH_ME}_${types.SUCCESS_SUFFIX}`;
export const FETCH_ME_FAILURE = `${FETCH_ME}_${types.FAILURE_SUFFIX}`;
export const FETCH_ME_UNAUTHORIZED = `${FETCH_ME}_UNAUTHORIZED`;
2018-07-13 10:57:11 +02:00
export const LOGOUT = "scm/auth/LOGOUT";
export const LOGOUT_PENDING = `${LOGOUT}_${types.PENDING_SUFFIX}`;
export const LOGOUT_SUCCESS = `${LOGOUT}_${types.SUCCESS_SUFFIX}`;
export const LOGOUT_FAILURE = `${LOGOUT}_${types.FAILURE_SUFFIX}`;
2018-07-13 10:57:11 +02:00
type LogoutRedirection = {
logoutRedirect: string
};
2018-07-13 10:57:11 +02:00
// Reducer
const initialState = {};
2018-07-13 10:57:11 +02:00
export default function reducer(
state: Object = initialState,
action: Object = { type: "UNKNOWN" }
) {
2018-07-13 10:57:11 +02:00
switch (action.type) {
case LOGIN_SUCCESS:
case FETCH_ME_SUCCESS:
return {
...state,
me: action.payload,
authenticated: true
2018-07-13 10:57:11 +02:00
};
case FETCH_ME_UNAUTHORIZED:
return {
me: {},
authenticated: false
2018-07-13 10:57:11 +02:00
};
case LOGOUT_SUCCESS:
return initialState;
2018-07-13 10:57:11 +02:00
default:
return state;
}
}
// Action Creators
export const loginPending = () => {
2018-07-13 10:57:11 +02:00
return {
type: LOGIN_PENDING
2018-07-13 10:57:11 +02:00
};
};
export const loginSuccess = (me: Me) => {
2018-07-13 10:57:11 +02:00
return {
type: LOGIN_SUCCESS,
payload: me
2018-07-13 10:57:11 +02:00
};
};
export const loginFailure = (error: Error) => {
return {
type: LOGIN_FAILURE,
payload: error
};
};
export const logoutPending = () => {
2018-07-13 10:57:11 +02:00
return {
type: LOGOUT_PENDING
2018-07-13 10:57:11 +02:00
};
};
export const logoutSuccess = () => {
return {
type: LOGOUT_SUCCESS
};
};
export const logoutFailure = (error: Error) => {
return {
type: LOGOUT_FAILURE,
payload: error
};
};
export const fetchMePending = () => {
2018-07-13 10:57:11 +02:00
return {
type: FETCH_ME_PENDING
2018-07-13 10:57:11 +02:00
};
};
export const fetchMeSuccess = (me: Me) => {
return {
type: FETCH_ME_SUCCESS,
payload: me
};
};
export const fetchMeUnauthenticated = () => {
return {
type: FETCH_ME_UNAUTHORIZED,
resetPending: true
2018-07-13 10:57:11 +02:00
};
};
export const fetchMeFailure = (error: Error) => {
return {
type: FETCH_ME_FAILURE,
payload: error
};
};
// side effects
2018-10-11 09:54:12 +02:00
const callFetchMe = (link: string): Promise<Me> => {
return apiClient.get(link).then(response => {
return response.json();
});
};
2018-10-11 09:54:12 +02:00
export const login = (
loginLink: string,
username: string,
password: string
) => {
2018-07-11 21:01:29 +02:00
const login_data = {
cookie: true,
grant_type: "password",
username,
password
2018-07-09 11:38:13 +02:00
};
2018-07-13 10:57:11 +02:00
return function(dispatch: any) {
dispatch(loginPending());
2018-07-10 16:52:23 +02:00
return apiClient
2018-10-11 09:54:12 +02:00
.post(loginLink, login_data)
2018-11-15 21:39:08 +01:00
.then(() => {
2018-10-29 13:42:56 +01:00
dispatch(fetchIndexResourcesPending());
2018-10-11 10:26:54 +02:00
return callFetchIndexResources();
2018-10-11 09:54:12 +02:00
})
.then(response => {
dispatch(fetchIndexResourcesSuccess(response));
2018-10-11 09:54:12 +02:00
const meLink = response._links.me.href;
2018-10-11 10:26:54 +02:00
return callFetchMe(meLink);
})
.then(me => {
dispatch(loginSuccess(me));
})
2018-07-11 21:01:29 +02:00
.catch(err => {
2018-07-13 10:57:11 +02:00
dispatch(loginFailure(err));
2018-07-09 11:38:13 +02:00
});
};
2018-07-13 10:57:11 +02:00
};
2018-07-05 16:48:56 +02:00
2018-10-11 09:54:12 +02:00
export const fetchMe = (link: string) => {
2018-07-13 10:57:11 +02:00
return function(dispatch: any) {
dispatch(fetchMePending());
2018-10-11 09:54:12 +02:00
return callFetchMe(link)
2018-07-13 10:57:11 +02:00
.then(me => {
dispatch(fetchMeSuccess(me));
2018-07-13 10:57:11 +02:00
})
.catch((error: Error) => {
2018-11-15 21:39:08 +01:00
if (error instanceof UnauthorizedError) {
2018-07-13 10:57:11 +02:00
dispatch(fetchMeUnauthenticated());
} else {
dispatch(fetchMeFailure(error));
}
});
2018-07-05 16:48:56 +02:00
};
2018-07-13 10:57:11 +02:00
};
2018-07-05 16:48:56 +02:00
export const logout = (link: string, history: History) => {
return function(dispatch: any) {
dispatch(logoutPending());
return apiClient
2018-10-11 08:27:24 +02:00
.delete(link)
.then(response => {
return response.status === 200
? response.json()
: new Promise(function(resolve) {
resolve(undefined);
});
})
.then(json => {
if (json && json.logoutRedirect) {
2019-04-12 14:32:17 +02:00
window.location.assign(json.logoutRedirect);
}
2019-04-12 14:32:17 +02:00
dispatch(logoutSuccess());
})
.then(() => {
dispatch(fetchIndexResources());
})
.catch(error => {
2018-07-13 10:57:11 +02:00
dispatch(logoutFailure(error));
});
};
2018-07-13 10:57:11 +02:00
};
2018-07-13 10:57:11 +02:00
// selectors
const stateAuth = (state: Object): Object => {
return state.auth || {};
};
export const isAuthenticated = (state: Object) => {
if (stateAuth(state).authenticated) {
return true;
}
return false;
};
export const getMe = (state: Object): Me => {
return stateAuth(state).me;
};
export const isFetchMePending = (state: Object) => {
return isPending(state, FETCH_ME);
};
export const getFetchMeFailure = (state: Object) => {
return getFailure(state, FETCH_ME);
};
export const isLoginPending = (state: Object) => {
return isPending(state, LOGIN);
};
export const getLoginFailure = (state: Object) => {
return getFailure(state, LOGIN);
};
export const isLogoutPending = (state: Object) => {
return isPending(state, LOGOUT);
};
export const getLogoutFailure = (state: Object) => {
return getFailure(state, LOGOUT);
2018-07-13 10:57:11 +02:00
};