From fbfebe1df7fcfdc2d13d4ecba6bd82347a0e6b43 Mon Sep 17 00:00:00 2001 From: Philipp Czora Date: Thu, 5 Jul 2018 16:48:56 +0200 Subject: [PATCH] Bootstrapped login in UI --- scm-ui/src/containers/App.js | 25 ++++++------ scm-ui/src/containers/Login.js | 70 ++++++++++++++++++++++++++------- scm-ui/src/createReduxStore.js | 9 +++-- scm-ui/src/modules/login.js | 71 ++++++++++++++++++++++++++++++++++ 4 files changed, 145 insertions(+), 30 deletions(-) create mode 100644 scm-ui/src/modules/login.js diff --git a/scm-ui/src/containers/App.js b/scm-ui/src/containers/App.js index e03221ec6a..b30ca75d0d 100644 --- a/scm-ui/src/containers/App.js +++ b/scm-ui/src/containers/App.js @@ -6,29 +6,26 @@ import { withRouter } from "react-router-dom"; type Props = { login: boolean -} +}; class App extends Component { - render() { + const { login } = this.props; - const { login} = this.props; - - if(login) { + if (!login) { return (
- + +
+ ); + } else { + return ( +
+ +
); } - else { - return ( -
- -
-
- ); - } } } diff --git a/scm-ui/src/containers/Login.js b/scm-ui/src/containers/Login.js index f85fbef9ed..fb20d48366 100644 --- a/scm-ui/src/containers/Login.js +++ b/scm-ui/src/containers/Login.js @@ -1,24 +1,35 @@ //@flow -import React from 'react'; -import injectSheet from 'react-jss'; +import React from "react"; +import injectSheet from "react-jss"; +import { login } from "../modules/login"; +import { connect } from "react-redux"; const styles = { wrapper: { - width: '100%', - display: 'flex', - height: '10em' + width: "100%", + display: "flex", + height: "10em" }, login: { - margin: 'auto', - textAlign: 'center' + margin: "auto", + textAlign: "center" } }; -type Props = { - classes: any; -} - class Login extends React.Component { + state = {}; + handleUsernameChange(event) { + this.setState({ username: event.target.value }); + } + + handlePasswordChange(event) { + this.setState({ password: event.target.value }); + } + + handleSubmit(event) { + event.preventDefault(); + this.props.login(this.state.username, this.state.password); + } render() { const { classes } = this.props; @@ -26,11 +37,44 @@ class Login extends React.Component {
You need to log in! ... +
+ + + +
); } - } -export default injectSheet(styles)(Login); +const mapStateToProps = state => { + return {}; +}; + +const mapDispatchToProps = dispatch => { + return { + login: (username: string, password: string) => + dispatch(login(username, password)) + }; +}; + +const StyledLogin = injectSheet(styles)( + connect( + mapStateToProps, + mapDispatchToProps + )(Login) +); +export default StyledLogin; +// export default connect( +// mapStateToProps, +// mapDispatchToProps +// )(StyledLogin); diff --git a/scm-ui/src/createReduxStore.js b/scm-ui/src/createReduxStore.js index 502fc55faf..df46c5be34 100644 --- a/scm-ui/src/createReduxStore.js +++ b/scm-ui/src/createReduxStore.js @@ -6,16 +6,19 @@ import { routerReducer, routerMiddleware } from "react-router-redux"; import repositories from "./repositories/modules/repositories"; import users from "./users/modules/users"; +import login from "./modules/login"; -import type {BrowserHistory} from "history/createBrowserHistory"; +import type { BrowserHistory } from "history/createBrowserHistory"; function createReduxStore(history: BrowserHistory) { - const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose; + const composeEnhancers = + window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose; const reducer = combineReducers({ router: routerReducer, repositories, - users + users, + login }); return createStore( diff --git a/scm-ui/src/modules/login.js b/scm-ui/src/modules/login.js new file mode 100644 index 0000000000..e46abc44f4 --- /dev/null +++ b/scm-ui/src/modules/login.js @@ -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; + } +}