refactor: migrates user password to it's own table (#13628)

This commit is contained in:
Omar López authored and GitHub committed 2024-02-12 14:13:21 -07:00
1 parent 7702ded85e
commit 7b0d5959cb
25 files changed
+189 -89

No files matched your search

@@ -0,0 +1,31 @@
/*
Warnings:
- You are about to drop the column `password` on the `users` table. All the data in the column will be lost.
*/
-- CreateTable
CREATE TABLE
"UserPassword" (
"hash" TEXT NOT NULL,
"userId" INTEGER NOT NULL
);
-- CreateIndex
CREATE UNIQUE INDEX "UserPassword_userId_key" ON "UserPassword" ("userId");
-- AddForeignKey
ALTER TABLE "UserPassword" ADD CONSTRAINT "UserPassword_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- Inserts current user password into new table
INSERT INTO
"UserPassword" ("hash", "userId")
SELECT
u."password",
u.id
FROM
users u;
-- We rename instead of dropping the column to avoid possible data loss, will be dropped in the next migration.
ALTER TABLE "users"
RENAME COLUMN "password" TO "password_deprecated";
+8 -1
View File
@@ -191,6 +191,13 @@ enum UserPermissionRole {
ADMIN
}
// It holds the password of a User, separate from the User model to avoid leaking the password hash
model UserPassword {
hash String
userId Int @unique
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
}
// It holds Personal Profiles of a User plus it has email, password and other core things
model User {
id Int @id @default(autoincrement())
@@ -199,7 +206,7 @@ model User {
/// @zod.email()
email String
emailVerified DateTime?
password String?
password UserPassword?
bio String?
avatar String?
avatarUrl String?
+15 -3
View File
@@ -78,7 +78,11 @@ async function main() {
insightsAdmin = await prisma.user.create({
data: {
email: "[email protected]",
password: await hashPassword("insightsinsightsinsights...!"),
password: {
create: {
hash: await hashPassword("insightsinsightsinsights...!"),
},
},
name: "Insights Admin",
role: "ADMIN",
username: "insights-admin",
@@ -102,7 +106,11 @@ async function main() {
insightsUser = await prisma.user.create({
data: {
email: "[email protected]",
password: await hashPassword("insightsuser"),
password: {
create: {
hash: await hashPassword("insightsuser"),
},
},
name: "Insights User",
role: "USER",
username: "insights-user",
@@ -415,7 +423,11 @@ async function createPerformanceData() {
const email = `insightsuser${timestamp}@example.com`;
const insightsUser = {
email,
password: await hashPassword("insightsuser"),
password: {
create: {
hash: await hashPassword("insightsuser"),
},
},
name: `Insights User ${timestamp}`,
username: `insights-user-${timestamp}`,
completedOnboarding: true,
+5 -1
View File
@@ -40,7 +40,11 @@ export async function createUserAndEventType({
}) {
const userData = {
...user,
password: await hashPassword(user.password),
password: {
create: {
hash: await hashPassword(user.password),
},
},
emailVerified: new Date(),
completedOnboarding: user.completedOnboarding ?? true,
locale: "en",
+35 -7
View File
@@ -122,7 +122,11 @@ async function createOrganizationAndAddMembersAndTeams({
data: {
...member.memberData,
emailVerified: new Date(),
password: await hashPassword(member.memberData.password),
password: {
create: {
hash: await hashPassword(member.memberData.password.create?.hash || ""),
},
},
},
})),
inTeams: member.inTeams,
@@ -149,7 +153,11 @@ async function createOrganizationAndAddMembersAndTeams({
name: user.name,
email: user.email,
emailVerified: new Date(),
password: await hashPassword(user.username),
password: {
create: {
hash: await hashPassword(user.username),
},
},
},
});
}),
@@ -255,7 +263,11 @@ async function createOrganizationAndAddMembersAndTeams({
await prisma.user.create({
data: {
...nonOrgMember,
password: await hashPassword(nonOrgMember.password),
password: {
create: {
hash: await hashPassword(nonOrgMember.username),
},
},
emailVerified: new Date(),
},
})
@@ -807,7 +819,11 @@ async function main() {
{
memberData: {
email: "[email protected]",
password: "owner1-acme",
password: {
create: {
hash: "owner1-acme",
},
},
username: "owner1-acme",
name: "Owner 1",
},
@@ -836,7 +852,11 @@ async function main() {
nonOrgMembers: [
{
email: "[email protected]",
password: "non-acme-member-1",
password: {
create: {
hash: "non-acme-member-1",
},
},
username: "non-acme-member-1",
name: "NonAcme Member1",
},
@@ -866,7 +886,11 @@ async function main() {
{
memberData: {
email: "[email protected]",
password: "owner1-dunder",
password: {
create: {
hash: "owner1-dunder",
},
},
username: "owner1-dunder",
name: "Owner 1",
},
@@ -895,7 +919,11 @@ async function main() {
nonOrgMembers: [
{
email: "[email protected]",
password: "non-dunder-member-1",
password: {
create: {
hash: "non-dunder-member-1",
},
},
username: "non-dunder-member-1",
name: "NonDunder Member1",
},