From 3424cfecb2fc5b0db57c1263ecbf8f4c36c43f57 Mon Sep 17 00:00:00 2001 From: MattPlayGamez <62027538+MattPlayGamez@users.noreply.github.com> Date: Sat, 12 Oct 2024 15:42:44 +0200 Subject: [PATCH] Now you can get info from the user from their email --- file.js | 11 +++++++++++ file.test.js | 13 +++++++------ memory.js | 12 ++++++++++++ memory.test.js | 5 +++++ mongodb.js | 9 +++++++++ mongodb.test.js | 5 +++++ package.json | 2 +- 7 files changed, 50 insertions(+), 7 deletions(-) diff --git a/file.js b/file.js index 8708a35..0c8cbe2 100644 --- a/file.js +++ b/file.js @@ -223,7 +223,18 @@ class Authenticator { if (!user) return null; return user } + /** + * Retrieves user information based on the user email + * @param {string} email - the email to retrieve information + * @returns {object} - an object with the user information + * @throws {Error} - any error that occurs during the process + */ + getInfoFromEmail(email) { + const user = this.users.find(u => u.email === email); + if (!user) return null; + return user + } /** * Verifies a JWT token and returns the user information if the token is valid * @param {string} token - the JWT token to verify diff --git a/file.test.js b/file.test.js index 36c072e..c203239 100644 --- a/file.test.js +++ b/file.test.js @@ -2,9 +2,6 @@ require("dotenv/config") const Authenticator = require('./file.js') const jwt = require('jsonwebtoken'); const speakeasy = require('speakeasy'); -const fs = require('fs'); - - const mockUser = { @@ -32,9 +29,10 @@ describe('Authenticator Class Tests', () => { beforeAll(async () => { authenticator = new Authenticator( - 'TestApp', 10, JWT_SECRET, { expiresIn: '1h' }, 3, "./app.db", "SECRETTTT" + 'TestApp', 10, JWT_SECRET, { expiresIn: '1h' }, 3, "app.db", "password123" ); authenticator.ALLOW_DB_DUMP = true + }); @@ -101,6 +99,11 @@ describe('Authenticator Class Tests', () => { expect(info.email).toBe(mockUser.email); }) + test('Get Info From Email', async () => { + const info = await authenticator.getInfoFromEmail(mockUser.email) + expect(info.email).toBe(mockUser.email); + }) + test('Verify JWT Token', async () => { const loginResult = await authenticator.login(mockUser.email, mockUser.password); const tokenVerification = await authenticator.verifyToken(loginResult.jwt_token); @@ -196,8 +199,6 @@ describe('Authenticator Class Tests', () => { }); afterAll(async () => { console.log(await authenticator.dumpDB()) - fs.rmSync("./app.db") - }); }); diff --git a/memory.js b/memory.js index a3abcbf..de95a6f 100644 --- a/memory.js +++ b/memory.js @@ -168,6 +168,18 @@ class Authenticator { return user } + /** + * Retrieves user information based on the user email + * @param {string} email - the user email to retrieve information + * @returns {object} - an object with the user information + * @throws {Error} - any error that occurs during the process + */ + getInfoFromEmail(email) { + const user = this.users.find(u => u.email === email); + if (!user) return null; + return user + } + /** * Verifies a JWT token and returns the user information if the token is valid * @param {string} token - the JWT token to verify diff --git a/memory.test.js b/memory.test.js index 265936b..fb51d9f 100644 --- a/memory.test.js +++ b/memory.test.js @@ -99,6 +99,11 @@ describe('Authenticator Class Tests', () => { expect(info.email).toBe(mockUser.email); }) + test('Get Info From Email', async () => { + const info = await authenticator.getInfoFromEmail(mockUser.email) + expect(info.email).toBe(mockUser.email); + }) + test('Verify JWT Token', async () => { const loginResult = await authenticator.login(mockUser.email, mockUser.password); const tokenVerification = await authenticator.verifyToken(loginResult.jwt_token); diff --git a/mongodb.js b/mongodb.js index 7d40ffc..7f61f42 100644 --- a/mongodb.js +++ b/mongodb.js @@ -186,6 +186,15 @@ class Authenticator { async getInfoFromUser(userId) { return await this.User.findOne({ _id: userId }); } + /** + * Retrieves user information based on the user's email address + * @param {string} email - the email address to retrieve information + * @returns {object} - an object with the user information + * @throws {Error} - any error that occurs during the process + */ + async getInfoFromEmail(email) { + return await this.User.findOne({ email: email }); + } /** * Verifies a JWT token and returns the user information if the token is valid diff --git a/mongodb.test.js b/mongodb.test.js index 272838a..7a45f81 100644 --- a/mongodb.test.js +++ b/mongodb.test.js @@ -107,6 +107,11 @@ describe('Authenticator Class Tests', () => { expect(info.email).toBe(mockUser.email); }) + test('Get Info From Email', async () => { + const info = await authenticator.getInfoFromEmail(mockUser.email) + expect(info.email).toBe(mockUser.email); + }) + test('Verify JWT Token', async () => { const loginResult = await authenticator.login(mockUser.email, mockUser.password); const tokenVerification = await authenticator.verifyToken(loginResult.jwt_token); diff --git a/package.json b/package.json index 9f83155..1bb809c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "seamless-auth", - "version": "3.8.0", + "version": "3.8.1", "description": "A full fledged authentication system...", "type": "commonjs", "main": "memory.js",