diff --git a/app/(auth)/lib/is-route06-user.ts b/app/(auth)/lib/is-route06-user.ts index cb536463..e5797f31 100644 --- a/app/(auth)/lib/is-route06-user.ts +++ b/app/(auth)/lib/is-route06-user.ts @@ -1,8 +1,7 @@ "use server"; import { getUser } from "@/lib/supabase"; - -const R06_EMAIL_DOMAIN = "route06.co.jp"; +import { isEmailFromRoute06 } from "@/lib/utils"; export const isRoute06User = async () => { const supabaseUser = await getUser(); @@ -12,6 +11,5 @@ export const isRoute06User = async () => { throw new Error("No email found for user"); } - const emailDomain = email.split("@")[1]; - return emailDomain === R06_EMAIL_DOMAIN; + return isEmailFromRoute06(email); }; diff --git a/middleware.ts b/middleware.ts index 4fc00198..89b7ee4d 100644 --- a/middleware.ts +++ b/middleware.ts @@ -1,4 +1,4 @@ -import { retrieveStripeSubscriptionBySupabaseUserId } from "@/services/accounts/actions"; +import { retrieveActiveStripeSubscriptionBySupabaseUserId } from "@/services/accounts/actions"; import { NextResponse } from "next/server"; import { supabaseMiddleware } from "./lib/supabase"; import { isEmailFromRoute06 } from "./lib/utils"; @@ -13,7 +13,7 @@ export default supabaseMiddleware(async (user, request) => { // Proceeding to check the user's subscription status since the email is not from the route06.co.jp if (!isEmailFromRoute06(user.email ?? "")) { - const subscription = await retrieveStripeSubscriptionBySupabaseUserId( + const subscription = await retrieveActiveStripeSubscriptionBySupabaseUserId( user.id, ); if (subscription == null) { diff --git a/services/accounts/actions.ts b/services/accounts/actions.ts index 28692260..2f5e531b 100644 --- a/services/accounts/actions.ts +++ b/services/accounts/actions.ts @@ -1 +1 @@ -export { retrieveStripeSubscriptionBySupabaseUserId } from "./actions/retrieve-stripe-subscription"; +export { retrieveActiveStripeSubscriptionBySupabaseUserId } from "./actions/retrieve-stripe-subscription"; diff --git a/services/accounts/actions/retrieve-stripe-subscription.ts b/services/accounts/actions/retrieve-stripe-subscription.ts index d56f4c91..c4265e96 100644 --- a/services/accounts/actions/retrieve-stripe-subscription.ts +++ b/services/accounts/actions/retrieve-stripe-subscription.ts @@ -1,17 +1,15 @@ import { db, organizations, - stripeUserMappings, subscriptions, supabaseUserMappings, teamMemberships, teams, users, } from "@/drizzle"; -import { stripe } from "@/services/external/stripe"; -import { eq } from "drizzle-orm"; +import { and, eq } from "drizzle-orm"; -export const retrieveStripeSubscriptionBySupabaseUserId = async ( +export const retrieveActiveStripeSubscriptionBySupabaseUserId = async ( supabaseUserId: string, ) => { const [subscription] = await db @@ -28,6 +26,11 @@ export const retrieveStripeSubscriptionBySupabaseUserId = async ( supabaseUserMappings, eq(supabaseUserMappings.userDbId, users.dbId), ) - .where(eq(supabaseUserMappings.supabaseUserId, supabaseUserId)); + .where( + and( + eq(supabaseUserMappings.supabaseUserId, supabaseUserId), + eq(subscriptions.status, "active"), + ), + ); return subscription; };