serve user from verified jwt

This commit is contained in:
Sorrel Bri 2020-01-17 16:32:40 -08:00
parent 8f5ec4c414
commit 3ea7dafb85
10 changed files with 110 additions and 23 deletions

View file

@ -25,23 +25,35 @@ function App() {
initState initState
); );
useEffect(() => { const fetchIndexAPI = () => {
fetch(config.apiAddress) fetch(config.apiAddress)
.then(res => res.text()) .then(res => res.text())
.catch(err => setError([...error, err])) .catch(err => setError([...error, err]))
.then(data => setFetchData(data)) .then(data => {
}) const action = {
type: 'INDEX',
useEffect(() => { message: 'INDEX_DATA',
body: data
}
dispatch(action)
})
}
const socketConnect = () => {
socket.emit('connect'); 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('connect_error', err => setError([...error, err]));
socket.on('error', err => setError([...error, err])) socket.on('error', err => setError([...error, err]))
}) }
useEffect(() => {
fetchIndexAPI();
socketConnect();
}, [socketData, state.user])
return ( return (
<Router> <Router>
{/* {fetchOnLoad()}; */}
<div data-testid="App" className="App"> <div data-testid="App" className="App">
<Switch> <Switch>
@ -71,7 +83,7 @@ function App() {
<h1>React Boilerplate</h1> <h1>React Boilerplate</h1>
{fetchData ? <p>{fetchData}</p> : <></>} {fetchData ? <p>{fetchData}</p> : <></>}
{socketData ? <p>{socketData}</p> : <></>} {socketData ? <p>{socketData}</p> : <></>}
{error ? error.map(err => <p>{err}</p>): <></>} {/* {error ? error.map(err => <p>{err}</p>): <></>} */}
</div> </div>
</Router> </Router>
); );

View file

@ -1,8 +1,6 @@
// @flow // @flow
import type { state, action } from '../stateReducer'; import type { state, action } from '../stateReducer';
import authServices from '../../services/authServices';
export const authReducer = (state: state, action: action):state => { export const authReducer = (state: state, action: action):state => {
switch (action.message) { switch (action.message) {
case 'LOGIN': case 'LOGIN':

View file

@ -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)
}

View file

@ -1,6 +1,7 @@
// @flow // @flow
import { initState } from './init/stateReducer.init'; import { initState } from './init/stateReducer.init';
import { authReducer } from './auth/stateReducer.auth'; import { authReducer } from './auth/stateReducer.auth';
import { indexReducer } from './index/stateReducer.index';
export type state = { export type state = {
user: {}, user: {},
@ -19,6 +20,9 @@ export const stateReducer = (state: state, action: action): state => {
switch (action.type) { switch (action.type) {
case 'INIT': return initState(); case 'INIT': return initState();
case 'INDEX':
return indexReducer(errorStrippedState, action);
case 'AUTH': case 'AUTH':
return authReducer(errorStrippedState, action); return authReducer(errorStrippedState, action);

View file

@ -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
}

View file

@ -1,4 +1,3 @@
const { validationResult } = require('express-validator'); const { validationResult } = require('express-validator');
const userQueries = require('../data/queries/user'); const userQueries = require('../data/queries/user');

View file

@ -11,7 +11,7 @@ const insertUser = async (user) => {
} }
const findUserByNameOrEmail = 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.email) user.email = '';
if (!user.username) user.username = ''; if (!user.username) user.username = '';

View file

@ -1,6 +1,7 @@
const express = require('express'); const express = require('express');
const router = express.Router(); const router = express.Router();
const apiIndexController = require('../controllers/api/apiIndex');
// router.get('/', ); router.get('/', apiIndexController.apiIndex);
module.exports = router; module.exports = router;

View file

@ -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;

View file

@ -10,6 +10,7 @@ const should = chai.should();
const authSignupSpec = require('./auth.signup.spec'); const authSignupSpec = require('./auth.signup.spec');
const authLoginSpec = require('./auth.login.spec'); const authLoginSpec = require('./auth.login.spec');
const apiIndexSpec = require('./api.index.spec');
chai.use(chaiHttp); chai.use(chaiHttp);
// ! to run tests from other testing modules // ! to run tests from other testing modules
@ -37,14 +38,6 @@ describe('Auth Routes', function() {
describe('API Routes', function() { describe('API Routes', function() {
setupDb(); setupDb();
it('home should return 200 status', done => { apiIndexSpec(chai, knex, server)
chai.request(server)
.get('/')
.end((err,res)=> {
if(err) done(err);
res.should.status(200);
done();
});
});
}); });