Skip to content

Commit 5542748

Browse files
committed
- Add redis to docker-compose
- Streamline env management and reorganize files - Add harsh.ag14901@gmail.com to keyring
1 parent 4e3d1d5 commit 5542748

File tree

18 files changed

+74
-59
lines changed

18 files changed

+74
-59
lines changed

.env.secret

25 Bytes
Binary file not shown.

.gitsecret/keys/pubring.kbx

-36 Bytes
Binary file not shown.

.gitsecret/keys/pubring.kbx~

0 Bytes
Binary file not shown.

.gitsecret/paths/mapping.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
.env:9c1a52e44745107c32d34055320ee62f4572a83a96453b4d38495940c79ab9a3
1+
.env:1074cf24f051cdd8cbbdf11b4e1c8f2ed88736319a8c0771476e55c77e239a78
22
src/config/private.pem:040731b01f84c8b9119367982872acb3d046cdf67f21566e18cfa337c130c1d4
33
src/config/public.pem:2a545e85b82c860d6185deff6f81a8d478b7d21eba2062dceae84d2cee03211d

docker-compose.yml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,24 @@ version: '3.4'
22

33
services:
44
database:
5-
image: '${REGISTRY_NAME}mongo:latest'
5+
image: '${REGISTRY_NAME}mongo:4.2-bionic'
66
volumes:
77
- casidb:/data/db
88
networks:
99
- 'internal'
1010
restart: 'unless-stopped'
11+
redis:
12+
image: '${REGISTRY_NAME}redis:6.2-alpine'
13+
restart: always
14+
networks:
15+
- 'internal'
16+
ports:
17+
- '6379:6379'
18+
env_file:
19+
- './.env'
20+
command: redis-server --loglevel warning --requirepass ${REDIS_PASS}
21+
volumes:
22+
- casi_redis:/data
1123
CASI:
1224
build: .
1325
image: '${REGISTRY_NAME}devclubiitd/casi:0.1'
@@ -30,9 +42,11 @@ services:
3042
- MONGODB_URI_LOCAL
3143
depends_on:
3244
- database
45+
- redis
3346

3447
volumes:
3548
casidb:
49+
casi_redis:
3650

3751
networks:
3852
reverseproxy:

src/config/axios.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as keys from './keys';
2+
3+
const HttpsProxyAgent = require('https-proxy-agent');
4+
5+
const axiosDefaultConfig = {
6+
proxy: false,
7+
httpsAgent: !keys.isDev
8+
? new HttpsProxyAgent('http://devclub.iitd.ac.in:3128')
9+
: null,
10+
};
11+
const axios = require('axios').create(axiosDefaultConfig);
12+
13+
export default axios;

src/config/keys.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
const fs = require('fs');
22
const path = require('path');
33

4+
require('dotenv').config({
5+
path: `${__dirname}/../../.env`,
6+
});
7+
48
export const expTime = 60 * 20;
59
export const rememberTime = 60 * 60 * 24 * 2;
610
export const reqExpTime = 60;
@@ -19,6 +23,7 @@ export const accountExists =
1923
'An account is already linked with that account, Please try linking another one.';
2024

2125
export const noRedirectState = 'xyz';
26+
export const isDev = process.env.NODE_ENV === 'DEV';
2227

2328
// Role to Privilege
2429
export const r2p = {

src/config/private.pem.secret

-3 Bytes
Binary file not shown.

src/config/public.pem.secret

-3 Bytes
Binary file not shown.

src/config/redis.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import redis from 'redis';
2+
import { isDev } from './keys';
3+
4+
const redisURl = isDev
5+
? 'redis://127.0.0.1:6379'
6+
: `redis://:${process.env.REDIS_PASS}@redis:6379`;
7+
console.log('Redis url: ', redisURl);
8+
const rtokens = redis.createClient({
9+
url: redisURl,
10+
});
11+
12+
rtokens.on('error', (err) => {
13+
console.log(err);
14+
});
15+
16+
export default rtokens;

src/data/resourceToken.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/routes/auth.js

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import express from 'express';
22
import { verify, decode } from 'jsonwebtoken';
33
import bcrypt from 'bcryptjs';
44
import util from 'util';
5-
import rtoken from '../data/resourceToken';
5+
import rtoken from '../config/redis';
66
import * as keys from '../config/keys';
77
import {
88
verifyToken,
@@ -24,19 +24,10 @@ import {
2424
noRedirectState,
2525
} from '../config/keys';
2626
import { Client, User } from '../models/user';
27+
import axios from '../config/axios';
2728

2829
const router = express.Router();
2930
const passport = require('passport');
30-
const HttpsProxyAgent = require('https-proxy-agent');
31-
32-
const axiosDefaultConfig = {
33-
proxy: false,
34-
httpsAgent:
35-
process.env.NODE_ENV !== 'DEV'
36-
? new HttpsProxyAgent('http://devclub.iitd.ac.in:3128')
37-
: null,
38-
};
39-
const axios = require('axios').create(axiosDefaultConfig);
4031
const qs = require('qs');
4132
// post route to check validity of tokens, clients will hit this route.
4233
router.post('/refresh-token', async (req, res) => {
@@ -69,10 +60,10 @@ router.get('/email/verify/token', async (req, res) => {
6960
} catch (error) {
7061
console.log(error);
7162
res.clearCookie(accessTokenName, {
72-
domain: process.env.NODE_ENV !== 'DEV' ? 'devclub.in' : null,
63+
domain: !keys.isDev ? 'devclub.in' : null,
7364
});
7465
res.clearCookie(refreshTokenName, {
75-
domain: process.env.NODE_ENV !== 'DEV' ? 'devclub.in' : null,
66+
domain: !keys.isDev ? 'devclub.in' : null,
7667
});
7768
res.render('account_verified', { error: true });
7869
}
@@ -130,10 +121,10 @@ router.get('/password/reset/token', async (req, res) => {
130121
} catch (error) {
131122
console.log(error);
132123
res.clearCookie(accessTokenName, {
133-
domain: process.env.NODE_ENV !== 'DEV' ? 'devclub.in' : null,
124+
domain: !keys.isDev ? 'devclub.in' : null,
134125
});
135126
res.clearCookie(refreshTokenName, {
136-
domain: process.env.NODE_ENV !== 'DEV' ? 'devclub.in' : null,
127+
domain: !keys.isDev ? 'devclub.in' : null,
137128
});
138129
res.render('login', {
139130
message: 'Invalid Token. Please try resetting your password again',
@@ -348,8 +339,8 @@ router.get('/clientVerify', async (req, res) => {
348339
const token = createJWTCookie(user, res, refreshTokenName);
349340
res.cookie('_rememberme', token, {
350341
httpOnly: false,
351-
domain: process.env.NODE_ENV !== 'DEV' ? 'devclub.in' : null,
352-
secure: process.env.NODE_ENV !== 'DEV',
342+
domain: !keys.isDev ? 'devclub.in' : null,
343+
secure: !keys.isDev,
353344
});
354345
return res.status(200).json({
355346
err: false,

src/routes/profile.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/* eslint-disable import/named */
33
import express from 'express';
44
import { verifyToken, getUserPrivilege } from '../utils/utils';
5-
import { accessTokenName, refreshTokenName } from '../config/keys';
5+
import { accessTokenName, isDev, refreshTokenName } from '../config/keys';
66
import settingsRoutes from './settings';
77
import { Client, SocialAccount, User } from '../models/user';
88

@@ -28,10 +28,10 @@ router.post('/', async (req, res) => {
2828
router.post('/logout', (req, res) => {
2929
try {
3030
res.clearCookie(accessTokenName, {
31-
domain: process.env.NODE_ENV !== 'DEV' ? 'devclub.in' : null,
31+
domain: !isDev ? 'devclub.in' : null,
3232
});
3333
res.clearCookie(refreshTokenName, {
34-
domain: process.env.NODE_ENV !== 'DEV' ? 'devclub.in' : null,
34+
domain: !isDev ? 'devclub.in' : null,
3535
});
3636
return res.json({
3737
err: false,
@@ -85,7 +85,7 @@ router.post('/delete', async (req, res) => {
8585
await user.remove();
8686

8787
res.clearCookie(accessTokenName, {
88-
domain: process.env.NODE_ENV !== 'DEV' ? 'devclub.in' : null,
88+
domain: !isDev ? 'devclub.in' : null,
8989
});
9090
return res.redirect('/');
9191
} catch (error) {

src/routes/settings.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable import/named */
22
import bcrypt from 'bcryptjs';
33
import { SocialAccount } from '../models/user';
4-
import { accessTokenName } from '../config/keys';
4+
import { accessTokenName, isDev } from '../config/keys';
55
import { createJWTCookie, verifyToken } from '../utils/utils';
66

77
const router = require('express').Router();
@@ -101,8 +101,7 @@ router.post('/', async (req, res) => {
101101
} else {
102102
// If the validation was successful, update the user and create a new JWT for the updated credentials
103103
res.clearCookie(accessTokenName, {
104-
domain:
105-
process.env.NODE_ENV !== 'DEV' ? 'devclub.in' : null,
104+
domain: !isDev ? 'devclub.in' : null,
106105
});
107106
await createJWTCookie(user, res);
108107
res.render('settings', { messages });

src/routes/user.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
sendVerificationEmail,
77
addRoles,
88
} from '../utils/utils';
9-
import { refreshTokenName } from '../config/keys';
9+
import { isDev, refreshTokenName } from '../config/keys';
1010
import { User } from '../models/user';
1111

1212
const router = express.Router();
@@ -124,7 +124,7 @@ router.post('/register', async (req, res) => {
124124
username,
125125
email,
126126
password,
127-
isverified: process.env.NODE_ENV === 'DEV',
127+
isverified: isDev,
128128
});
129129

130130
// encrypt the password using bcrypt
@@ -142,7 +142,7 @@ router.post('/register', async (req, res) => {
142142

143143
addRoles(user);
144144

145-
if (process.env.NODE_ENV !== 'DEV') {
145+
if (!isDev) {
146146
sendVerificationEmail(user);
147147
}
148148

src/server.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ import * as keys from './config/keys';
1313

1414
import { socialAuthenticate, linkSocial } from './utils/utils';
1515

16-
require('dotenv').config({
17-
path: `${__dirname}/../.env`,
18-
});
19-
2016
const app = express();
2117

2218
const passport = require('passport');
@@ -192,7 +188,7 @@ app.use('/profile', profile);
192188
app.use('/client', client);
193189
app.use('/api', api);
194190

195-
if (process.env.NODE_ENV === 'DEV') {
191+
if (keys.isDev) {
196192
app.use('/test', tests);
197193
}
198194
app.get('/privacy-policy', (req, res) => {

src/tests/tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import express from 'express';
22
import util from 'util';
3-
import rtoken from '../data/resourceToken';
3+
import rtoken from '../config/redis';
44
import { makeid } from '../utils/utils';
55

66
const router = express.Router();

src/utils/utils.js

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,7 @@
66
import jwt, { verify } from 'jsonwebtoken';
77
import * as keys from '../config/keys';
88
import { User, SocialAccount, Role } from '../models/user';
9-
10-
const HttpsProxyAgent = require('https-proxy-agent');
11-
12-
const axiosDefaultConfig = {
13-
proxy: false,
14-
httpsAgent:
15-
process.env.NODE_ENV !== 'DEV'
16-
? new HttpsProxyAgent('http://devclub.iitd.ac.in:3128')
17-
: null,
18-
};
19-
const axios = require('axios').create(axiosDefaultConfig);
9+
import axios from '../config/axios';
2010

2111
const getUserPrivilege = (user) => {
2212
let privilege = 0;
@@ -67,10 +57,10 @@ const createJWTCookie = (user, res, tokenName = keys.accessTokenName) => {
6757
// set the cookie with token with the same age as that of token
6858
res.cookie(tokenName, token, {
6959
maxAge: exp * 1000, // in milli seconds
70-
secure: process.env.NODE_ENV !== 'DEV', // set to true if you are using https
60+
secure: !keys.isDev, // set to true if you are using https
7161
httpOnly: true,
7262
sameSite: 'lax',
73-
domain: process.env.NODE_ENV !== 'DEV' ? 'devclub.in' : null,
63+
domain: !keys.isDev ? 'devclub.in' : null,
7464
});
7565
return token;
7666
};
@@ -120,10 +110,10 @@ const verifyToken = async (
120110
// I wasn't able to verify the token as it was invalid
121111
// clear the tokens
122112
res.clearCookie(keys.accessTokenName, {
123-
domain: process.env.NODE_ENV !== 'DEV' ? 'devclub.in' : null,
113+
domain: !keys.isDev ? 'devclub.in' : null,
124114
});
125115
res.clearCookie(keys.refreshTokenName, {
126-
domain: process.env.NODE_ENV !== 'DEV' ? 'devclub.in' : null,
116+
domain: !keys.isDev ? 'devclub.in' : null,
127117
});
128118
throw err;
129119
}

0 commit comments

Comments
 (0)