Skip to content

Commit

Permalink
feat: login front
Browse files Browse the repository at this point in the history
  • Loading branch information
YoanRos committed Aug 23, 2024
1 parent aa71ad9 commit 408bf10
Show file tree
Hide file tree
Showing 27 changed files with 835 additions and 30 deletions.
5 changes: 4 additions & 1 deletion api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@prisma/client": "5.16.2",
"@sentry/node": "6.19.6",
"@sentry/tracing": "6.17.6",
"bcrypt": "5.1.1",
"cors": "2.8.5",
"cross-env": "7.0.3",
"data-forge": "1.10.2",
Expand All @@ -38,12 +39,14 @@
"express": "4.18.2",
"geoip-lite": "1.4.10",
"helmet": "4.0.0",
"jsonwebtoken": "9.0.2",
"morgan": "1.10.0",
"node-cron": "3.0.2",
"node-fetch": "2.6.7",
"node-pushnotifications": "^3.1.1",
"prisma": "5.16.2",
"uuid": "^10.0.0"
"uuid": "^10.0.0",
"validator": "13.12.0"
},
"devDependencies": {
"@types/node": "^20.14.10",
Expand Down
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");
2 changes: 2 additions & 0 deletions api/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ datasource db {
model User {
id String @id @default(uuid())
matomo_id String @unique
email String @unique
password String
push_notif_token String @default("")
reminder Reminder?
notifications Notification[]
Expand Down
2 changes: 2 additions & 0 deletions api/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const MATOMO_URL = process.env.MATOMO_URL;
const MATOMO_IDSITE_1 = process.env.MATOMO_IDSITE_1;
const METABASE_ACCOUNT = process.env.METABASE_ACCOUNT;
const METABASE_PASSWORD = process.env.METABASE_PASSWORD;
const JWT_SECRET = process.env.JWT_SECRET;

module.exports = {
PORT,
Expand All @@ -46,4 +47,5 @@ module.exports = {
MATOMO_IDSITE_1,
METABASE_ACCOUNT,
METABASE_PASSWORD,
JWT_SECRET,
};
4 changes: 4 additions & 0 deletions api/src/controllers/appMilestone.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ router.post(
create: {
matomo_id: matomoId,
created_from: "AppMilestonePost",
email: "[email protected]",
password: "password12@Abc",
},
update: {},
});
Expand Down Expand Up @@ -48,6 +50,8 @@ router.post(
create: {
matomo_id: matomoId,
created_from: "AppMilestoneInit",
email: "[email protected]",
password: "password12@Abc",
},
update: {},
});
Expand Down
2 changes: 2 additions & 0 deletions api/src/controllers/articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ router.post(
create: {
matomo_id: matomoId,
created_from: "Articles",
email: "[email protected]",
password: "password12@Abc",
},
update: {},
});
Expand Down
3 changes: 3 additions & 0 deletions api/src/controllers/badge.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
2 changes: 2 additions & 0 deletions api/src/controllers/consommation.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ router.post(
where: { matomo_id: matomoId },
create: {
matomo_id: matomoId,
email: "[email protected]",
password: "password12@Abc",
},
update: {},
});
Expand Down
2 changes: 2 additions & 0 deletions api/src/controllers/defis.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ router.post(
where: { matomo_id: matomoId },
create: {
matomo_id: matomoId,
email: "[email protected]",
password: "password12@Abc",
created_from: "Defis",
},
update: {},
Expand Down
2 changes: 2 additions & 0 deletions api/src/controllers/drinksContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {},
Expand Down
4 changes: 4 additions & 0 deletions api/src/controllers/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ router.post(
where: { matomo_id: matomoId },
create: {
matomo_id: matomoId,
email: "[email protected]",
password: "password12@Abc",
created_from: "EventUserSurveyStarted",
},
update: {},
Expand Down Expand Up @@ -71,6 +73,8 @@ router.post(
where: { matomo_id: matomoId },
create: {
matomo_id: matomoId,
email: "[email protected]",
password: "password12@Abc",
created_from: "EventUserSurveyFinished",
},
update: {},
Expand Down
4 changes: 4 additions & 0 deletions api/src/controllers/goal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -111,6 +113,8 @@ router.get(
where: { matomo_id: matomoId },
create: {
matomo_id: matomoId,
email: "[email protected]",
password: "password12@Abc",
created_from: "GetGoal",
},
update: {},
Expand Down
2 changes: 2 additions & 0 deletions api/src/controllers/reminder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions api/src/controllers/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
Expand Down
124 changes: 124 additions & 0 deletions api/src/controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"/",
Expand Down Expand Up @@ -30,6 +152,8 @@ router.put(
update: updateObj,
create: {
matomo_id: matomoId,
email: "[email protected]",
password: "password12@Abc",
created_from,
...updateObj,
},
Expand Down
6 changes: 4 additions & 2 deletions api/src/utils/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
Expand Down Expand Up @@ -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: {},
});

Expand Down Expand Up @@ -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: {},
});

Expand Down
Loading

0 comments on commit 408bf10

Please sign in to comment.