mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-10 07:25:44 +01:00
Bootstrapped login in UI
This commit is contained in:
71
scm-ui/src/modules/login.js
Normal file
71
scm-ui/src/modules/login.js
Normal file
@@ -0,0 +1,71 @@
|
||||
//@flow
|
||||
|
||||
const LOGIN = "scm/auth/login";
|
||||
const LOGIN_REQUEST = "scm/auth/login_request";
|
||||
const LOGIN_SUCCESSFUL = "scm/auth/login_successful";
|
||||
const LOGIN_FAILED = "scm/auth/login_failed";
|
||||
|
||||
export function loginRequest() {
|
||||
return {
|
||||
type: LOGIN_REQUEST
|
||||
};
|
||||
}
|
||||
|
||||
export function login(username: string, password: string) {
|
||||
var login_data = {
|
||||
cookie: true,
|
||||
grant_type: "password",
|
||||
password: username,
|
||||
username: password
|
||||
};
|
||||
console.log(login_data);
|
||||
return function(dispatch) {
|
||||
dispatch(loginRequest());
|
||||
return fetch("/api/rest/v2/auth/access_token", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json; charset=utf-8"
|
||||
},
|
||||
body: JSON.stringify(login_data)
|
||||
}).then(
|
||||
response => {
|
||||
if (response.ok) {
|
||||
dispatch(loginSuccessful());
|
||||
}
|
||||
},
|
||||
error => console.log("error logging in: " + error)
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
export function loginSuccessful() {
|
||||
return {
|
||||
type: LOGIN_SUCCESSFUL
|
||||
};
|
||||
}
|
||||
|
||||
export default function reducer(state = {}, action = {}) {
|
||||
switch (action.type) {
|
||||
case LOGIN:
|
||||
return {
|
||||
...state,
|
||||
login: false,
|
||||
error: null
|
||||
};
|
||||
case LOGIN_SUCCESSFUL:
|
||||
return {
|
||||
...state,
|
||||
login: true,
|
||||
error: null
|
||||
};
|
||||
case LOGIN_FAILED:
|
||||
return {
|
||||
...state,
|
||||
login: false,
|
||||
error: action.payload
|
||||
};
|
||||
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user