mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-14 17:26:22 +01:00
improve authentication
This commit is contained in:
@@ -1,97 +1,58 @@
|
||||
//@flow
|
||||
|
||||
import { apiClient, NOT_AUTHENTICATED_ERROR } from "../apiclient";
|
||||
import { fetchMe } from "./me";
|
||||
|
||||
const LOGIN_URL = "/auth/access_token";
|
||||
const AUTHENTICATION_INFO_URL = "/me";
|
||||
|
||||
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 login(username: string, password: string) {
|
||||
const login_data = {
|
||||
cookie: true,
|
||||
grant_type: "password",
|
||||
username,
|
||||
password
|
||||
};
|
||||
}
|
||||
|
||||
export function getIsAuthenticated() {
|
||||
return function(dispatch: any => void) {
|
||||
dispatch(getIsAuthenticatedRequest());
|
||||
dispatch(loginRequest());
|
||||
return apiClient
|
||||
.get(AUTHENTICATION_INFO_URL)
|
||||
.post(LOGIN_URL, login_data)
|
||||
.then(response => {
|
||||
return response.json();
|
||||
// not the best way or?
|
||||
dispatch(fetchMe());
|
||||
dispatch(loginSuccessful());
|
||||
})
|
||||
.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
|
||||
}
|
||||
.catch(err => {
|
||||
dispatch(loginFailed(err));
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
export function isAuthenticated(username: string) {
|
||||
return {
|
||||
type: IS_AUTHENTICATED,
|
||||
username
|
||||
};
|
||||
}
|
||||
|
||||
export function isNotAuthenticated() {
|
||||
return {
|
||||
type: IS_NOT_AUTHENTICATED
|
||||
};
|
||||
}
|
||||
|
||||
export function loginRequest() {
|
||||
return {
|
||||
type: LOGIN_REQUEST
|
||||
};
|
||||
}
|
||||
|
||||
export function login(username: string, password: string) {
|
||||
var login_data = {
|
||||
cookie: true,
|
||||
grant_type: "password",
|
||||
username,
|
||||
password
|
||||
};
|
||||
return function(dispatch: any => void) {
|
||||
dispatch(loginRequest());
|
||||
return apiClient.post(LOGIN_URL, login_data).then(response => {
|
||||
if (response.ok) {
|
||||
dispatch(getIsAuthenticated());
|
||||
dispatch(loginSuccessful());
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
export function loginSuccessful() {
|
||||
return {
|
||||
type: LOGIN_SUCCESSFUL
|
||||
};
|
||||
}
|
||||
|
||||
export default function reducer(
|
||||
state: any = { loading: true },
|
||||
action: any = {}
|
||||
) {
|
||||
export function loginFailed(error: Error) {
|
||||
return {
|
||||
type: LOGIN_FAILED,
|
||||
payload: error
|
||||
};
|
||||
}
|
||||
|
||||
export default function reducer(state: any = {}, action: any = {}) {
|
||||
switch (action.type) {
|
||||
case LOGIN:
|
||||
case LOGIN_REQUEST:
|
||||
return {
|
||||
...state,
|
||||
loading: true,
|
||||
@@ -112,21 +73,6 @@ export default function reducer(
|
||||
login: false,
|
||||
error: action.payload
|
||||
};
|
||||
case IS_AUTHENTICATED:
|
||||
return {
|
||||
...state,
|
||||
login: true,
|
||||
loading: false,
|
||||
username: action.username
|
||||
};
|
||||
case IS_NOT_AUTHENTICATED:
|
||||
return {
|
||||
...state,
|
||||
login: false,
|
||||
loading: false,
|
||||
username: null,
|
||||
error: null
|
||||
};
|
||||
|
||||
default:
|
||||
return state;
|
||||
|
||||
Reference in New Issue
Block a user