Skip to content

Commit 75b286f

Browse files
author
Adam Robins
committed
Update
1 parent d720b64 commit 75b286f

File tree

7 files changed

+57
-38
lines changed

7 files changed

+57
-38
lines changed

.env

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
SECRET_KEY=KEY
1+
SECRET_KEY=KEY
2+
MONGODB_URI=mongodb://127.0.0.1:27017/benderirc

main.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ app.use("/login", Express.static("public/login/login.html"));
5555

5656
app.post("/join/dm/:nick", isLoggedIn, async (req, res) => {
5757
const nick = req.params.nick;
58-
var dms = await MongooseDal.getDirectMessagesForUser(UserSettings.nick, nick);
58+
var dms = await MongooseDal.getDirectMessagesForUser("tes", nick);
5959
res.send({ nick: nick, messages: dms?.messages || [] });
6060
});
6161

@@ -75,7 +75,7 @@ app.post("/channel/join", isLoggedIn, async (req, res) => {
7575
active: true,
7676
name: req.body.channel,
7777
description: "Test Channel",
78-
owner: UserSettings.nick, // TODO: This should ideally be the logged-in user's nick
78+
owner: "UserSettings.nick", // TODO: This should ideally be the logged-in user's nick
7979
created_at: new Date(),
8080
updated_at: new Date(),
8181
messages: [],

models/user.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,5 @@ const userSchema = new Schema<IUser>({
3838

3939
const User = model<IUser>('User', userSchema);
4040

41-
export default User;
41+
export default User;
42+
export { IUser }; // Export interfaces for use in other modules

public/login/login.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ <h1 class="text-xl mb-8">Login</h1>
4242
var password = document.getElementById('password').value;
4343

4444
// POST with axios to /login
45-
axios.post('http://127.0.0.1:3000/user/login', {
45+
axios.post('http://127.0.0.1:3000/api/user/login', {
4646
username: username,
4747
password: password
4848
}).then((response) => {

router/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import express from 'express';
2-
import express from 'express';
32
// import { channelRouter } from './channel'; // Not currently used
43
import { nickservRouter } from './nickserv';
54
import { chanservRouter } from './chanserv';

services/mongo.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,34 @@
11
// Mongoose controller
22
import mongoose from "mongoose";
3+
import { log } from "mercedlogger";
34
import { Channel, DirectMessages, IChannel, IMessage } from "../models/channel";
45
import { MessageQueue, IMessageQueue } from "../models/messageQueue";
56

67
// MONGODB MONGOOS DAL CLASS
78
const MongooseDal = {
89
connect: async () => {
9-
return await mongoose.connect("mongodb://127.0.0.1:27017/benderirc");
10+
const defaultURI = "mongodb://127.0.0.1:27017/benderirc";
11+
let connectionString = process.env.MONGODB_URI;
12+
13+
if (connectionString) {
14+
log.red("MongoDB URI found in environment variables.");
15+
} else {
16+
connectionString = defaultURI;
17+
log.red(`MONGODB_URI not set in environment. Using default: ${connectionString}`);
18+
}
19+
20+
// Basic sanitization to avoid logging credentials if present in the URI
21+
const safeLogURI = connectionString.includes('@') ? `mongodb://${connectionString.split('@').pop()}` : connectionString;
22+
log.cyan(`Attempting to connect to MongoDB at ${safeLogURI}...`);
23+
24+
try {
25+
await mongoose.connect(connectionString);
26+
log.cyan("MongoDB Connected Successfully to " + safeLogURI);
27+
} catch (error) {
28+
log.red("MongoDB Connection Error:", error.message);
29+
// Re-throw the error or handle it as per application's error handling strategy
30+
throw error;
31+
}
1032
},
1133
createChannel: async (channel: IChannel) => {
1234
// Create only if the channel doesn't have the same name

services/userService.ts

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import dotenv from 'dotenv';
22
dotenv.config();
33

4-
// import { SaveUser, GetUser, UserModel } from '../models/userSchema'; // Temporarily commented out
4+
import User, { IUser } from '../models/user';
55
import bcrypt from "bcryptjs";
66
import jwt from "jsonwebtoken";
77
import { log } from "mercedlogger";
@@ -10,43 +10,39 @@ const SECRET_KEY = process.env.SECRET_KEY ? process.env.SECRET_KEY : "SECRET_KEY
1010
log.magenta("SECRET KEY FOUND : " + SECRET_KEY);
1111

1212
const register = async (req, callback) => {
13-
// try {
14-
// const { username, email, password } = req;
15-
// const pw = await bcrypt.hash(password, 12);
16-
// const user = new UserModel(username, email, pw); // UserModel is from the deleted file
17-
// const token = createJWT(user);
18-
// log.magenta("REGISTER", 'User Created Successfully' + username);
19-
// await SaveUser(user); // SaveUser is from the deleted file
13+
try {
14+
const { username, email, password } = req;
15+
const pw = await bcrypt.hash(password, 12);
16+
const user = new User({ username, email, password: pw });
17+
const token = createJWT(user);
18+
log.magenta("REGISTER", 'User Created Successfully' + username);
19+
await user.save();
2020

21-
// callback(null, { token });
22-
// } catch (error) {
23-
// log.red("REGISTER", 'Could not register' + error);
24-
// callback({ error: error.message }, null);
25-
// }
26-
log.warn("userService.register is currently disabled due to missing models/userSchema.ts");
27-
callback({ error: "Registration is temporarily disabled." }, null);
21+
callback(null, { token });
22+
} catch (error) {
23+
log.red("REGISTER", 'Could not register' + error);
24+
callback({ error: error.message }, null);
25+
}
2826
}
2927

3028
const login = async (req, callback) => {
31-
// try {
32-
// const { username, password } = req;
33-
// const user = await GetUser(username); // GetUser is from the deleted file
34-
// if (await bcrypt.compare(password, user.password)) {
35-
// const token = createJWT(user);
36-
// callback(null, { token: token, username: user.username });
37-
// } else {
38-
// callback({ error: "Invalid Credentials" });
39-
// }
40-
// } catch (error) {
41-
// callback({ error: error.message }, null);
42-
// }
43-
log.warn("userService.login is currently disabled due to missing models/userSchema.ts");
44-
callback({ error: "Login is temporarily disabled." }, null);
29+
try {
30+
const { username, password } = req;
31+
const user = await User.findOne({ username: username });
32+
if (user && await bcrypt.compare(password, user.password)) {
33+
const token = createJWT(user);
34+
callback(null, { token: token, username: user.username });
35+
} else {
36+
callback({ error: "Invalid Credentials" });
37+
}
38+
} catch (error) {
39+
callback({ error: error.message }, null);
40+
}
4541
}
4642

47-
const createJWT = (user) => { // user type here was UserModel, will need adjustment if used
43+
const createJWT = (user: IUser) => {
4844
return jwt.sign(
49-
{ user }, // This 'user' object structure might need to align with actual User model from models/user.ts
45+
{ user },
5046
SECRET_KEY,
5147
{ expiresIn: "24h" }
5248
);

0 commit comments

Comments
 (0)