-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
17c9cd5
commit 33d9a88
Showing
8 changed files
with
108 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import React from 'react' | ||
|
||
export const Login = ({ login }) => ( | ||
<form onSubmit={evt => { | ||
evt.preventDefault() | ||
login(evt.target.username.value, evt.target.password.value) | ||
} }> | ||
<input name="username" /> | ||
<input name="password" type="password" /> | ||
<input type="submit" value="Login" /> | ||
</form> | ||
) | ||
|
||
import {login} from 'APP/app/reducers/auth' | ||
import {connect} from 'react-redux' | ||
|
||
export default connect ( | ||
state => ({}), | ||
dispatch => ({ | ||
login(username, password) { | ||
return dispatch(login(username, password)) | ||
} | ||
}), | ||
) (Login) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import React from 'react' | ||
import chai, {expect} from 'chai' | ||
chai.use(require('chai-enzyme')) | ||
import {shallow} from 'enzyme' | ||
|
||
import {Login} from './Login' | ||
|
||
describe('<Login />', () => { | ||
let root | ||
beforeEach('render the root', () => | ||
root = shallow(<Login/>) | ||
) | ||
|
||
it('shows a login form', () => { | ||
expect(root.find('input[name="username"]')).to.have.length(1) | ||
expect(root.find('input[name="password"]')).to.have.length(1) | ||
}) | ||
|
||
it('shows a password field', () => { | ||
expect(root.find('input[type="password"]')).to.have.length(1) | ||
}) | ||
|
||
it('has a login button', () => { | ||
const submit = root.find('input[type="submit"]') | ||
expect(submit).to.have.length(1) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,19 @@ | ||
'use strict' | ||
import React from 'react' | ||
import {Router, Route, IndexRedirect, browserHistory} from 'react-router' | ||
import {render} from 'react-dom' | ||
import { Provider } from 'react-redux' | ||
|
||
import store from './store' | ||
import Root from './components/Root' | ||
import Login from './components/Login' | ||
|
||
render ( | ||
<Provider store={store}> | ||
<Root/> | ||
<Router history={browserHistory}> | ||
<Route path="/" component={Root} /> | ||
<Route path="/login" component={Login} /> | ||
</Router> | ||
</Provider>, | ||
document.getElementById('main') | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const reducer = (state=null, action) => { | ||
switch(action.type) { | ||
case AUTHENTICATED: | ||
return action.user | ||
} | ||
return state | ||
} | ||
|
||
const AUTHENTICATED = 'AUTHENTICATED' | ||
export const authenticated = user => ({ | ||
type: AUTHENTICATED, user | ||
}) | ||
|
||
import axios from 'axios' | ||
|
||
export const login = (username, password) => | ||
dispatch => { | ||
const body = {username, password} | ||
console.log('req body=', body) | ||
return axios.post('/api/auth/local/login', body) | ||
.then(() => dispatch(whoami())) | ||
} | ||
|
||
export const whoami = () => | ||
dispatch => | ||
axios.get('/api/auth/whoami') | ||
.then(response => { | ||
const user = response.data | ||
if (!Object.keys(user).length) { | ||
return dispatch(authenticated(null)) | ||
} | ||
dispatch(authenticated(user)) | ||
}) | ||
|
||
export default reducer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,7 @@ | ||
import { combineReducers } from 'redux' | ||
|
||
const initialState = {} | ||
|
||
const rootReducer = function(state = initialState, action) { | ||
switch(action.type) { | ||
default: return state | ||
} | ||
}; | ||
const rootReducer = combineReducers({ | ||
auth: require('./auth').default, | ||
}) | ||
|
||
export default rootReducer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters