Skip to content

Commit

Permalink
Minor
Browse files Browse the repository at this point in the history
  • Loading branch information
msveshnikov committed Apr 29, 2024
1 parent a4169d2 commit 598fc6a
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 11 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"@mui/icons-material": "^5.15.15",
"@mui/material": "^5.15.15",
"html-to-pdfmake": "^2.5.7",
"i18next": "^23.11.2",
"i18next": "^23.11.3",
"i18next-browser-languagedetector": "^7.2.1",
"markdown-it": "^14.1.0",
"md5": "^2.3.0",
Expand Down
16 changes: 14 additions & 2 deletions server/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,23 @@ export const completePasswordReset = async (token, password) => {
}
};

export const registerUser = async (email, password) => {
export const getIpFromRequest = (req) => {
let ips = (req.headers["x-real-ip"] || req.headers["x-forwarded-for"] || req.connection.remoteAddress || "").split(
","
);
return ips[0].trim();
};

export const registerUser = async (email, password, req) => {
try {
const salt = await bcrypt.genSalt(10);
const hashedPassword = await bcrypt.hash(password, salt);
const user = new User({ email, password: hashedPassword });
const user = new User({
email,
password: hashedPassword,
userAgent: req.headers["user-agent"],
ip: getIpFromRequest(req),
});
await user.save();
sendWelcomeEmail(user);
return { success: true };
Expand Down
2 changes: 1 addition & 1 deletion server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ app.post("/interact", verifyToken, async (req, res) => {

app.post("/register", async (req, res) => {
const { email, password } = req.body;
const result = await registerUser(email, password);
const result = await registerUser(email, password, req);
if (result.success) {
res.status(200).json({ message: "Registration successful" });
} else {
Expand Down
6 changes: 6 additions & 0 deletions server/model/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ const userSchema = new mongoose.Schema({
email: { type: String, required: true, unique: true },
password: { type: String, required: true },
admin: { type: Boolean, required: false },
ip: {
type: String,
required: true,
unique: [true, "Cannot signup with the same IP"],
},
userAgent: { type: String, required: false },
usageStats: {
gemini: {
inputTokens: { type: Number, default: 0 },
Expand Down
2 changes: 1 addition & 1 deletion src/components/AppHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const AppHeader = ({
<ProfileMenu userEmail={userEmail} onSettings={onSettings} onSignOut={onSignOut} />
) : (
<Box component="span" onClick={onOpenAuthModal} sx={{ cursor: "pointer" }}>
{t("Login")}
<Typography>{t("Login")}</Typography>
</Box>
)}
</Box>
Expand Down

0 comments on commit 598fc6a

Please sign in to comment.