diff --git a/packages/play-node-go/play-node-go/src/App.js b/packages/play-node-go/play-node-go/src/App.js index df061eb..6543cd7 100644 --- a/packages/play-node-go/play-node-go/src/App.js +++ b/packages/play-node-go/play-node-go/src/App.js @@ -25,23 +25,35 @@ function App() { initState ); - useEffect(() => { + const fetchIndexAPI = () => { fetch(config.apiAddress) .then(res => res.text()) .catch(err => setError([...error, err])) - .then(data => setFetchData(data)) - }) + .then(data => { + const action = { + type: 'INDEX', + message: 'INDEX_DATA', + body: data + } + dispatch(action) + }) + } - useEffect(() => { - + const socketConnect = () => { socket.emit('connect'); - socket.on('connect', data => setSocketData('socket connected')); + socket.on('connected', data => setSocketData('socket connected')); socket.on('connect_error', err => setError([...error, err])); socket.on('error', err => setError([...error, err])) - }) + } + + useEffect(() => { + fetchIndexAPI(); + socketConnect(); + }, [socketData, state.user]) return ( + {/* {fetchOnLoad()}; */}
@@ -71,7 +83,7 @@ function App() {

React Boilerplate

{fetchData ?

{fetchData}

: <>} {socketData ?

{socketData}

: <>} - {error ? error.map(err =>

{err}

): <>} + {/* {error ? error.map(err =>

{err}

): <>} */}
); diff --git a/packages/play-node-go/play-node-go/src/reducers/auth/stateReducer.auth.js b/packages/play-node-go/play-node-go/src/reducers/auth/stateReducer.auth.js index 5715a14..41e1aba 100644 --- a/packages/play-node-go/play-node-go/src/reducers/auth/stateReducer.auth.js +++ b/packages/play-node-go/play-node-go/src/reducers/auth/stateReducer.auth.js @@ -1,8 +1,6 @@ // @flow import type { state, action } from '../stateReducer'; -import authServices from '../../services/authServices'; - export const authReducer = (state: state, action: action):state => { switch (action.message) { case 'LOGIN': diff --git a/packages/play-node-go/play-node-go/src/reducers/index/stateReducer.index.js b/packages/play-node-go/play-node-go/src/reducers/index/stateReducer.index.js new file mode 100644 index 0000000..86a1566 --- /dev/null +++ b/packages/play-node-go/play-node-go/src/reducers/index/stateReducer.index.js @@ -0,0 +1,18 @@ +// @flow +import type { state, action } from '../stateReducer'; + +export const indexReducer = (state: state, action: action):state => { + switch(action.message) { + + case 'INDEX_DATA': + const parsedData = indexDataParse(action.body); + return state; + + default: + return state; + } +} + +function indexDataParse(indexData) { + console.log(indexData) +} \ No newline at end of file diff --git a/packages/play-node-go/play-node-go/src/reducers/stateReducer.js b/packages/play-node-go/play-node-go/src/reducers/stateReducer.js index 9bdd296..8ce3557 100644 --- a/packages/play-node-go/play-node-go/src/reducers/stateReducer.js +++ b/packages/play-node-go/play-node-go/src/reducers/stateReducer.js @@ -1,6 +1,7 @@ // @flow import { initState } from './init/stateReducer.init'; import { authReducer } from './auth/stateReducer.auth'; +import { indexReducer } from './index/stateReducer.index'; export type state = { user: {}, @@ -19,6 +20,9 @@ export const stateReducer = (state: state, action: action): state => { switch (action.type) { case 'INIT': return initState(); + case 'INDEX': + return indexReducer(errorStrippedState, action); + case 'AUTH': return authReducer(errorStrippedState, action); diff --git a/packages/play-node-go/server/controllers/api/apiIndex.js b/packages/play-node-go/server/controllers/api/apiIndex.js new file mode 100644 index 0000000..87a267c --- /dev/null +++ b/packages/play-node-go/server/controllers/api/apiIndex.js @@ -0,0 +1,21 @@ +const userQueries = require('../../data/queries/user'); +const verifyToken = require('../../services/verifyToken'); + +const apiIndex = async (req, res, next) => { + try { + if (req.cookies && req.cookies.token) { + const token = req.cookies.token; + const verifiedToken = verifyToken(token); + res.status(200).json(verifiedToken.user) + } + res.status(200).json() + } + + catch { + res.status(500).json(err); + } +} + +module.exports = { + apiIndex +} \ No newline at end of file diff --git a/packages/play-node-go/server/controllers/auth.js b/packages/play-node-go/server/controllers/auth.js index d0db682..e03c4b0 100644 --- a/packages/play-node-go/server/controllers/auth.js +++ b/packages/play-node-go/server/controllers/auth.js @@ -1,4 +1,3 @@ - const { validationResult } = require('express-validator'); const userQueries = require('../data/queries/user'); diff --git a/packages/play-node-go/server/data/queries/user.js b/packages/play-node-go/server/data/queries/user.js index 253de3d..cc38058 100644 --- a/packages/play-node-go/server/data/queries/user.js +++ b/packages/play-node-go/server/data/queries/user.js @@ -1,6 +1,6 @@ const knex = require('../db') -const insertUser = async (user) => { +const insertUser = async (user) => { return await knex('user') .returning(['username', 'email']) .insert(user) @@ -11,7 +11,7 @@ const insertUser = async (user) => { } const findUserByNameOrEmail = async (user) => { - if (! user.email && !user.username) return []; + if (!user.email && !user.username) return []; if (!user.email) user.email = ''; if (!user.username) user.username = ''; diff --git a/packages/play-node-go/server/routes/api.js b/packages/play-node-go/server/routes/api.js index ad2d759..db73972 100644 --- a/packages/play-node-go/server/routes/api.js +++ b/packages/play-node-go/server/routes/api.js @@ -1,6 +1,7 @@ const express = require('express'); const router = express.Router(); +const apiIndexController = require('../controllers/api/apiIndex'); -// router.get('/', ); +router.get('/', apiIndexController.apiIndex); module.exports = router; diff --git a/packages/play-node-go/server/test/api.index.spec.js b/packages/play-node-go/server/test/api.index.spec.js new file mode 100644 index 0000000..dfe044b --- /dev/null +++ b/packages/play-node-go/server/test/api.index.spec.js @@ -0,0 +1,41 @@ +const apiIndexSpec = (chai, knex, server) => { + const newUserFormData = { + 'username':'newUser', + 'password':'password', + 'confirmPassword':'password', + 'email':'user@example.com' + } + + it('home should return 200 status', done => { + chai.request(server) + .get('/api/v1') + .end((err,res)=> { + if(err) done(err); + res.should.status(200); + done(); + }); + }); + + it('home should return user object if req contains verified JWT', done => { + const agent = chai.request.agent(server); + agent + .post('/auth/signup') + .type('form') + .send(newUserFormData) + .end((err, res) => { + if (err) done(err); + agent + .get('/api/v1') + .end((err,res)=> { + if(err) done(err); + res.should.have.property('body').property('username').equal('newUser'); + res.should.have.property('body').property('email').equal('user@example.com'); + res.should.status(200); + done(); + }); + }); + }) + +} + +module.exports = apiIndexSpec; \ No newline at end of file diff --git a/packages/play-node-go/server/test/spec.js b/packages/play-node-go/server/test/spec.js index 2f0ec61..d218a69 100644 --- a/packages/play-node-go/server/test/spec.js +++ b/packages/play-node-go/server/test/spec.js @@ -10,6 +10,7 @@ const should = chai.should(); const authSignupSpec = require('./auth.signup.spec'); const authLoginSpec = require('./auth.login.spec'); +const apiIndexSpec = require('./api.index.spec'); chai.use(chaiHttp); // ! to run tests from other testing modules @@ -37,14 +38,6 @@ describe('Auth Routes', function() { describe('API Routes', function() { setupDb(); - it('home should return 200 status', done => { - chai.request(server) - .get('/') - .end((err,res)=> { - if(err) done(err); - res.should.status(200); - done(); - }); - }); + apiIndexSpec(chai, knex, server) });