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

81 lines
1.6 KiB
JavaScript
Raw Normal View History

2018-07-05 16:48:56 +02:00
//@flow
import { apiClient, NOT_AUTHENTICATED_ERROR } from "../apiclient";
2018-07-11 21:01:29 +02:00
import { fetchMe } from "./me";
2018-07-10 16:52:23 +02:00
const LOGIN_URL = "/auth/access_token";
2018-07-09 11:38:13 +02:00
export const LOGIN_REQUEST = "scm/auth/login_request";
export const LOGIN_SUCCESSFUL = "scm/auth/login_successful";
export const LOGIN_FAILED = "scm/auth/login_failed";
2018-07-11 21:01:29 +02:00
export function login(username: string, password: string) {
const login_data = {
cookie: true,
grant_type: "password",
username,
password
2018-07-09 11:38:13 +02:00
};
return function(dispatch: any => void) {
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
// not the best way or?
dispatch(fetchMe());
dispatch(loginSuccessful());
})
2018-07-11 21:01:29 +02:00
.catch(err => {
dispatch(loginFailed(err));
2018-07-09 11:38:13 +02:00
});
};
}
2018-07-05 16:48:56 +02:00
export function loginRequest() {
return {
type: LOGIN_REQUEST
};
}
2018-07-11 21:01:29 +02:00
export function loginSuccessful() {
return {
type: LOGIN_SUCCESSFUL
2018-07-05 16:48:56 +02:00
};
}
2018-07-11 21:01:29 +02:00
export function loginFailed(error: Error) {
2018-07-05 16:48:56 +02:00
return {
2018-07-11 21:01:29 +02:00
type: LOGIN_FAILED,
payload: error
2018-07-05 16:48:56 +02:00
};
}
2018-07-11 21:01:29 +02:00
export default function reducer(state: any = {}, action: any = {}) {
2018-07-05 16:48:56 +02:00
switch (action.type) {
2018-07-11 21:01:29 +02:00
case LOGIN_REQUEST:
2018-07-05 16:48:56 +02:00
return {
...state,
loading: true,
2018-07-05 16:48:56 +02:00
login: false,
error: null
};
case LOGIN_SUCCESSFUL:
return {
...state,
loading: false,
2018-07-05 16:48:56 +02:00
login: true,
error: null
};
case LOGIN_FAILED:
return {
...state,
loading: false,
2018-07-05 16:48:56 +02:00
login: false,
error: action.payload
};
default:
return state;
}
}