Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api: update login endpoint to detect possible user migrations #162

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- AlterTable
ALTER TABLE "public"."user_profile" ADD COLUMN "legacy_profile_migrated_at" TIMESTAMP(3);

-- CreateIndex
CREATE INDEX "user_profile_legacy_email_legacy_email_verified_at_idx" ON "public"."user_profile"("legacy_email", "legacy_email_verified_at");
8 changes: 5 additions & 3 deletions apps/api/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,10 @@ model UserProfile {
showGemsBalance Boolean @default(true) @map("show_gems_balance")

// Migration
legacyAddress String? @unique @map("legacy_address") @db.VarChar(42)
legacyEmail String? @map("legacy_email")
legacyEmailVerifiedAt DateTime? @map("legacy_email_verified_at")
legacyAddress String? @unique @map("legacy_address") @db.VarChar(42)
legacyEmail String? @map("legacy_email")
legacyEmailVerifiedAt DateTime? @map("legacy_email_verified_at")
legacyProfileMigratedAt DateTime? @map("legacy_profile_migrated_at")
alecananian marked this conversation as resolved.
Show resolved Hide resolved

// Status
testnetFaucetLastUsedAt DateTime? @map("testnet_faucet_last_used_at")
Expand All @@ -109,6 +110,7 @@ model UserProfile {
updatedAt DateTime @updatedAt @map("updated_at")

@@index([legacyAddress])
@@index([legacyEmail, legacyEmailVerifiedAt])
@@index([tag])
@@map("user_profile")
@@schema("public")
Expand Down
47 changes: 38 additions & 9 deletions apps/api/src/routes/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
import type { TdkApiContext } from "../types";
import {
USER_PROFILE_SELECT_FIELDS,
USER_PUBLIC_PROFILE_SELECT_FIELDS,
USER_SMART_ACCOUNT_INCLUDE_FIELDS,
} from "../utils/db";
import { throwUnauthorizedError } from "../utils/error";
Expand Down Expand Up @@ -172,15 +173,38 @@ export const authRoutes =
}

const { user } = userSmartAccount;
const profile = await db.userProfile.upsert({
where: { userId: user.id },
update: {},
create: {
userId: user.id,
email: userSmartAccount.initialEmail,
},
select: USER_PROFILE_SELECT_FIELDS,
});
const [profile, legacyUserProfiles] = await Promise.all([
// Fetch or create user profile
db.userProfile.upsert({
where: { userId: user.id },
update: {},
create: {
userId: user.id,
email: userSmartAccount.initialEmail,
},
select: USER_PROFILE_SELECT_FIELDS,
}),
// Detect any migrations related to this user
userSmartAccount.initialEmail
? db.userProfile.findMany({
where: {
legacyEmail: userSmartAccount.initialEmail,
legacyEmailVerifiedAt: { not: null },
},
select: {
...USER_PUBLIC_PROFILE_SELECT_FIELDS,
id: true,
},
})
: userSmartAccount.initialWalletAddress
? db.userProfile.findUnique({
where: {
legacyAddress: userSmartAccount.initialWalletAddress,
},
select: { ...USER_PUBLIC_PROFILE_SELECT_FIELDS, id: true },
})
: [],
]);

const [authTokenResult, userSessionsResult] = await Promise.allSettled([
auth.generateJWT<UserContext>(address, {
Expand Down Expand Up @@ -232,6 +256,11 @@ export const authRoutes =
address,
sessions,
},
legacyProfiles: legacyUserProfiles
? Array.isArray(legacyUserProfiles)
? legacyUserProfiles
: [legacyUserProfiles]
: [],
});
},
);
Expand Down
9 changes: 9 additions & 0 deletions apps/api/src/schema/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
EXAMPLE_WALLET_ADDRESS,
sessionSchema,
userProfileSchema,
userPublicProfileSchema,
userSchema,
} from "./shared";

Expand Down Expand Up @@ -45,6 +46,14 @@ export const loginReplySchema = Type.Object({
sessions: Type.Array(sessionSchema),
}),
]),
legacyProfiles: Type.Array(
Type.Intersect([
Type.Object({
id: Type.String(),
}),
userPublicProfileSchema,
]),
),
});

export const loginCustomBodySchema = Type.Object({
Expand Down