-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from SakshamSahgal/61-add-a-user-actvity-log-f…
…eature 61 add a user actvity log feature
- Loading branch information
Showing
10 changed files
with
176 additions
and
28 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,89 @@ | ||
const axios = require('axios'); | ||
|
||
//function to get the browser information Object | ||
function getBrowser(browser) { | ||
|
||
let browserInfo = { | ||
name: browser | ||
} | ||
|
||
// console.log(browser) | ||
|
||
// if (fs.existsSync(path.join(__dirname, "..", "ClientSide", "Static", "GUI", "Browser", browser + ".png"))) | ||
// browserInfo.logo = path.join("GUI", "Browser", browser + ".png") | ||
// else | ||
// browserInfo.logo = path.join("GUI", "Browser", "default.png") | ||
|
||
return browserInfo; | ||
} | ||
|
||
function getOS(os) { | ||
|
||
let osInfo = { | ||
name: os.os | ||
} | ||
|
||
// console.log(os) | ||
|
||
|
||
// if (os.isAndroid) | ||
// osInfo.logo = path.join("GUI", "OS", "Android.png") | ||
// else if (os.isWindows) | ||
// osInfo.logo = path.join("GUI", "OS", "Windows.png") | ||
// else if (os.isLinux || os.isLinux64) | ||
// osInfo.logo = path.join("GUI", "OS", "Linux.png") | ||
// else if (os.isMac) | ||
// osInfo.logo = path.join("GUI", "OS", "Mac.png") | ||
// else if (os.isIOS) | ||
// osInfo.logo = path.join("GUI", "OS", "IOS.png") | ||
// else | ||
// osInfo.logo = path.join("GUI", "OS", "Default.png") | ||
|
||
return osInfo; | ||
} | ||
|
||
function getDeviceType(userAgentInfo) { | ||
|
||
DeviceTypeInfo = { | ||
} | ||
|
||
if (userAgentInfo.isMobile) { | ||
DeviceTypeInfo.name = "Mobile" | ||
// DeviceTypeInfo.logo = path.join("GUI", "DeviceType", "Mobile.png") | ||
} else if (userAgentInfo.isTablet) { | ||
DeviceTypeInfo.name = "Tablet" | ||
// DeviceTypeInfo.logo = path.join("GUI", "DeviceType", "Tablet.png") | ||
} else if (userAgentInfo.isDesktop) { | ||
DeviceTypeInfo.name = "Desktop" | ||
// DeviceTypeInfo.logo = path.join("GUI", "DeviceType", "Desktop.png") | ||
} else { | ||
DeviceTypeInfo.name = "Unknown" | ||
// DeviceTypeInfo.logo = path.join("GUI", "DeviceType", "Default.png") | ||
} | ||
|
||
return DeviceTypeInfo; | ||
} | ||
|
||
const getUserLocation = async (ip) => { | ||
|
||
const ipAddress = ip; //IPv6-mapped IPv4 address | ||
console.log("IP : ") | ||
console.log(ipAddress); | ||
const ipv4Address = ipAddress.split(':').pop(); | ||
console.log(ipAddress + " " + ipv4Address); | ||
|
||
try { | ||
const response = await axios.get(`https://ipinfo.io/${ipv4Address}/json`); | ||
const locationData = response.data; | ||
console.log(locationData) | ||
locationData.latitude = locationData.loc.split(',')[0]; | ||
locationData.longitude = locationData.loc.split(',')[1]; | ||
return locationData; | ||
} catch (error) { | ||
// console.error(error); | ||
console.log("req failed") | ||
return null; | ||
} | ||
}; | ||
|
||
module.exports = { getBrowser, getOS, getDeviceType, getUserLocation }; |
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,31 @@ | ||
const { getBrowser, getOS, getDeviceType, getUserLocation } = require('./Analytics'); | ||
|
||
// This module is used to log user activity | ||
async function GetUserActivity(req) { | ||
// const userActivity = { | ||
// timestamp: new Date(), | ||
// method: req.method, | ||
// url: req.url, | ||
// headers: req.rawHeaders, | ||
// clientIP: req.socket.remoteAddress, | ||
// isAuthenticated: req.isAuthenticated(), | ||
// userId: req.user ? req.user.id : null, | ||
// userName: req.user ? req.user.displayName : null, | ||
// // Add other relevant information as needed | ||
// }; | ||
// console.log(userActivity); | ||
// Access user agent information from req.useragent | ||
const userAgentInfo = req.useragent; | ||
// console.log(userAgentInfo); | ||
|
||
let template = { | ||
browserInfo: getBrowser(userAgentInfo.browser), | ||
osInfo: getOS(userAgentInfo), | ||
ip: req.ip, | ||
deviceTypeInfo: getDeviceType(userAgentInfo), | ||
locationData: await getUserLocation(req.ip) | ||
} | ||
console.log(template); | ||
} | ||
|
||
module.exports = { GetUserActivity }; |
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
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
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
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
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,18 @@ | ||
const express = require("express"); //including express package for creating a server | ||
const session = require('express-session'); // Import express-session use to manage sessions | ||
const path = require('path'); | ||
const useragent = require('express-useragent'); | ||
|
||
require('dotenv').config() //including the .env file (for the API keys and DB Credentials) | ||
|
||
const app = express(); | ||
|
||
app.use(session({ secret: process.env.SESSION_SECRET, resave: false, saveUninitialized: true })); //telling express to use sesssion middleware [Secret used to sign the session cookie] | ||
app.use(express.json({ limit: '1mb' })); //telling that my webapp will be sending/recieving data in json format (limiting to 1MB) | ||
app.use(express.static(path.join(__dirname, "..", "ClientSide", "Static"))); //telling that my webapp will be using the files in the ClientSide/Static folder for static files | ||
|
||
app.use(useragent.express()); // use the useragent middleware to parse useragent header | ||
app.set('trust proxy', true); // Enable "trust proxy" to get the client's IP address through proxy headers [setting this makes the load balancer to forward the client's IP address in the X-Forwarded-For header instead of loopback address] | ||
app.set('view engine', 'ejs'); // Setting the view engine to EJS | ||
|
||
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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