2018-07-13 10:57:11 +02:00
|
|
|
// @flow
|
2018-07-25 08:25:07 +02:00
|
|
|
import type { Me } from "../types/Me";
|
2018-07-05 16:48:56 +02:00
|
|
|
|
2018-07-13 10:57:11 +02:00
|
|
|
import { apiClient, UNAUTHORIZED_ERROR } from "../apiclient";
|
2018-07-10 16:52:23 +02:00
|
|
|
|
2018-07-13 10:57:11 +02:00
|
|
|
// Action
|
|
|
|
|
|
|
|
|
|
export const LOGIN_REQUEST = "scm/auth/LOGIN_REQUEST";
|
|
|
|
|
export const LOGIN_SUCCESS = "scm/auth/LOGIN_SUCCESS";
|
|
|
|
|
export const LOGIN_FAILURE = "scm/auth/LOGIN_FAILURE";
|
|
|
|
|
|
|
|
|
|
export const FETCH_ME_REQUEST = "scm/auth/FETCH_ME_REQUEST";
|
|
|
|
|
export const FETCH_ME_SUCCESS = "scm/auth/FETCH_ME_SUCCESS";
|
|
|
|
|
export const FETCH_ME_FAILURE = "scm/auth/FETCH_ME_FAILURE";
|
|
|
|
|
export const FETCH_ME_UNAUTHORIZED = "scm/auth/FETCH_ME_UNAUTHORIZED";
|
|
|
|
|
|
|
|
|
|
export const LOGOUT_REQUEST = "scm/auth/LOGOUT_REQUEST";
|
|
|
|
|
export const LOGOUT_SUCCESS = "scm/auth/LOGOUT_SUCCESS";
|
|
|
|
|
export const LOGOUT_FAILURE = "scm/auth/LOGOUT_FAILURE";
|
|
|
|
|
|
|
|
|
|
// Reducer
|
|
|
|
|
|
|
|
|
|
const initialState = {
|
|
|
|
|
me: { loading: true }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default function reducer(state: any = initialState, action: any = {}) {
|
|
|
|
|
switch (action.type) {
|
|
|
|
|
case LOGIN_REQUEST:
|
|
|
|
|
return {
|
|
|
|
|
...state,
|
|
|
|
|
login: {
|
|
|
|
|
loading: true
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
case LOGIN_SUCCESS:
|
|
|
|
|
return {
|
|
|
|
|
...state,
|
|
|
|
|
login: {
|
|
|
|
|
authenticated: true
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
case LOGIN_FAILURE:
|
|
|
|
|
return {
|
|
|
|
|
...state,
|
|
|
|
|
login: {
|
|
|
|
|
error: action.payload
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
case FETCH_ME_REQUEST:
|
|
|
|
|
return {
|
|
|
|
|
...state,
|
|
|
|
|
me: {
|
|
|
|
|
loading: true
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
case FETCH_ME_SUCCESS:
|
|
|
|
|
return {
|
|
|
|
|
...state,
|
|
|
|
|
me: {
|
|
|
|
|
entry: action.payload
|
|
|
|
|
},
|
|
|
|
|
login: {
|
|
|
|
|
authenticated: true
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
case FETCH_ME_UNAUTHORIZED:
|
|
|
|
|
return {
|
|
|
|
|
...state,
|
|
|
|
|
me: {},
|
|
|
|
|
login: {
|
|
|
|
|
authenticated: false
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
case FETCH_ME_FAILURE:
|
|
|
|
|
return {
|
|
|
|
|
...state,
|
|
|
|
|
me: {
|
|
|
|
|
error: action.payload
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
case LOGOUT_REQUEST:
|
|
|
|
|
return {
|
|
|
|
|
...state,
|
|
|
|
|
logout: {
|
|
|
|
|
loading: true
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
case LOGOUT_SUCCESS:
|
|
|
|
|
return initialState;
|
|
|
|
|
case LOGOUT_FAILURE:
|
|
|
|
|
return {
|
|
|
|
|
...state,
|
|
|
|
|
logout: {
|
|
|
|
|
error: action.payload
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
default:
|
|
|
|
|
return state;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Action Creators
|
|
|
|
|
|
|
|
|
|
export const loginRequest = () => {
|
|
|
|
|
return {
|
|
|
|
|
type: LOGIN_REQUEST
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const loginSuccess = () => {
|
|
|
|
|
return {
|
|
|
|
|
type: LOGIN_SUCCESS
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const loginFailure = (error: Error) => {
|
|
|
|
|
return {
|
|
|
|
|
type: LOGIN_FAILURE,
|
|
|
|
|
payload: error
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const logoutRequest = () => {
|
|
|
|
|
return {
|
|
|
|
|
type: LOGOUT_REQUEST
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const logoutSuccess = () => {
|
|
|
|
|
return {
|
|
|
|
|
type: LOGOUT_SUCCESS
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const logoutFailure = (error: Error) => {
|
|
|
|
|
return {
|
|
|
|
|
type: LOGOUT_FAILURE,
|
|
|
|
|
payload: error
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const fetchMeRequest = () => {
|
|
|
|
|
return {
|
|
|
|
|
type: FETCH_ME_REQUEST
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const fetchMeSuccess = (me: Me) => {
|
|
|
|
|
return {
|
|
|
|
|
type: FETCH_ME_SUCCESS,
|
|
|
|
|
payload: me
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const fetchMeUnauthenticated = () => {
|
|
|
|
|
return {
|
|
|
|
|
type: FETCH_ME_UNAUTHORIZED
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const fetchMeFailure = (error: Error) => {
|
|
|
|
|
return {
|
|
|
|
|
type: FETCH_ME_FAILURE,
|
|
|
|
|
payload: error
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// urls
|
2018-07-09 11:38:13 +02:00
|
|
|
|
2018-07-13 10:57:11 +02:00
|
|
|
const ME_URL = "/me";
|
|
|
|
|
const LOGIN_URL = "/auth/access_token";
|
2018-07-09 11:38:13 +02:00
|
|
|
|
2018-07-13 10:57:11 +02:00
|
|
|
// side effects
|
2018-07-12 11:33:41 +02:00
|
|
|
|
2018-07-13 10:57:11 +02:00
|
|
|
export const login = (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) {
|
2018-07-11 21:01:29 +02:00
|
|
|
dispatch(loginRequest());
|
2018-07-10 16:52:23 +02:00
|
|
|
return apiClient
|
2018-07-11 21:01:29 +02:00
|
|
|
.post(LOGIN_URL, login_data)
|
2018-07-09 11:38:13 +02:00
|
|
|
.then(response => {
|
2018-07-11 21:01:29 +02:00
|
|
|
dispatch(fetchMe());
|
2018-07-13 10:57:11 +02:00
|
|
|
dispatch(loginSuccess());
|
2018-07-11 12:02:53 +02:00
|
|
|
})
|
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-07-13 10:57:11 +02:00
|
|
|
export const fetchMe = () => {
|
|
|
|
|
return function(dispatch: any) {
|
|
|
|
|
dispatch(fetchMeRequest());
|
|
|
|
|
return apiClient
|
|
|
|
|
.get(ME_URL)
|
|
|
|
|
.then(response => {
|
|
|
|
|
return response.json();
|
|
|
|
|
})
|
|
|
|
|
.then(me => {
|
2018-07-24 17:05:38 +02:00
|
|
|
dispatch(
|
|
|
|
|
fetchMeSuccess({ userName: me.name, displayName: me.displayName })
|
|
|
|
|
);
|
2018-07-13 10:57:11 +02:00
|
|
|
})
|
|
|
|
|
.catch((error: Error) => {
|
|
|
|
|
if (error === UNAUTHORIZED_ERROR) {
|
|
|
|
|
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
|
|
|
|
2018-07-13 10:57:11 +02:00
|
|
|
export const logout = () => {
|
2018-07-12 11:33:41 +02:00
|
|
|
return function(dispatch: any) {
|
|
|
|
|
dispatch(logoutRequest());
|
|
|
|
|
return apiClient
|
|
|
|
|
.delete(LOGIN_URL)
|
|
|
|
|
.then(() => {
|
|
|
|
|
dispatch(logoutSuccess());
|
|
|
|
|
dispatch(fetchMe());
|
|
|
|
|
})
|
|
|
|
|
.catch(error => {
|
2018-07-13 10:57:11 +02:00
|
|
|
dispatch(logoutFailure(error));
|
2018-07-12 11:33:41 +02:00
|
|
|
});
|
|
|
|
|
};
|
2018-07-13 10:57:11 +02:00
|
|
|
};
|
2018-07-12 11:33:41 +02:00
|
|
|
|
2018-07-13 10:57:11 +02:00
|
|
|
// selectors
|
2018-07-12 11:33:41 +02:00
|
|
|
|
2018-07-13 10:57:11 +02:00
|
|
|
export const isAuthenticated = (state: any): boolean => {
|
|
|
|
|
return state.auth && state.auth.login && state.auth.login.authenticated;
|
|
|
|
|
};
|