Skip to content

Commit

Permalink
fix(api): update visitor_id field name in req.headers and firestore t…
Browse files Browse the repository at this point in the history
…o visitorid
  • Loading branch information
rhahao authored Sep 30, 2022
1 parent f4910c6 commit 82aca1a
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 32 deletions.
8 changes: 4 additions & 4 deletions src/controllers/auth-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ export const loginUser = async (req, res, next) => {
return;
}

const { email, password, visitor_id } = req.body;
const { email, password, visitorid } = req.body;

// validate visitor id
const client = new FingerprintJsServerApiClient({
region: Region.Global,
apiKey: process.env.FINGERPRINT_API_SERVER_KEY,
});

const visitorHistory = await client.getVisitorHistory(visitor_id, {
const visitorHistory = await client.getVisitorHistory(visitorid, {
limit: 1,
});

Expand Down Expand Up @@ -90,14 +90,14 @@ export const loginUser = async (req, res, next) => {

// revoke matched session
let newSessions = sessions.filter(
(session) => session.visitor_id !== visitor_id
(session) => session.visitorid !== visitorid
);

const now = new Date();
const expiryDate = now.getTime() + 24 * 60 * 60000; // expired after 1 day

newSessions.push({
visitor_id: visitor_id,
visitorid: visitorid,
visitor_details: { ...visitorHistory.visits[0] },
expires: expiryDate,
mfaVerified: false,
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/congregation-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -1033,7 +1033,7 @@ export const deletePocketDevice = async (req, res, next) => {
return;
}

const { pocket_visitor_id } = req.body;
const { pocket_visitorid } = req.body;

if (id && user) {
const cong = await getCongregationInfo(id);
Expand All @@ -1047,7 +1047,7 @@ export const deletePocketDevice = async (req, res, next) => {
// remove device
let devices = userData.pocket_devices || [];
let newDevices = devices.filter(
(device) => device.visitor_id !== pocket_visitor_id
(device) => device.visitorid !== pocket_visitorid
);

if (newDevices.length > 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/mfa-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ export const verifyToken = async (req, res, next) => {

if (verified?.delta === 0) {
// update mfa enabled && verified
const { visitor_id } = req.headers;
const { visitorid } = req.headers;

let newSessions = sessions.map((session) => {
if (session.visitor_id === visitor_id) {
if (session.visitorid === visitorid) {
return {
...session,
mfaVerified: true,
Expand Down
10 changes: 5 additions & 5 deletions src/controllers/sws-pocket-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ export const pocketSignUp = async (req, res, next) => {
}

// get visitor ID and otp code
const { otp_code, visitor_id } = req.body;
const { otp_code, visitorid } = req.body;

// validate visitor id
const client = new FingerprintJsServerApiClient({
region: Region.Global,
apiKey: process.env.FINGERPRINT_API_SERVER_KEY,
});

const visitorHistory = await client.getVisitorHistory(visitor_id, {
const visitorHistory = await client.getVisitorHistory(visitorid, {
limit: 1,
});

Expand Down Expand Up @@ -104,13 +104,13 @@ export const pocketSignUp = async (req, res, next) => {
let devices = user.pocket_devices || [];

const obj = {
visitor_id: visitor_id,
visitorid: visitorid,
name: `${visit.browserDetails.os} ${visit.browserDetails.osVersion} (${visit.browserDetails.browserName} ${visit.browserDetails.browserFullVersion})`,
sws_last_seen: new Date().getTime(),
};

const foundDevice = devices.find(
(device) => device.visitor_id === visitor_id
(device) => device.visitorid === visitorid
);

// device already added
Expand All @@ -136,7 +136,7 @@ export const pocketSignUp = async (req, res, next) => {
pocket_members,
cong_name,
cong_number,
} = await findPocketByVisitorID(visitor_id);
} = await findPocketByVisitorID(visitorid);

res.locals.type = 'info';
res.locals.message = 'pocket device visitor id added successfully';
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/users-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,10 @@ export const deleteUserSession = async (req, res, next) => {

export const userLogout = async (req, res, next) => {
try {
const { email, visitor_id } = req.headers;
const { email, visitorid } = req.headers;
const user = await getUserInfo(email);

await revokeSessions(user.id, visitor_id);
await revokeSessions(user.id, visitorid);

res.locals.type = 'info';
res.locals.message = `the current user has logged out`;
Expand Down
12 changes: 6 additions & 6 deletions src/middleware/sws-pocket-auth-checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const db = getFirestore();
export const pocketAuthChecker = () => {
return async (req, res, next) => {
try {
await check('visitor_id').notEmpty().run(req);
await check('visitorid').notEmpty().run(req);

const errors = validationResult(req);

Expand All @@ -29,9 +29,9 @@ export const pocketAuthChecker = () => {
return;
}

const { visitor_id } = req.headers;
const { visitorid } = req.headers;

const user = await findPocketByVisitorID(visitor_id);
const user = await findPocketByVisitorID(visitorid);

// found user or it is a sign up request
if (user || req.path === '/signup') {
Expand All @@ -41,15 +41,15 @@ export const pocketAuthChecker = () => {
if (user) {
const { id, pocket_devices } = user;
const foundDevice = pocket_devices.find(
(device) => device.visitor_id === visitor_id
(device) => device.visitorid === visitorid
);
const filteredDevices = pocket_devices.filter(
(device) => device.visitor_id !== visitor_id
(device) => device.visitorid !== visitorid
);

const updatedDevices = [
{
visitor_id,
visitorid,
name: foundDevice.name,
sws_last_seen: new Date().getTime(),
},
Expand Down
8 changes: 4 additions & 4 deletions src/middleware/visitor-checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const db = getFirestore();
export const visitorChecker = () => {
return async (req, res, next) => {
try {
await check('visitor_id').notEmpty().run(req);
await check('visitorid').notEmpty().run(req);
await check('email').isEmail().run(req);

const errors = validationResult(req);
Expand All @@ -29,7 +29,7 @@ export const visitorChecker = () => {
return;
}

const { email, visitor_id } = req.headers;
const { email, visitorid } = req.headers;
const user = await getUserInfo(email);

if (user) {
Expand All @@ -52,7 +52,7 @@ export const visitorChecker = () => {

// find if visitor id has valid session
const findSession = sessions.find(
(session) => session.visitor_id === visitor_id
(session) => session.visitorid === visitorid
);

if (findSession) {
Expand All @@ -64,7 +64,7 @@ export const visitorChecker = () => {
// update last seen

let newSessions = sessions.map((session) => {
if (session.visitor_id === visitor_id) {
if (session.visitorid === visitorid) {
return { ...session, sws_last_seen: new Date().getTime() };
} else {
return session;
Expand Down
2 changes: 1 addition & 1 deletion src/routes/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ router.post(
'/user-login',
body('email').isEmail(),
body('password').isLength({ min: 6 }),
body('visitor_id').notEmpty(),
body('visitorid').notEmpty(),
loginUser
);

Expand Down
2 changes: 1 addition & 1 deletion src/routes/congregation.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ router.get('/:id/pockets/:user/code', generatePocketOTPCode);
// delete pocket device
router.delete(
'/:id/pockets/:user',
body('pocket_visitor_id').notEmpty(),
body('pocket_visitorid').notEmpty(),
deletePocketDevice
);

Expand Down
2 changes: 1 addition & 1 deletion src/routes/sws-pocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const router = express.Router();

router.post(
'/signup',
body('visitor_id').notEmpty(),
body('visitorid').notEmpty(),
body('otp_code').isLength(10),
pocketSignUp
);
Expand Down
4 changes: 2 additions & 2 deletions src/utils/sws-pocket-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
import { getUsers } from './user-utils.js';
import { decryptData } from './encryption-utils.js';

export const findPocketByVisitorID = async (visitor_id) => {
export const findPocketByVisitorID = async (visitorid) => {
const users = await getUsers();

let user;

for (let i = 0; i < users.length; i++) {
const devices = users[i].pocket_devices || [];
const found = devices.find((device) => device.visitor_id === visitor_id);
const found = devices.find((device) => device.visitorid === visitorid);

if (found) {
user = users[i];
Expand Down
4 changes: 2 additions & 2 deletions src/utils/user-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export const getUserActiveSessions = async (userID) => {

const result = sessions.map((session) => {
let obj = {
visitor_id: session.visitor_id,
visitorid: session.visitorid,
ip: session.visitor_details.ip,
country_name: session.visitor_details.ipLocation.country.name,
device: {
Expand All @@ -195,7 +195,7 @@ export const revokeSessions = async (userID, visitorID) => {
if (userSnap.exists) {
const sessions = userSnap.data().about.sessions || [];
const newSessions = sessions.filter(
(session) => session.visitor_id !== visitorID
(session) => session.visitorid !== visitorID
);

await db
Expand Down

0 comments on commit 82aca1a

Please sign in to comment.