Skip to content

Commit

Permalink
Implemented forgetting user.
Browse files Browse the repository at this point in the history
  • Loading branch information
kallaspriit committed Aug 11, 2016
1 parent 8653089 commit a73f793
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 11 deletions.
5 changes: 5 additions & 0 deletions actions/platform-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import platformApi from '../apis/platform-api';
import {
SET_CREDENTIALS,
AUTHENTICATE,
LOGOUT,
GET_DEVICES,
GET_DEVICE,
GET_REALTIME_UPDATES,
Expand Down Expand Up @@ -43,6 +44,10 @@ export const authenticate = createAction(
platformApi.authenticate
);

export const logout = createAction(
LOGOUT
);

export const getRealtimeUpdates = (channel) => (dispatch) => {
dispatch(
createAction(GET_REALTIME_UPDATES)({
Expand Down
1 change: 1 addition & 0 deletions config/constants.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// platform
export const SET_CREDENTIALS = 'SET_CREDENTIALS';
export const AUTHENTICATE = 'AUTHENTICATE';
export const LOGOUT = 'LOGOUT';
export const GET_DEVICES = 'GET_DEVICES';
export const GET_DEVICE = 'GET_DEVICE';
export const GET_DEVICE_LATEST_MEASUREMENTS = 'GET_DEVICE_LATEST_MEASUREMENTS';
Expand Down
6 changes: 5 additions & 1 deletion gfx/css/views/authentication-view.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
.authentication-view {
.login-button {
margin-top: 20px;
margin-top: 40px;
}
.forget-button {
margin-top: 10px !important;
min-width: 100% !important;
}
}
23 changes: 22 additions & 1 deletion reducers/authentication-reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import { handleActions } from 'redux-actions';
import store from 'store';
import keyMirror from 'keymirror';
import { getDefaultAsyncState } from '../libs/redux-promise-loading-middleware';
import { SET_CREDENTIALS, AUTHENTICATE } from '../config/constants';
import { SET_CREDENTIALS, AUTHENTICATE, LOGOUT } from '../config/constants';

const StoreKey = keyMirror({
AUTHENTICATION_TENANT: null,
AUTHENTICATION_USERNAME: null,
AUTHENTICATION_PASSWORD: null,
AUTHENTICATION_REMEMBERED: null,
});

const defaultState = {
Expand All @@ -16,6 +17,7 @@ const defaultState = {
tenant: store.get(StoreKey.AUTHENTICATION_TENANT, ''),
username: store.get(StoreKey.AUTHENTICATION_USERNAME, ''),
password: store.get(StoreKey.AUTHENTICATION_PASSWORD, ''),
isRemembered: store.get(StoreKey.AUTHENTICATION_REMEMBERED, false),
isLoggedIn: false,
isInvalidCredentials: false,
},
Expand All @@ -34,6 +36,7 @@ export default handleActions({
store.set(StoreKey.AUTHENTICATION_TENANT, state.info.tenant);
store.set(StoreKey.AUTHENTICATION_USERNAME, state.info.username);
store.set(StoreKey.AUTHENTICATION_PASSWORD, state.info.password);
store.set(StoreKey.AUTHENTICATION_REMEMBERED, true);
}

return {
Expand All @@ -46,4 +49,22 @@ export default handleActions({
},
};
},
[LOGOUT]: (state, action) => {
store.remove(StoreKey.AUTHENTICATION_TENANT);
store.remove(StoreKey.AUTHENTICATION_USERNAME);
store.remove(StoreKey.AUTHENTICATION_PASSWORD);
store.remove(StoreKey.AUTHENTICATION_REMEMBERED);

return {
...state,
info: {
tenant: '',
username: '',
password: '',
isRemembered: false,
isLoggedIn: false,
isInvalidCredentials: false,
},
};
},
}, defaultState);
34 changes: 25 additions & 9 deletions views/AuthenticationView.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import CardTitle from 'material-ui/Card/CardTitle';
import CardMedia from 'material-ui/Card/CardMedia';
import CardText from 'material-ui/Card/CardText';
import TextField from 'material-ui/TextField';
import MenuItem from 'material-ui/MenuItem';
import FlatButton from 'material-ui/FlatButton';
import RaisedButton from 'material-ui/RaisedButton';
import LinearProgress from 'material-ui/LinearProgress';

Expand All @@ -23,6 +23,7 @@ class AuthenticationView extends Component {

setCredentials: PropTypes.func.isRequired,
authenticate: PropTypes.func.isRequired,
logout: PropTypes.func.isRequired,
};

constructor(props) {
Expand Down Expand Up @@ -50,12 +51,12 @@ class AuthenticationView extends Component {
const isLoggedIn = authentication.info.isLoggedIn;
const isFormDisabled = isLoading || isLoggedIn;
const loaderStyle = {
visibility: isLoading ? 'visible' : 'hidden',
visibility: isLoading || isLoggedIn ? 'visible' : 'hidden',
};

return (
<div className="authentication-view">
<HeaderComponent title="Authentication" menus={this.renderHeaderMenus()} />
<HeaderComponent title="Authentication" />
<Card className="main-contents">
<CardMedia
overlay={
Expand Down Expand Up @@ -106,6 +107,7 @@ class AuthenticationView extends Component {
disabled={isFormDisabled}
onTouchTap={() => this.handleLogin()}
/>
{this.renderForgetButton()}
</CardText>
</Card>
</div>
Expand Down Expand Up @@ -136,10 +138,18 @@ class AuthenticationView extends Component {
return <MessageComponent {...messageProps} />;
}

renderHeaderMenus() {
return [
<MenuItem key={1} onTouchTap={() => this.handleLogout()}>Logout</MenuItem>,
];
renderForgetButton() {
if (!this.props.authentication.info.isRemembered) {
return null;
}

return (
<FlatButton
label="Forget user"
className="forget-button"
onTouchTap={() => this.handleLogout()}
/>
);
}

handleLogin() {
Expand All @@ -153,7 +163,13 @@ class AuthenticationView extends Component {
}

handleLogout() {
console.log('logout');
this.props.logout();

this.setState({
tenant: '',
username: '',
password: '',
});
}

handleTextFieldChange(event) {
Expand All @@ -168,7 +184,7 @@ class AuthenticationView extends Component {

setTimeout(() => {
browserHistory.replace('/devices');
}, 2000);
}, 1000);
}
}
}
Expand Down

0 comments on commit a73f793

Please sign in to comment.