Skip to content
Snippets Groups Projects
Commit 5596f9e5 authored by Federico Hurtado's avatar Federico Hurtado
Browse files

Able to log successful logins using localhost.

parent ac38ef7f
No related branches found
No related tags found
1 merge request!8Feature/activity logger
// library useful for development // library useful for development
const passport = require("passport"); const passport = require("passport");
const jwt = require("jsonwebtoken"); const jwt = require("jsonwebtoken");
const {
documentSuccessfulLogin,
} = require("../../service/activityLoggerService");
// function to handle the login process // function to handle the login process
const casLogin = function (req, res, next) { const casLogin = async function (req, res, next) {
console.log("Beginning log in...."); console.log("Beginning log in....");
// hard-coded for now. // hard-coded for now.
...@@ -13,6 +16,14 @@ const casLogin = function (req, res, next) { ...@@ -13,6 +16,14 @@ const casLogin = function (req, res, next) {
console.log("user: ", process.env.DB_USER); console.log("user: ", process.env.DB_USER);
if (process.env.DB_USER === "corps_directory_dev") { if (process.env.DB_USER === "corps_directory_dev") {
// document a successful login
try {
const timestamp = new Date(); // Current timestamp
await documentSuccessfulLogin(1, timestamp); // userID = 1 for now?
} catch (error) {
console.error("Error logging the successful login: ", error);
}
// payload for the JWT token // payload for the JWT token
const payload = { const payload = {
id: 1, id: 1,
...@@ -32,7 +43,7 @@ const casLogin = function (req, res, next) { ...@@ -32,7 +43,7 @@ const casLogin = function (req, res, next) {
} }
// call the authenticate function with CAS // call the authenticate function with CAS
passport.authenticate("cas", function (err, user, info) { passport.authenticate("cas", async function (err, user, info) {
if (err) { if (err) {
console.log("found error during authentication: ", err); console.log("found error during authentication: ", err);
return next(err); return next(err);
...@@ -48,7 +59,7 @@ const casLogin = function (req, res, next) { ...@@ -48,7 +59,7 @@ const casLogin = function (req, res, next) {
// create a user object and redirect user // create a user object and redirect user
// to the root route // to the root route
req.logIn(user, function (err) { req.logIn(user, async function (err) {
console.log("Executing log in."); console.log("Executing log in.");
if (err) { if (err) {
return next(err); return next(err);
...@@ -64,6 +75,14 @@ const casLogin = function (req, res, next) { ...@@ -64,6 +75,14 @@ const casLogin = function (req, res, next) {
// sign the token with secret key // sign the token with secret key
const token = jwt.sign(payload, JWT_SECRET, { expiresIn: "1h" }); const token = jwt.sign(payload, JWT_SECRET, { expiresIn: "1h" });
// document a successful login
try {
const timestamp = new Date(); // Current timestamp
await documentSuccessfulLogin(user.id, timestamp);
} catch (error) {
console.error("Error logging the successful login: ", error);
}
// Send the JWT token to the frontend // Send the JWT token to the frontend
console.log("sending JWT To: ", process.env.FRONTEND_URL); console.log("sending JWT To: ", process.env.FRONTEND_URL);
res.redirect(`${process.env.FRONTEND_URL}/home?token=${token}`); res.redirect(`${process.env.FRONTEND_URL}/home?token=${token}`);
......
const { pool } = require("../config/database/database.config");
/*
Repository for login and changelog tables
*/
/*
Add a row to the userLogin table indicating a successful
login.
*/
async function logSuccessfulLogin(userId, timestamp) {
try {
// SQL query to insert the login info
const query = `
INSERT INTO userLogin (eventTimestamp, userId) VALUES (?,?)
`;
// make query
const [result] = await pool.query(query, [timestamp, userId]);
console.log("Successfully logged log in.");
// return the inserted id
return result.insertId;
} catch (error) {
// throw error to be caught by the service
throw error;
}
}
async function getLoginDataTimerange() {}
async function getLoginDataByUser() {}
async function logDatabaseChange() {}
async function getDatabaseChangeTimerange() {}
async function getDatabaseChangeByUser() {}
module.exports = {
logSuccessfulLogin,
};
const { logSuccessfulLogin } = require("../repository/activityRepository");
/*
Function that creates a row of data in the userLogin table
to documenmt a successful log in.
*/
async function documentSuccessfulLogin(userId, timestamp) {
try {
const result = await logSuccessfulLogin(userId, timestamp);
} catch (error) {
// SQL error from the repository
console.log("Error when logging a successful login: ", error);
}
}
async function logDatabaseChange() {}
async function viewLogins() {}
async function viewDatabaseChanges() {}
module.exports = { documentSuccessfulLogin };
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment