Fixed nasty error in Login

This commit is contained in:
Claudio Maggioni 2020-04-09 14:48:24 +02:00
parent 462cad0e53
commit bad5d04238
4 changed files with 15 additions and 11 deletions

View file

@ -73,9 +73,6 @@ class App extends Component {
} }
} }
const mapStateToProps = (state, _) => { const mapStateToProps = (state, _) => ({ loggedIn: state.login.loggedIn });
console.log("malusae react", state);
return { loggedIn: !!(state.login && state.login.loggedIn) };
};
const AppContainer = connect(mapStateToProps, RemoteService)(App); const AppContainer = connect(mapStateToProps, RemoteService)(App);
export default AppContainer; export default AppContainer;

View file

@ -25,6 +25,11 @@ export class MyHeader extends React.Component {
}; };
this.getInfo(); this.getInfo();
this.logout = this.logout.bind(this);
}
logout() {
this.props.logout().then(() => this.props.history.push("/"));
} }
getInfo() { getInfo() {
@ -55,7 +60,7 @@ export class MyHeader extends React.Component {
{this.state.username} {this.state.username}
</Label> </Label>
<Divider /> <Divider />
<Button onClick={this.props.logout}>Logout</Button> <Button onClick={this.logout}>Logout</Button>
</Grid.Column> </Grid.Column>
</Grid.Row> </Grid.Row>
</Grid> </Grid>

View file

@ -85,7 +85,6 @@ const Endpoint = {
.then((res) => { .then((res) => {
localStorage.setItem("token", res.data.jwttoken); localStorage.setItem("token", res.data.jwttoken);
localStorage.setItem("exp", new Date().getTime() + 5 * 60 * 60 * 1000); localStorage.setItem("exp", new Date().getTime() + 5 * 60 * 60 * 1000);
Endpoint.socket = new ServiceSocket(res.data.jwttoken);
return res.data.jwttoken; return res.data.jwttoken;
}); });
}, },
@ -95,6 +94,8 @@ const Endpoint = {
* @return {Promise<Undefined, _>} An always-resolved promise * @return {Promise<Undefined, _>} An always-resolved promise
*/ */
logout: () => { logout: () => {
localStorage.removeItem("token");
localStorage.removeItem("exp");
return Promise.resolve(void 0); return Promise.resolve(void 0);
}, },
@ -285,7 +286,7 @@ export const RemoteService = {
* @returns {Promise<Undefined, RemoteError>} promise that resolves to void and rejects * @returns {Promise<Undefined, RemoteError>} promise that resolves to void and rejects
* with user-fiendly errors as a RemoteError * with user-fiendly errors as a RemoteError
*/ */
updateDevice: (data) => { saveDevice: (data) => {
return (dispatch) => { return (dispatch) => {
let url = "/device"; let url = "/device";
if ((data.id && data.flowType === "OUTPUT") || !data.id) { if ((data.id && data.flowType === "OUTPUT") || !data.id) {

View file

@ -95,7 +95,7 @@ class Login extends Component {
color="blue" color="blue"
fluid fluid
size="large" size="large"
onClick={this.handleLogin.bind(this)} onClick={this.handleLogin}
> >
Login Login
</Button> </Button>
@ -116,7 +116,8 @@ class Login extends Component {
} }
const mapStateToProps = (state, _) => ({ loggedIn: state.login.loggedIn }); const mapStateToProps = (state, _) => ({ loggedIn: state.login.loggedIn });
const LoginContainer = withRouter(connect(mapStateToProps, RemoteService))( const LoginContainer = connect(
Login mapStateToProps,
); RemoteService
)(withRouter(Login));
export default LoginContainer; export default LoginContainer;