Skip to content

Commit

Permalink
formats all files
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmetonurslmz committed Sep 26, 2020
1 parent 4091e2e commit 4da46c7
Show file tree
Hide file tree
Showing 15 changed files with 227 additions and 174 deletions.
11 changes: 6 additions & 5 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const app = express();

const path = require('path');

app.set('view engine','ejs');
app.set('views',path.join(__dirname, '/views'));
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, '/views'));

app.use(express.json());
app.use(express.urlencoded({ extended: true }));
Expand All @@ -32,15 +32,16 @@ app.use((err, req, res, next) => {
}
});


const run = async () => {
try {
const connectDb = require('./core/database/mongo');
await connectDb();
app.listen(process.env.PORT,async () => console.log(`Application runs on port ${process.env.PORT}`));
app.listen(process.env.PORT, async () =>
console.log(`Application runs on port ${process.env.PORT}`)
);
} catch (e) {
console.log('App has been crashed!');
}
}
};

run();
6 changes: 4 additions & 2 deletions core/services/hashGenerator.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ class HashGeneratorService {
}

generate() {
return crypto.createHmac(this.algorithm, this.secret).update(this.generateRandomString()).digest(this.encodingType);
return crypto
.createHmac(this.algorithm, this.secret)
.update(this.generateRandomString())
.digest(this.encodingType);
}
}


module.exports = HashGeneratorService;
16 changes: 9 additions & 7 deletions core/services/jwtGenerator.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const base64 = require('base64url');
const fs = require('fs');
const path = require('path');


/**
* When instance initialized, function that createHashedSignature works.
* Default header or payload values cannot be changed.
Expand All @@ -24,10 +23,12 @@ class JwtGeneratorService {
}

fetchPrivateKey() {
this.PREV_KEY = fs.readFileSync(`${path.dirname(require.main.filename)}/id_rsa_priv.pem`, 'utf8');
this.PREV_KEY = fs.readFileSync(
`${path.dirname(require.main.filename)}/id_rsa_priv.pem`,
'utf8'
);
}


/**
* creates iat and exp date.
* @param isLongPeriod {Boolean}: If it is false, period is 60 minutes otherwise it is 60 days.
Expand All @@ -36,12 +37,14 @@ class JwtGeneratorService {
getDateInformation(isLongPeriod = false) {
const date = new Date();

const period = isLongPeriod ? (24 * 60 * 60) : 60;
const period = isLongPeriod ? 24 * 60 * 60 : 60;

return {
iat: date.getTime(),
exp: new Date(date.setMinutes(date.getMinutes() + period)).getTime()
}
exp: new Date(
date.setMinutes(date.getMinutes() + period)
).getTime(),
};
}

get header() {
Expand Down Expand Up @@ -87,5 +90,4 @@ class JwtGeneratorService {
}
}


module.exports = JwtGeneratorService;
11 changes: 9 additions & 2 deletions core/services/jwtVerification.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ class JwtVerificationService {
}

fetchPublicKey() {
this.PUB_KEY = fs.readFileSync(`${path.dirname(require.main.filename)}/id_rsa_pub.pem`, 'utf8');
this.PUB_KEY = fs.readFileSync(
`${path.dirname(require.main.filename)}/id_rsa_pub.pem`,
'utf8'
);
}

parseAccessToken(accessToken) {
Expand All @@ -32,7 +35,11 @@ class JwtVerificationService {
verifyFunction.end();

const jwtSignatureBase64 = base64.toBase64(this.signature);
return verifyFunction.verify(this.PUB_KEY, jwtSignatureBase64, 'base64');
return verifyFunction.verify(
this.PUB_KEY,
jwtSignatureBase64,
'base64'
);
}
}

Expand Down
8 changes: 7 additions & 1 deletion core/services/validateEntries.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ const validateEntries = (entries, req) => {
// checks whether req.body has key that equals dynamic entry parameter
// checks whether it filled
// checks whether it does not have blank.
if (!(Object.prototype.hasOwnProperty.call(req.body, entry) && req.body[entry] && req.body[entry].trim())) {
if (
!(
Object.prototype.hasOwnProperty.call(req.body, entry) &&
req.body[entry] &&
req.body[entry].trim()
)
) {
const err = new Error();
err.field = entry;
err.message = `Couldn't find ${entry}`;
Expand Down
45 changes: 23 additions & 22 deletions modules/account/models/AccountModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,29 @@ const collection = 'accounts';

const { Schema } = mongoose;


const ModelSchema = new Schema({
email: {
required: true,
unique: true,
type: String,
},
password: {
type: String,
unique: true,
required: true,
},
is_active: {
type: Boolean,
default: true,
const ModelSchema = new Schema(
{
email: {
required: true,
unique: true,
type: String,
},
password: {
type: String,
unique: true,
required: true,
},
is_active: {
type: Boolean,
default: true,
},
created_at: {
type: Date,
default: new Date(),
},
},
created_at: {
type: Date,
default: new Date(),
},
}, { collection, versionKey: false });

{ collection, versionKey: false }
);

const model = mongoose.model(collection, ModelSchema);
module.exports = model
module.exports = model;
18 changes: 10 additions & 8 deletions modules/account/routers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ const { check } = require('express-validator');

const { login, register } = require('../controllers/accountController');

router.post('/login', [
check('email').exists(),
check('password').exists(),
], login);
router.post(
'/login',
[check('email').exists(), check('password').exists()],
login
);

router.post('/register', [
check('email').exists(),
check('password').exists(),
], register);
router.post(
'/register',
[check('email').exists(), check('password').exists()],
register
);

module.exports = router;
83 changes: 42 additions & 41 deletions modules/auth/models/AccessTokenModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,47 @@ const collection = 'access_tokens';

const { Schema } = mongoose;


const ModelSchema = new Schema({
grant_type: {
type: String,
required: true,
},
expires_in: {
type: Number,
default: 36000,
},
client_id: {
type: mongoose.ObjectId,
required: true,
},
is_active: {
type: Boolean,
default: true,
},
access_token: {
type: String,
required: true,
},
refresh_token: {
type: String,
required: true,
},
token_type: {
type: String,
default: 'Bearer',
},
created_at: {
type: Date,
default: new Date(),
},
authorization_code_id: {
type: mongoose.ObjectId,
required: false,
},
}, { collection, versionKey: false });

const ModelSchema = new Schema(
{
grant_type: {
type: String,
required: true,
},
expires_in: {
type: Number,
default: 36000,
},
client_id: {
type: mongoose.ObjectId,
required: true,
},
is_active: {
type: Boolean,
default: true,
},
access_token: {
type: String,
required: true,
},
refresh_token: {
type: String,
required: true,
},
token_type: {
type: String,
default: 'Bearer',
},
created_at: {
type: Date,
default: new Date(),
},
authorization_code_id: {
type: mongoose.ObjectId,
required: false,
},
},
{ collection, versionKey: false }
);

const model = mongoose.model(collection, ModelSchema);
module.exports = model
module.exports = model;
53 changes: 27 additions & 26 deletions modules/auth/models/AuthorizationCodeModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,33 @@ const collection = 'authorization_codes';

const { Schema } = mongoose;


const ModelSchema = new Schema({
client_id: {
type: mongoose.ObjectId,
required: true,
},
code: {
type: String,
unique: true,
required: true,
},
expiry_date: {
type: Date,
required: true,
const ModelSchema = new Schema(
{
client_id: {
type: mongoose.ObjectId,
required: true,
},
code: {
type: String,
unique: true,
required: true,
},
expiry_date: {
type: Date,
required: true,
},
is_active: {
type: Boolean,
required: true,
default: true,
},
created_at: {
type: Date,
default: new Date(),
},
},
is_active: {
type: Boolean,
required: true,
default: true,
},
created_at: {
type: Date,
default: new Date(),
},
}, { collection, versionKey: false });

{ collection, versionKey: false }
);

const model = mongoose.model(collection, ModelSchema);
module.exports = model
module.exports = model;
Loading

0 comments on commit 4da46c7

Please sign in to comment.