From 96202ff43910d0716124eba84f36f4e3da7f9b06 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Fri, 14 Sep 2018 11:24:19 +0200 Subject: [PATCH] Refactoring Names during the check user process --- server/app.js | 48 +++++++++---------- .../middleware/{get-user.js => check-user.js} | 6 +++ 2 files changed, 30 insertions(+), 24 deletions(-) rename server/auth/middleware/{get-user.js => check-user.js} (59%) diff --git a/server/app.js b/server/app.js index aa7b6c3e3..15b52bfbe 100644 --- a/server/app.js +++ b/server/app.js @@ -1,27 +1,27 @@ /* This module imports */ -const globals = require('./globals'); -const sessions = require('./sessions'); -const express = require('express'); -const graphqlHTTP = require('express-graphql'); -const schema = require('./schema/schema'); -const mongoose = require('mongoose'); +const globals = require('./globals') +const sessions = require('./sessions') +const express = require('express') +const graphqlHTTP = require('express-graphql') +const schema = require('./schema/schema') +const mongoose = require('mongoose') -const checkJwt = require('./auth/middleware/jwt'); -const getUser = require('./auth/middleware/get-user'); +const checkJwt = require('./auth/middleware/jwt') +const checkUser = require('./auth/middleware/check-user') -const cors = require('cors'); +const cors = require('cors') -const app = express(); +const app = express() // Allow crosss-origin requests -app.use(cors()); +app.use(cors()) // Connect to the database -mongoose.connect('mongodb://users-module-graphql-server:dsadTRYUtrsgg34@ds141952.mlab.com:41952/users', { useNewUrlParser: true }); +mongoose.connect('mongodb://users-module-graphql-server:dsadTRYUtrsgg34@ds141952.mlab.com:41952/users', { useNewUrlParser: true }) mongoose.connection.once('open', () => { - console.log('Connected to database'); + console.log('Connected to database') }) /* Here we bind all requests to this endpoint to be procecced by the GraphQL Library. */ @@ -29,23 +29,23 @@ mongoose.connection.once('open', () => { app.get('/graphql', graphqlHTTP({ schema, graphiql: true -})); +})) app.post('/graphql', graphqlHTTP({ schema, graphiql: false, context: req => ({ ...req }) -})); +})) app.post('/graphql', checkJwt, (err, req, res, next) => { - console.log(req); - if (err) return res.status(401).send(`[Authenticate Token Error] ${err.message}`); - next(); - } -); + console.log(req) + if (err) return res.status(401).send(`[Authenticate Token Error] ${err.message}`) + next() +} +) -app.post('/graphql', (req, res, done) => getUser(req, res, done)); +app.post('/graphql', (req, res, done) => checkUser(req, res, done)) -app.listen(4000,() => { - console.log('Now listening for requests on port 4000'); -}); +app.listen(4000, () => { + console.log('Now listening for requests on port 4000') +}) diff --git a/server/auth/middleware/get-user.js b/server/auth/middleware/check-user.js similarity index 59% rename from server/auth/middleware/get-user.js rename to server/auth/middleware/check-user.js index 9c80b1757..7ef1c2341 100644 --- a/server/auth/middleware/get-user.js +++ b/server/auth/middleware/check-user.js @@ -1,3 +1,9 @@ +/* +We check here if the User that have just logged in or signed up already exists at +the Mongo DB Database or not. If now then we should create it with the minimun +set of information available. +*/ + const UserMongo = require('./add-user') const getUser = async (req, res, next) => {