Skip to content
This repository has been archived by the owner on Apr 15, 2019. It is now read-only.

Setup auto-login option and fix login redirect for better developer experience - Closes #633 #640

Merged
merged 5 commits into from
Aug 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ npm run dev

Open http://localhost:8080

For ease of development, you can set a cookie to prefill a passphrase, e.g.:
```
document.cookie = 'passphrase=wagon stock borrow episode laundry kitten salute link globe zero feed marble'
```

And then you can set a cookie to login automatically
```
document.cookie = 'autologin=true'
```

## Build

```
Expand Down
13 changes: 12 additions & 1 deletion src/components/login/loginForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { isValidPassphrase } from '../../utils/passphrase';
import networksRaw from './networks';
import Passphrase from '../passphrase';
import styles from './login.css';
import env from '../../constants/env';

/**
* The container component containing login
Expand Down Expand Up @@ -42,7 +43,9 @@ class LoginForm extends React.Component {

componentDidUpdate() {
if (this.props.account && this.props.account.address) {
this.props.history.replace('/main/transactions');
const search = this.props.history.location.search;
this.props.history.replace(
search.indexOf('?referrer') === 0 ? search.replace('?referrer=', '') : '/main/transactions');
if (this.state.address) {
Cookies.set('address', this.state.address);
}
Expand Down Expand Up @@ -111,6 +114,14 @@ class LoginForm extends React.Component {
...this.validators.address(address),
...this.validators.passphrase(passphrase),
});

// ignore this in coverage as it is hard to test and does not run in production
/* istanbul ignore if */
if (!env.production && Cookies.get('autologin') && !this.props.account.afterLogout && passphrase) {
setTimeout(() => {
this.onLoginSubmission(passphrase);
});
}
}

render() {
Expand Down
5 changes: 4 additions & 1 deletion src/components/login/loginForm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('LoginForm', () => {
const props = {
peers: {},
account,
history: [],
history: {},
onAccountUpdated: () => {},
setActiveDialog: spy(),
activePeerSet: (network) => {
Expand Down Expand Up @@ -73,6 +73,9 @@ describe('LoginForm', () => {
props.account = { address: 'dummy' };
props.history = {
replace: spy(),
location: {
search: '',
},
};

it('calls this.props.history.replace(\'/main/transactions\')', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/privateRoute/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { connect } from 'react-redux';

export const PrivateRouteRender = ({ render, isAuthenticated, ...rest }) => (
<Route {...rest} render={ matchProps => (
isAuthenticated ? render(matchProps) : <Redirect to='/' />
isAuthenticated ? render(matchProps) : <Redirect to={`/?referrer=${rest.history.location.pathname}`} />
)}/>
);

Expand Down
1 change: 1 addition & 0 deletions src/components/privateRoute/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe('PrivateRouteRender', () => {
<div>
<Route path='/' component={Public} />
<PrivateRouteRender
history={ { location: { pathname: '' } } }
path='/private'
render={({ match }) => <Route to={`${match.url}/test`} component={Private}/>}
isAuthenticated={isAuthenticated} />
Expand Down
4 changes: 3 additions & 1 deletion src/store/reducers/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ const account = (state = {}, action) => {
case actionTypes.accountLoggedIn:
return merge(state, action.data);
case actionTypes.accountLoggedOut:
return {};
return {
afterLogout: true,
};
default:
return state;
}
Expand Down
2 changes: 1 addition & 1 deletion src/store/reducers/account.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('Reducer: account(state, action)', () => {
type: actionTypes.accountLoggedOut,
};
const changedAccount = account(state, action);
expect(changedAccount).to.deep.equal({ });
expect(changedAccount).to.deep.equal({ afterLogout: true });
});

it('should return state if action.type is none of the above', () => {
Expand Down
1 change: 1 addition & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module.exports = (env) => {
path: path.resolve(__dirname, 'app', 'dist'),
filename: env.test ? 'bundle.js' : 'bundle.[name].js',
},
devtool: 'source-map',
devServer: {
contentBase: 'src',
inline: true,
Expand Down