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

Login system v1.0.0 #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["es2015"]
}
69 changes: 66 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,67 @@
node_modules
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Config file contains credentials
config.js

# VSCode removal
.vscode
./node_modules
./vscode

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# next.js build output
.next
119 changes: 0 additions & 119 deletions .npmignore

This file was deleted.

1 change: 0 additions & 1 deletion Procfile

This file was deleted.

32 changes: 26 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
# Getting Started with Inductogram
# Inductogram Backend

### This project is built in React.Js and NODE.js(v10.13.0)
## Steps to setup the API:
* Install nodejs for your os, to run the server, from
[download node.js](https://nodejs.org/en/download/)

# Installation Guide
1. Install [Node.Js](https://nodejs.org).
2. change directory to Inductogram and run npm install.
3. Crana would be installed along with the other dependencies and now run "crana dev" to start the project.
Note: You will need to make a config.js file in the project directory.
It's structure is similar to [config.js gist url](https://gist.github.com/naman-gupta99/14bb8b2802fa28ebb48686160c3c564c).
just add your mongodb url in the config.js file.

* Run the following command in the project directory to install all the required packages
```bash
npm install
```

* Run the following command in the project directory to run the server in development mode in windows
```bash
npm run server_dev
```

* To generate the Swagger API Docs in windows , run
```bash
npm run server_gen
```

## API Documentation
* Run the project in Swagger API Docs Generation Mode
* Navigate to <YOUR_PROJECT_DIRECTORY>/public/api-docs
* Open index.html
2 changes: 2 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require("babel-register");
require("./server.js");
18 changes: 18 additions & 0 deletions app/global/middlewares/ContentType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import ResponseTemplate from '../templates/response';

const ContentType = (req, res, next) => {
if (req.method === 'POST') {
const contentType = req.headers['content-type'];

if (!contentType || contentType.indexOf('application/json') !== 0) {
res.json(ResponseTemplate.invalidContentType());
} else {
next();
}
} else {
next();
}
};


export default ContentType;
13 changes: 13 additions & 0 deletions app/global/middlewares/DelayResponse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const DelayResponse = (req, res, next) => {
// delyay response with 1 second
if (req.method === 'GET' || req.method === 'POST') {
setTimeout(() => {
next();
}, 1000);
} else {
next();
}
};


export default DelayResponse;
12 changes: 12 additions & 0 deletions app/global/middlewares/EmptyContent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import ResponseTemplate from '../templates/response';

const EmptyContent = (req, res, next) => {
if (req.method === 'POST' && Object.keys(req.body).length === 0) {
res.json(ResponseTemplate.emptyContent());
} else {
next();
}
};


export default EmptyContent;
31 changes: 31 additions & 0 deletions app/global/middlewares/ValidAuthToken.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import jwt from 'jsonwebtoken';
import ResponseTemplate from '../templates/response';
import configServer from '../../../config';
import User from '../../models/Users';
import { logger } from '../../../log';

const ValidAuthToken = (req, res, next) => {
const token = req.headers.authorization;
if (token) {
jwt.verify(token, configServer.app.WEB_TOKEN_SECRET, (err, decodedUser) => {
if (err) {
logger.error(err);
res.status(401).json(ResponseTemplate.authError());
} else {
User.findById(decodedUser.user._id, (error, user) => {
if (error) {
res.status(401).json(ResponseTemplate.authError());
} else {
req.user = user;
next();
}
});
}
});
} else {
res.status(401).json(ResponseTemplate.authError());
}
};


export default ValidAuthToken;
13 changes: 13 additions & 0 deletions app/global/middlewares/csrfMidlleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const XSSProtection = (req, res, next) => {
if (req.method === 'OPTIONS') {
res.send();
return;
}
// adding response headers
res.header('X-XSS-Protection', '1; mode=block');
res.header('X-Frame-Options', 'deny');
res.header('X-Content-Type-Options', 'nosniff');
next();
};

export default XSSProtection;
26 changes: 26 additions & 0 deletions app/global/templates/response/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const Response = {
error(code, message, data = null) {
return {
success: false,
code: code || 400,
message: message || 'some error occoured',
data,
};
},
authError() {
return Response.error(
401,
'authentication error',
);
},
success(message, data = null) {
return {
success: true,
code: 200,
message,
data,
};
},
};

export default Response;
12 changes: 12 additions & 0 deletions app/helper/httpFunc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import fetch from "node-fetch";

const httpGet = (url, callback) => {
fetch(url)
.then(res => res.json())
.then(json => callback(null, json))
.catch(err => callback(err, null));
};

export default {
httpGet
};
Loading