Skip to content

Commit

Permalink
Now you can get info from the user from their email
Browse files Browse the repository at this point in the history
  • Loading branch information
MattPlayGamez committed Oct 12, 2024
1 parent e48b7b1 commit 3424cfe
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 7 deletions.
11 changes: 11 additions & 0 deletions file.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 7 additions & 6 deletions file.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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

});


Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -196,8 +199,6 @@ describe('Authenticator Class Tests', () => {
});
afterAll(async () => {
console.log(await authenticator.dumpDB())
fs.rmSync("./app.db")

});

});
12 changes: 12 additions & 0 deletions memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions memory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions mongodb.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 3424cfe

Please sign in to comment.