Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

123 link auth to form #167

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
5 changes: 4 additions & 1 deletion client/actions/auth.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {setToken} from '../utils/tokens'
import {register as registerApi, signin as signinApi} from '../api/auth'
import {registerApi} from '../api/auth'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

register is a better name. Can you rename the function in the ../api/auth file?

import {signin as signinApi} from '../api/auth'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

signin is a better name. Can you rename the function in the ../api/auth file?


Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need 2 consecutive blank lines? Your linter should complain about this.


export const signinPending = _ => {
return {
Expand Down Expand Up @@ -50,6 +52,7 @@ export const signin = (user) => dispatch => {
}

export const register = (user) => dispatch => {
console.log(user)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove this console.log

dispatch(registerPending())
return registerApi(user)
.then(res => {
Expand Down
8 changes: 5 additions & 3 deletions client/api/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import request from 'superagent'

const url = 'http://localhost:3000/api/v1/auth'

export function register (user) {
export function registerApi (user) {
console.log('hello')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove console.log

return request
.post(`${url}/register`)
.send(user)
Expand All @@ -12,5 +13,6 @@ export function register (user) {
export const signin = (user) => {
return request
.post(`${url}/signin`)
.send({ user })
.then(res => res.body)
.send(user)
.then(res => res.body)
}
3 changes: 1 addition & 2 deletions client/components/Dashboard.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import React, { Component } from 'react'
import ProgressModule from './ProgressModule'
import DropdownModule from './DropdownModule'
Expand Down Expand Up @@ -50,4 +49,4 @@ export default class Dashboard extends Component {
</React.Fragment>
)
}
}
}
108 changes: 68 additions & 40 deletions client/components/Register.jsx
Original file line number Diff line number Diff line change
@@ -1,46 +1,55 @@
import React from 'react'
import {connect} from 'react-redux'

class Register extends React.Component {
constructor (props) {
super(props)
this.state = {
import {register, registerError} from '../actions/auth'

export class Register extends React.Component {
state = {
name: '',
surname: '',
hash: '',
role: '',
cohort: ''
password: ''
// role: '',
// cohort: ''

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove these properties completely instead of commenting them out.

}

this.handleChange = this.handleChange.bind(this)
this.handleSubmit = this.handleSubmit.bind(this)
}
registering = () => {
const { name, surname, password } = this.state

handleSubmit (evt) {
this.props.saveItem(this.state)
this.setState(this.state)
evt.preventDefault()
}
if(!name) {
return this.props.dispatch(registerFailure('Must provide name.'))
}
if(!surname) {
return this.props.dispatch(registerFailure('Must provide surname.'))
}
if(!password) {
return this.props.dispatch(registerFailure('Must provide email.'))
}

this.props.dispatch(register({name, surname, password}))

handleChange (evt) {
this.setState({
[evt.target.name]: evt.target.value
name: '',
surname: '',
password: ''
})
}

resetForm (evt) {
handleChange = (event) => {
this.setState({
item: this.state,
invalid: {}
[event.target.name]: event.target.value
})
evt && evt.preventDefault()
}

render () {
console.log(this.state)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove console.log

return (
<form>
<label htmlFor='name'>First name</label>
<br/>
<input type='text' placeholder='First name' name='name'
<input
type='text'
placeholder='First name'
name='name'
className='u-full-width'
value={this.state.name}
onChange={this.handleChange}
Expand All @@ -49,27 +58,55 @@ class Register extends React.Component {
<br/>
<label htmlFor='surname'>Surname</label>
<br/>
<input type='text' placeholder='Last name' name='surname'
<input
type='text'
placeholder='Last name'
name='surname'
className='u-full-width'
value={this.state.name}
value={this.state.surname}
onChange={this.handleChange}
/>
<br/>
<br/>
<label htmlFor='hash'>Hash</label>
<br/>
<input type='password' placeholder='Password' name='password'
<input
type='password'
placeholder='Password'
name='password'
className='u-full-width'
value={this.state.description}
value={this.state.password}
onChange={this.handleChange}
/>
<br/>
<br/>
<label htmlFor='role'>Role</label>
<br/>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the <br /> tags and use CSS to create the space you need. This should be done for this whole component. Using elements and attributes for styling is an anti-pattern. CSS is more than capable these days 😉

<button
type='submit'
className='button-primary'
value='Add'
onClick={this.registering}
>
Register</button>
</form>
)
}
}

const mapStateToProps = ({auth}) => {
return {
auth
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to refactor lines 96-100, you can do it with one line: const mapStateToProps = ({auth}) => ({auth}) 😸


export default connect(mapStateToProps)(Register)


{/* <label htmlFor='role'>Role</label>
<br/>
<select placeholder='Are you a student or a teacher?' name='role'
className='u-full-width'
value={this.state.description}
value={this.state.role}
onChange={this.handleChange}
>
<option value="student">Student</option>
Expand All @@ -78,19 +115,10 @@ class Register extends React.Component {
<br/>
<br/>
{/* cohort will be a drop down box */}
<label htmlFor='cohort'>Cohort</label>
{/* <label htmlFor='cohort'>Cohort</label>
<br/>
<input type='text' name='cohort' placeholder='Your cohort'
className='u-full-width'
value={this.state.description}
value={this.state.cohor}
onChange={this.handleChange}
/>
<br/>
<button type='submit' className='button-primary' value='Add' onSubmit={this.handleSubmit}
onClick={this.resetForm}>Register</button>
</form>
)
}
}

export default Register
/> */}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove all of these comments

1 change: 1 addition & 0 deletions client/reducers/auth.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {getToken} from '../utils/tokens'


Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please undo this change.

const initialState = {
error: null,
loggedIn: !!getToken(),
Expand Down
1 change: 1 addition & 0 deletions server/routes/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ router.post('/register', register, token.issue)
router.post('/login', validateLogin, checkUser, token.issue)

function register (req, res, next) {
console.log(req.body)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please undo this change.

db.registerUser(req.body)
.then(([id]) => {
res.locals.userId = id
Expand Down