serve user from verified jwt

This commit is contained in:
Sorrel Bri 2020-01-17 16:32:40 -08:00 committed by sorrelbri
parent 97efe2314b
commit 080ab8a994
10 changed files with 110 additions and 23 deletions

View file

@ -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 (
<Router>
{/* {fetchOnLoad()}; */}
<div data-testid="App" className="App">
<Switch>
@ -71,7 +83,7 @@ function App() {
<h1>React Boilerplate</h1>
{fetchData ? <p>{fetchData}</p> : <></>}
{socketData ? <p>{socketData}</p> : <></>}
{error ? error.map(err => <p>{err}</p>): <></>}
{/* {error ? error.map(err => <p>{err}</p>): <></>} */}
</div>
</Router>
);

View file

@ -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':

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

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 userQueries = require('../data/queries/user');

View file

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

View file

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

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