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

135 lines
2.9 KiB
JavaScript
Raw Normal View History

2018-07-05 16:48:56 +02:00
//@flow
import { apiClient, NOT_AUTHENTICATED_ERROR } from "../apiclient";
2018-07-10 16:52:23 +02:00
const LOGIN_URL = "/auth/access_token";
const AUTHENTICATION_INFO_URL = "/me";
2018-07-09 11:38:13 +02:00
export const LOGIN = "scm/auth/login";
export const LOGIN_REQUEST = "scm/auth/login_request";
export const LOGIN_SUCCESSFUL = "scm/auth/login_successful";
export const LOGIN_FAILED = "scm/auth/login_failed";
export const GET_IS_AUTHENTICATED_REQUEST = "scm/auth/is_authenticated_request";
export const GET_IS_AUTHENTICATED = "scm/auth/get_is_authenticated";
export const IS_AUTHENTICATED = "scm/auth/is_authenticated";
export const IS_NOT_AUTHENTICATED = "scm/auth/is_not_authenticated";
export function getIsAuthenticatedRequest() {
return {
type: GET_IS_AUTHENTICATED_REQUEST
};
}
export function getIsAuthenticated() {
return function(dispatch: any => void) {
2018-07-09 11:38:13 +02:00
dispatch(getIsAuthenticatedRequest());
2018-07-10 16:52:23 +02:00
return apiClient
.get(AUTHENTICATION_INFO_URL)
2018-07-09 11:38:13 +02:00
.then(response => {
2018-07-10 16:52:23 +02:00
return response.json();
2018-07-09 11:38:13 +02:00
})
.then(data => {
if (data) {
dispatch(isAuthenticated(data.username));
}
})
.catch((error: Error) => {
if (error === NOT_AUTHENTICATED_ERROR) {
dispatch(isNotAuthenticated());
} else {
// TODO: Handle errors other than not_authenticated
}
2018-07-09 11:38:13 +02:00
});
};
}
export function isAuthenticated(username: string) {
return {
type: IS_AUTHENTICATED,
username
};
}
export function isNotAuthenticated() {
return {
type: IS_NOT_AUTHENTICATED
};
}
2018-07-05 16:48:56 +02:00
export function loginRequest() {
return {
type: LOGIN_REQUEST
};
}
export function login(username: string, password: string) {
var login_data = {
cookie: true,
grant_type: "password",
2018-07-10 16:52:23 +02:00
username,
password
2018-07-05 16:48:56 +02:00
};
return function(dispatch: any => void) {
2018-07-05 16:48:56 +02:00
dispatch(loginRequest());
2018-07-10 16:52:23 +02:00
return apiClient.post(LOGIN_URL, login_data).then(response => {
if (response.ok) {
dispatch(getIsAuthenticated());
dispatch(loginSuccessful());
}
});
2018-07-05 16:48:56 +02:00
};
}
export function loginSuccessful() {
return {
type: LOGIN_SUCCESSFUL
};
}
export default function reducer(
state: any = { loading: true },
action: any = {}
) {
2018-07-05 16:48:56 +02:00
switch (action.type) {
case LOGIN:
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
};
2018-07-09 11:38:13 +02:00
case IS_AUTHENTICATED:
return {
...state,
login: true,
loading: false,
2018-07-09 11:38:13 +02:00
username: action.username
};
case IS_NOT_AUTHENTICATED:
return {
...state,
login: false,
loading: false,
2018-07-09 11:38:13 +02:00
username: null,
error: null
};
2018-07-05 16:48:56 +02:00
default:
return state;
}
}