-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
835 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
api/prisma/migrations/20240821100937_add_email_password_to_user/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* | ||
Warnings: | ||
- A unique constraint covering the columns `[email]` on the table `User` will be added. If there are existing duplicate values, this will fail. | ||
- Added the required column `email` to the `User` table without a default value. This is not possible if the table is not empty. | ||
- Added the required column `password` to the `User` table without a default value. This is not possible if the table is not empty. | ||
*/ | ||
-- AlterTable | ||
ALTER TABLE "User" ADD COLUMN "email" TEXT NOT NULL, | ||
ADD COLUMN "password" TEXT NOT NULL; | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "User_email_key" ON "User"("email"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,8 @@ router.post( | |
create: { | ||
matomo_id: matomoId, | ||
created_from: "AppMilestonePost", | ||
email: "[email protected]", | ||
password: "password12@Abc", | ||
}, | ||
update: {}, | ||
}); | ||
|
@@ -48,6 +50,8 @@ router.post( | |
create: { | ||
matomo_id: matomoId, | ||
created_from: "AppMilestoneInit", | ||
email: "[email protected]", | ||
password: "password12@Abc", | ||
}, | ||
update: {}, | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,8 @@ router.post( | |
create: { | ||
matomo_id: matomoId, | ||
created_from: "Articles", | ||
email: "[email protected]", | ||
password: "password12@Abc", | ||
}, | ||
update: {}, | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,8 +64,11 @@ router.post( | |
where: { matomo_id: matomoId }, | ||
create: { | ||
matomo_id: matomoId, | ||
email: "[email protected]", | ||
password: "password12@Abc", | ||
created_from: "GetBadges", | ||
}, | ||
|
||
update: {}, | ||
}); | ||
const share_badges = await prisma.badge.findMany({ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,8 @@ router.post( | |
where: { matomo_id: matomoId }, | ||
create: { | ||
matomo_id: matomoId, | ||
email: "[email protected]", | ||
password: "password12@Abc", | ||
}, | ||
update: {}, | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,8 @@ router.post( | |
where: { matomo_id: matomoId }, | ||
create: { | ||
matomo_id: matomoId, | ||
email: "[email protected]", | ||
password: "password12@Abc", | ||
created_from: "Defis", | ||
}, | ||
update: {}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,6 +72,8 @@ router.post( | |
const user = await prisma.user.upsert({ | ||
where: { matomo_id: matomoId }, | ||
create: { | ||
email: "[email protected]", | ||
password: "password12@Abc", | ||
matomo_id: matomoId, | ||
}, | ||
update: {}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,8 @@ router.post( | |
where: { matomo_id: matomoId }, | ||
create: { | ||
matomo_id: matomoId, | ||
email: "[email protected]", | ||
password: "password12@Abc", | ||
created_from: "EventUserSurveyStarted", | ||
}, | ||
update: {}, | ||
|
@@ -71,6 +73,8 @@ router.post( | |
where: { matomo_id: matomoId }, | ||
create: { | ||
matomo_id: matomoId, | ||
email: "[email protected]", | ||
password: "password12@Abc", | ||
created_from: "EventUserSurveyFinished", | ||
}, | ||
update: {}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,8 @@ router.post( | |
where: { matomo_id: matomoId }, | ||
create: { | ||
matomo_id: matomoId, | ||
email: "[email protected]", | ||
password: "password12@Abc", | ||
created_from: "Goal", | ||
goal_isSetup: true, | ||
goal_daysWithGoalNoDrink: daysWithGoalNoDrink, | ||
|
@@ -111,6 +113,8 @@ router.get( | |
where: { matomo_id: matomoId }, | ||
create: { | ||
matomo_id: matomoId, | ||
email: "[email protected]", | ||
password: "password12@Abc", | ||
created_from: "GetGoal", | ||
}, | ||
update: {}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,6 +98,8 @@ router.put( | |
push_notif_token: pushNotifToken, | ||
}, | ||
create: { | ||
email: "[email protected]", | ||
password: "password12@Abc", | ||
push_notif_token: pushNotifToken, | ||
matomo_id: matomoId, | ||
created_from: "Reminder", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,6 +63,8 @@ router.post( | |
let user = await prisma.user.upsert({ | ||
where: { matomo_id: matomoId }, | ||
create: { | ||
email: "[email protected]", | ||
password: "password12@Abc", | ||
matomo_id: matomoId, | ||
created_from: "test", | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,128 @@ const { catchErrors } = require("../middlewares/errors"); | |
const router = express.Router(); | ||
const prisma = require("../prisma"); | ||
const geoip = require("geoip-lite"); | ||
const bcrypt = require("bcrypt"); | ||
const validator = require("validator"); | ||
const { isStrongPassword } = require("validator"); | ||
const jwt = require("jsonwebtoken"); | ||
const { JWT_SECRET } = require("../config"); | ||
|
||
// 1 year | ||
const JWT_MAX_AGE = "365d"; | ||
|
||
function validatePassword(password) { | ||
return isStrongPassword(password, { | ||
minLength: 12, | ||
minLowercase: 1, | ||
minUppercase: 1, | ||
minNumbers: 1, | ||
minSymbols: 1, | ||
}); | ||
} | ||
|
||
router.post( | ||
"/signup", | ||
catchErrors(async (req, res) => { | ||
const { email, password, matomoId } = req.body || {}; | ||
if (!matomoId) return res.status(400).json({ ok: false, error: "no matomo id" }); | ||
|
||
if (!email || !password) { | ||
return res.status(400).json({ ok: false, error: "missing email or password" }); | ||
} | ||
|
||
if (!validator.isEmail(email)) { | ||
return res.status(400).json({ ok: false, error: "invalid email" }); | ||
} | ||
if (!validatePassword(password)) { | ||
return res.status(400).json({ ok: false, error: "password is not strong enough" }); | ||
} | ||
const user = await prisma.user.findUnique({ | ||
where: { email }, | ||
}); | ||
|
||
if (user) { | ||
return res.status(400).json({ ok: false, error: "email already exists" }); | ||
} | ||
|
||
const hashedPassword = await bcrypt.hash(password, 10); | ||
|
||
await prisma.user.upsert({ | ||
where: { matomo_id: matomoId }, | ||
update: updateObj, | ||
create: { | ||
email, | ||
password: hashedPassword, | ||
matomo_id: matomoId, | ||
created_from, | ||
...updateObj, | ||
}, | ||
}); | ||
|
||
const token = jwt.sign({ email }, JWT_SECRET, { | ||
expiresIn: JWT_MAX_AGE, | ||
}); | ||
|
||
return res.status(200).send({ ok: true, token }); | ||
}) | ||
); | ||
|
||
router.post( | ||
"/signin", | ||
catchErrors(async (req, res) => { | ||
const { email, password, matomoId } = req.body || {}; | ||
validator.isEmail(email); | ||
validator.isStrongPassword(password); | ||
console.log("signin", email, password, matomoId); | ||
if (!matomoId) return res.status(400).json({ ok: false, error: "no matomo id" }); | ||
|
||
if (!email || !password) { | ||
return res.status(400).json({ ok: false, error: "missing email or password" }); | ||
} | ||
|
||
const user = await prisma.user.findUnique({ | ||
where: { email }, | ||
}); | ||
|
||
if (!user) { | ||
return res.status(400).json({ ok: false, error: "wrong email or password" }); | ||
} | ||
console.log("user", user); | ||
|
||
// const match = await bcrypt.compare(password, user.password); | ||
const match = password === user.password; | ||
|
||
if (!match) { | ||
return res.status(400).json({ ok: false, error: "wrong email or password" }); | ||
} | ||
|
||
const token = jwt.sign({ email }, JWT_SECRET, { | ||
expiresIn: JWT_MAX_AGE, | ||
}); | ||
|
||
return res.status(200).send({ ok: true, token }); | ||
}) | ||
); | ||
|
||
router.get( | ||
"/signin_token", | ||
catchErrors(async (req, res) => { | ||
const token = req.headers.authorization?.split(" ")[1]; // Bearer token extraction | ||
if (!token) { | ||
return res.status(401).json({ ok: false, error: "No token provided" }); | ||
} | ||
const decoded = jwt.verify(token, JWT_SECRET); | ||
|
||
const user = await prisma.user.findUnique({ | ||
where: { email: decoded.email }, | ||
}); | ||
|
||
if (!user) { | ||
return res.status(400).json({ ok: false, error: "user not found" }); | ||
} | ||
|
||
return res.status(200).send({ ok: true, user, token }); | ||
}) | ||
); | ||
|
||
router.put( | ||
"/", | ||
|
@@ -30,6 +152,8 @@ router.put( | |
update: updateObj, | ||
create: { | ||
matomo_id: matomoId, | ||
email: "[email protected]", | ||
password: "password12@Abc", | ||
created_from, | ||
...updateObj, | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,8 @@ const updateLastConsoAdded = async (matomoId) => { | |
where: { matomo_id: matomoId }, | ||
create: { | ||
matomo_id: matomoId, | ||
email: "[email protected]", | ||
password: "password12@Abc", | ||
lastConsoAdded: dayjs().utc().toDate(), | ||
created_from: "UpdateLastConso", | ||
}, | ||
|
@@ -318,7 +320,7 @@ const scheduleDefi1Day1 = async (matomoId) => { | |
const type = "DEFI1_DAY1"; | ||
const user = await prisma.user.upsert({ | ||
where: { matomo_id: matomoId }, | ||
create: { matomo_id: matomoId, created_from: "SheduleDefiDay1" }, | ||
create: { matomo_id: matomoId, created_from: "SheduleDefiDay1", email: "[email protected]", password: "password12@Abc" }, | ||
update: {}, | ||
}); | ||
|
||
|
@@ -356,7 +358,7 @@ const scheduleUserSurvey = async (matomoId) => { | |
const type = "USER_SURVEY"; | ||
const user = await prisma.user.upsert({ | ||
where: { matomo_id: matomoId }, | ||
create: { matomo_id: matomoId, created_from: "UserSurvey" }, | ||
create: { matomo_id: matomoId, created_from: "UserSurvey", email: "[email protected]", password: "password12@Abc" }, | ||
update: {}, | ||
}); | ||
|
||
|
Oops, something went wrong.