Skip to content
This repository has been archived by the owner on May 28, 2018. It is now read-only.

Commit

Permalink
Call api from login form
Browse files Browse the repository at this point in the history
  • Loading branch information
nbrohee committed Nov 16, 2016
1 parent b40114d commit 69496a3
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 2 deletions.
1 change: 1 addition & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"istanbul": "0.4.4",
"jsdom": "9.4.1",
"mocha": "3.0.2",
"moxios": "0.3.0",
"nock": "8.0.0",
"node-sass": "3.8.0",
"npm-run-all": "2.3.0",
Expand Down
2 changes: 2 additions & 0 deletions client/src/actions/actionTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ export const LOAD_PROFILE_SUCCESS = "UPDATE_PROFILE_SUCCESS"
export const UPDATE_PROFILE = "UPDATE_PROFILE"

export const UPDATE_ERRORS = "UPDATE_ERRORS"

export const LOGIN_USER_SUCCESS = "LOGIN_USER_SUCCESS"
14 changes: 14 additions & 0 deletions client/src/actions/userActions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as types from './actionTypes'
import userApi from '../api/userApi'

export function loginUserSuccess(user) {
return { type: types.LOGIN_USER_SUCCESS, user }
}

export function loginUser(user) {
return (dispatch) => {
return userApi.loginUser(user).then((token) => {
dispatch(loginUserSuccess({username: user.email, token, isAuthenticated: true}))
})
}
}
66 changes: 66 additions & 0 deletions client/src/actions/userActions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import expect from 'expect'
import * as userActions from './userActions'
import * as types from './actionTypes'

import thunk from 'redux-thunk'
import configureMockStore from 'redux-mock-store'
import moxios from 'moxios'

describe('User Actions', function () {
describe('loginUserSuccess', () => {
it('should create a LOGIN_USER_SUCCESS action', () => {
const user = { someUser: 'test' }
const expectedAction = {
type: types.LOGIN_USER_SUCCESS,
user
}

const action = userActions.loginUserSuccess(user)

expect(action).toEqual(expectedAction)
})
})

const middleware = [thunk]
const mockStore = configureMockStore(middleware)

describe('Async Actions', () => {
beforeEach(function () {
moxios.install()
})

afterEach(function () {
moxios.uninstall()
})

describe('loginUser', () => {
it('should create an LOGIN_USER_SUCCESS action', () => {
const expectedToken = 'expectedValidToken'

moxios.wait(() => {
let request = moxios.requests.mostRecent()
request.respondWith({
status: 200,
response: { token: expectedToken }
})
})

const expectedUser = {
username: 'test',
token: expectedToken,
isAuthenticated: true
}
const expecedValidPassword = 'test'
const expectedActions = [
{ type: types.LOGIN_USER_SUCCESS, user: expectedUser }
]
const store = mockStore({})
return store.dispatch(userActions.loginUser({ email: expectedUser.username, password: expecedValidPassword })).then(() => {
const actions = store.getActions()
expect(actions[0].type).toEqual(types.LOGIN_USER_SUCCESS)
expect(actions[0].user).toEqual(expectedUser)
})
})
})
})
})
18 changes: 18 additions & 0 deletions client/src/api/userApi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import axios from 'axios'
// eslint-disable-next-line no-undef
axios.defaults.baseURL = API_URI

class userApi {
static loginUser(user) {
return axios.post('/auth', user)
.then((response) => {
return response.data.token
})
.catch((error) => {
const errorMessage = error.response.data.message ? error.response.data.message : error
throw new Error(errorMessage)
})
}
}

export default userApi
24 changes: 22 additions & 2 deletions client/src/components/login/LoginPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import Validation from '../common/Validation'
import {userValidationConstraints} from './userValidationConstraints'
import toastr from 'toastr'
import {isEmptyObject} from '../common/validationHelper'
import {connect} from 'react-redux'
import {bindActionCreators} from 'redux'
import * as userActions from '../../actions/userActions'

export class LoginPage extends React.Component {
constructor(props, context) {
Expand Down Expand Up @@ -36,7 +39,13 @@ export class LoginPage extends React.Component {
toastr.error('Tu dois renseigner un email et un mot de passe valides')
}
else {
//TODO perform authent
this.props.actions.loginUser(this.state.user)
.then((user) => {
toastr.success('Authentification réussie')
})
.catch((err) => {
toastr.error(err)
})
}
}

Expand Down Expand Up @@ -65,7 +74,18 @@ export class LoginPage extends React.Component {
}
}

function mapStateToProps(state, ownProps) {
return {}
}

function mapDispatchToProps(dispatch) {
return {
actions: bindActionCreators(userActions, dispatch),
}
}

LoginPage.propTypes = {
actions: PropTypes.object.isRequired
}

export default LoginPage
export default connect(mapStateToProps, mapDispatchToProps)(LoginPage)
1 change: 1 addition & 0 deletions client/src/components/login/LoginPage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { LoginPage } from './LoginPage'

function setup() {
const props = {
actions: {}
}
return mount(<LoginPage {...props} />)
}
Expand Down
2 changes: 2 additions & 0 deletions client/src/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import pepite from './pepiteReducer'
import errors from './errorsReducer'
import career from './careerReducer'
import profile from './profileReducer'
import user from './userReducer'

const rootReducer = combineReducers({
application,
Expand All @@ -14,6 +15,7 @@ const rootReducer = combineReducers({
pepite,
career,
profile,
user,
errors
})

Expand Down
3 changes: 3 additions & 0 deletions client/src/reducers/initialState.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ export default {
linkedin: '',
viadeo: '',
},
user: {
isAuthenticated: false
},
errors: {
contact: {},
bac: {},
Expand Down
11 changes: 11 additions & 0 deletions client/src/reducers/userReducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as types from '../actions/actionTypes'
import initialState from './initialState'

export default function userReducer(state = initialState.user, action) {
switch (action.type) {
case types.LOGIN_USER_SUCCESS:
return Object.assign({}, state.user, action.user )
default:
return state
}
}

0 comments on commit 69496a3

Please sign in to comment.