-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Abhishek Singh
committed
Jun 9, 2021
0 parents
commit ec12ae2
Showing
33 changed files
with
3,490 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
DB_DEV = '' | ||
DB_LIVE = '' | ||
DB_TEST = '' | ||
PORT=4444; | ||
SECRET='sdgatbawtawuiytw4378vnek' | ||
NODE_ENV = 'development' | ||
ADMIN = [email protected] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.env | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Change Log | ||
|
||
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. | ||
|
||
<a name="1.0.2"></a> | ||
## [1.0.2](https://github.com/abhi11210646/Nodejs-API/compare/v1.0.1...v1.0.2) (2017-11-24) | ||
|
||
|
||
|
||
<a name="1.0.1"></a> | ||
## 1.0.1 (2017-11-24) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#REST API |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const expect = require('chai').expect; | ||
const request = require("supertest"); | ||
const server = require("../../server"); | ||
describe('API status check', () => { | ||
it('should return OK', async () => { | ||
const res = await request(server).get('/'); | ||
expect(res.status).to.equal(200); | ||
expect(res.body.status).to.equal('OK'); | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
const expect = require('chai').expect; | ||
const request = require("supertest"); | ||
const mongoose = require("mongoose"); | ||
describe('/Auth-Middleware', () => { | ||
let server; | ||
let User; | ||
let userSchema = { username: "[email protected]", password: "1234567890" } | ||
before(async () => { | ||
server = require("./../../server"); | ||
User = mongoose.model('User'); | ||
await User.deleteMany({}); | ||
|
||
const user = new User(); | ||
const password = user.encryptPassword(userSchema.password); | ||
await User.create({ username: userSchema.username, password: password }); | ||
}); | ||
describe('SignUp#', () => { | ||
it('should SignUp succesfully if valid Input Passed', async () => { | ||
const res = await request(server).post('/v1/api/signUp').send({ username: "[email protected]", password: "1234567890" }); | ||
expect(res.status).to.equal(201); | ||
expect(res.body.data).to.be.an('object').with.a.property('token'); | ||
}) | ||
it('should return status 409 if username already exists', async () => { | ||
const res = await request(server).post('/v1/api/signUp').send(userSchema); | ||
expect(res.status).to.equal(409); | ||
}) | ||
}); | ||
describe('Login#', () => { | ||
it('should Login with valid credentials', async () => { | ||
const res = await request(server).post('/v1/api/login').send(userSchema); | ||
expect(res.status).to.equal(200); | ||
expect(res.body.data).to.be.an('object').with.a.property('token'); | ||
}) | ||
it('should return 401 if credential are not valid', async () => { | ||
const res = await request(server).post('/v1/api/login').send({ username: "[email protected]", password: "sss" }); | ||
expect(res.status).to.equal(401); | ||
}) | ||
}); | ||
after(async () => { | ||
await User.deleteMany({}); | ||
await server.close(); | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const jwtService = require("./../../src/app/services/jwtService"); | ||
const jwt = require("jsonwebtoken"); | ||
const mongoose = require('mongoose'); | ||
const config = require('config'); | ||
const expect = require('chai').expect; | ||
describe('/JWT-Service', () => { | ||
it('should create valid jwt token', async () => { | ||
const payload = { id: mongoose.Types.ObjectId(), email: "[email protected]" }; | ||
const token = await new jwtService().createJwtToken(payload); | ||
const decoded = jwt.verify(token, config.get("jwt.secretOrKey")); | ||
expect(decoded.user.email).to.equal(payload.email); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"database": { | ||
"url": "DB" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"database": { | ||
"url": "" | ||
}, | ||
"jwt": { | ||
"secretOrKey": "DEV_SECRET", | ||
"issuer": "JONU", | ||
"audience": "JONU", | ||
"expiresIn": "24h", | ||
"algorithm":"HS256" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"database": { | ||
"url": "" | ||
}, | ||
"jwt": { | ||
"secretOrKey": "PROD_SECRET", | ||
"issuer": "JONU", | ||
"audience": "JONU", | ||
"expiresIn": "24h", | ||
"algorithm":"HS256" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"database": { | ||
"url": "" | ||
}, | ||
"jwt": { | ||
"secretOrKey": "TEST_SECRET", | ||
"issuer": "JONU", | ||
"audience": "JONU", | ||
"expiresIn": "1h", | ||
"algorithm": "HS256" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const fs = require("fs"); | ||
const join = require("path").join; | ||
|
||
// require all models | ||
const modelDir = join(__dirname,'./../../src/app/model'); | ||
fs.readdirSync(modelDir) | ||
.filter(file => ~file.search(/^[^\.].*\.js$/)) | ||
.forEach(file => require(join(modelDir, file))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'use strict'; | ||
const mongoose = require('mongoose'); | ||
mongoose.Promise = global.Promise; | ||
const config = require("config"); | ||
// module.exports = () => { | ||
const options = { | ||
keepAlive: 1000, | ||
useNewUrlParser: true, | ||
useCreateIndex: true | ||
}; | ||
mongoose.connect(config.get("database.url"), options, (err, db) => { | ||
if (err) console.log('Mongoose connection error', err.message); | ||
}); | ||
mongoose.connection.on('connected', function () { | ||
console.log('Mongoose connected'); | ||
}); | ||
mongoose.connection.on('disconnected', function () { | ||
console.log('Mongoose default connection disconnected'); | ||
}); | ||
mongoose.connection.on('error', console.error.bind(console, 'MongoDb connection error')); | ||
|
||
process.on('SIGINT', function () { | ||
mongoose.connection.close(function () { | ||
console.log('Mongoose default connection disconnected through app termination'); | ||
process.exit(0); | ||
}); | ||
}); | ||
// }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const app = require('express')(); | ||
require("dotenv").config(); | ||
const passport = require('passport'); | ||
const bodyParser = require('body-parser'); | ||
const noc = require('no-console'); | ||
const cors = require('cors'); | ||
|
||
// Bootstrap schemas, models | ||
require("./bootstrap"); | ||
|
||
// App configuration | ||
noc(app); | ||
app.use(bodyParser.json()); | ||
app.use(passport.initialize()); | ||
app.use(cors()); | ||
|
||
//Database connection | ||
require('./db'); | ||
//Passport configuration | ||
require('./passport')(passport); | ||
//Routes configuration | ||
require("./../src/routes")(app); | ||
|
||
|
||
module.exports = app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const local = require("./local"); | ||
const jwt = require("./jwt"); | ||
module.exports = (passport) => { | ||
passport.use(local); | ||
passport.use(jwt); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const JwtStrategy = require('passport-jwt').Strategy, | ||
ExtractJwt = require('passport-jwt').ExtractJwt; | ||
const config = require("config"); | ||
const opts = { | ||
secretOrKey: config.get("jwt.secretOrKey"), | ||
issuer: config.get("jwt.issuer"), | ||
audience: config.get("jwt.audience"), | ||
passReqToCallback: false, | ||
jwtFromRequest: ExtractJwt.fromAuthHeaderWithScheme("jwt") | ||
}; | ||
module.exports = new JwtStrategy(opts, function (jwt_payload, done) { | ||
return done(null, jwt_payload.user, {}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const localStratagy = require('passport-local').Strategy; | ||
const mongoose = require('mongoose'); | ||
const User = mongoose.model('User'); | ||
module.exports = new localStratagy({ | ||
usernameField: 'username', | ||
passwordField: 'password', | ||
}, | ||
async (username, password, callback) => { | ||
try { | ||
let user = await User.findOne({ username }); | ||
if (user) { | ||
if (!user.isValidPassword(password)) { | ||
return callback(null, false, { "message": "Password is Incorrect!" }); | ||
} | ||
}else { | ||
return callback(null, false, { "message": "User does not exist!" }); | ||
} | ||
return callback(null, user, { "message": "Successfully LoggedIn!" }); | ||
} | ||
catch (error) { | ||
return callback(error, false, { "message": "Something Went Wrong!" }); | ||
} | ||
} | ||
); |
Oops, something went wrong.