diff options
Diffstat (limited to 'app/client/auth')
| -rw-r--r-- | app/client/auth/auth.actions.js | 89 | ||||
| -rw-r--r-- | app/client/auth/login.component.js | 4 | ||||
| -rw-r--r-- | app/client/auth/signup.component.js | 18 |
3 files changed, 59 insertions, 52 deletions
diff --git a/app/client/auth/auth.actions.js b/app/client/auth/auth.actions.js index 5968f87..991a3f9 100644 --- a/app/client/auth/auth.actions.js +++ b/app/client/auth/auth.actions.js @@ -1,4 +1,5 @@ -import * as types from '../types'; +import types from '../types' +import { put } from '../api/crud.fetch' export const setToken = (data) => { return { type: types.auth.set_token, data } @@ -10,73 +11,81 @@ export const setCurrentUser = (data) => { return { type: types.auth.set_current_user, data } } export function logout() { - return { type: types.auth.logout_user }; + return { type: types.auth.logout_user } } export function authLoading() { - return { type: types.auth.loading }; + return { type: types.auth.loading } } export function InvalidCredentialsException(message) { - this.message = message; - this.name = 'InvalidCredentialsException'; + this.message = message + this.name = 'InvalidCredentialsException' +} + +const api = { + login: '/api/login', + logout: '/api/logout', + signup: '/api/signup', + checkin: '/api/checkin', } export function login(username, password) { return (dispatch) => { - dispatch(authLoading()); - apiClient() - .post(api.GET_TOKEN, { + dispatch(authLoading()) + fetch(api.login, put({ username, password + })) + .then(req => req.json()) + .then(data => { + dispatch(setToken(data.token)) + dispatch(checkin()) }) - .then(function (response) { - dispatch(setToken(response.data.token)); - dispatch(getCurrentUser()); - }) - .catch(function (error) { - dispatch(setError(true)); + .catch(error => { + dispatch(setError(true)) if (error.response.status === 400) { - throw new InvalidCredentialsException(error); + throw new InvalidCredentialsException(error) } - throw error; - }); - }; + throw error + }) + } } export function signup(data) { return (dispatch) => { - dispatch(authLoading()); - apiClient() - .post(api.SIGNUP, data) - .then(function (response) { - console.log(response.data); - dispatch(login(data.username, data.password)); + dispatch(authLoading()) + fetch(api.signup, put(data)) + .then(req => req.json()) + .then(data => { + console.log(data) + dispatch(login(data.username, data.password)) }) - .catch(function (error) { + .catch(error => { console.log(error) if (error.response.status === 400) { // dispatch(accountError("There was an error creating your account.")) - throw new InvalidCredentialsException(error); + throw new InvalidCredentialsException(error) } - throw error; - }); - }; + throw error + }) + } } -export function getCurrentUser() { +export function checkin() { return (dispatch) => { - dispatch(authLoading()); - apiClient() - .get(api.CURRENT_USER) - .then(function (response) { - dispatch(setCurrentUser(response.data)); + dispatch(authLoading()) + fetch(api.checkin) + .then(req => req.json()) + .then(data => { + console.log(data) + dispatch(setCurrentUser(data)) console.log('set current user') }) - .catch(function (error) { + .catch(error => { if (error.response.status === 400) { - throw new InvalidCredentialsException(error); + throw new InvalidCredentialsException(error) } - throw error; - }); - }; + throw error + }) + } } diff --git a/app/client/auth/login.component.js b/app/client/auth/login.component.js index 4ffab34..58c3eaf 100644 --- a/app/client/auth/login.component.js +++ b/app/client/auth/login.component.js @@ -46,14 +46,14 @@ class Login extends Component { name="username" type="text" value={this.state.username} - onChange={this.handleChange} + onInput={this.handleChange} /> <TextInput title="Password" name="password" type="password" value={this.state.password} - onChange={this.handleChange} + onInput={this.handleChange} /> <Button loading={this.props.auth.loading} diff --git a/app/client/auth/signup.component.js b/app/client/auth/signup.component.js index c86d31b..e54084b 100644 --- a/app/client/auth/signup.component.js +++ b/app/client/auth/signup.component.js @@ -2,7 +2,7 @@ import { h, Component } from 'preact'; import { bindActionCreators } from 'redux'; import { connect } from 'react-redux'; import { Redirect } from 'react-router-dom'; -import actions from './auth.actions'; +import * as actions from './auth.actions'; import { Group, Param, TextInput, Button } from '../common'; @@ -17,12 +17,9 @@ class Signup extends Component { this.handleChange = this.handleChange.bind(this) this.handleSubmit = this.handleSubmit.bind(this) } - handleChange(e) { - const name = e.target.name - const value = e.target.value + handleChange(value, name) { this.setState({ [name]: value, - error: null, }) } validate(){ @@ -36,7 +33,8 @@ class Signup extends Component { if (!this.validate) { return this.props.actions.setError('bad password') } - this.props.actions.signup(this.state) + let { ...user } = this.state + this.props.actions.signup(user) } render(){ if (this.props.auth.isAuthenticated) { @@ -54,26 +52,26 @@ class Signup extends Component { name="username" type="text" value={this.state.username} - onChange={this.handleChange} + onInput={this.handleChange} /> <TextInput title="Password" name="password" type="password" value={this.state.password} - onChange={this.handleChange} + onInput={this.handleChange} /> <TextInput title="Password again :)" name="password2" type="password" value={this.state.password2} - onChange={this.handleChange} + onInput={this.handleChange} /> <Button loading={this.props.auth.loading} > - Login + Sign up </Button> {this.renderAuthError()} </Group> |
