Skip to content

Commit

Permalink
convert subscribers.js
Browse files Browse the repository at this point in the history
  • Loading branch information
codemist committed Aug 26, 2024
1 parent cf46025 commit c39bece
Show file tree
Hide file tree
Showing 4 changed files with 290 additions and 258 deletions.
9 changes: 6 additions & 3 deletions src/app/api/utils/auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export const authOptions: AuthOptions = {
account.access_token,
account.refresh_token,
account.expires_at ?? 0,
JSON.stringify(profile),
profile,
);
// MNTOR-2599 The breach_resolution object can get pretty big,
// causing the session token cookie to balloon in size,
Expand Down Expand Up @@ -272,8 +272,11 @@ export const authOptions: AuthOptions = {
// MNTOR-2599 The breach_resolution object can get pretty big,
// causing the session token cookie to balloon in size,
// eventually resulting in a 400 Bad Request due to headers being too large.
delete updatedUser.breach_resolution;
token.subscriber = updatedUser;

if (updatedUser) {
delete updatedUser.breach_resolution;
token.subscriber = updatedUser;
}
} catch (error) {
logger.error("refresh_access_token", error);
// The error property can be used client-side to handle the refresh token error
Expand Down
22 changes: 3 additions & 19 deletions src/app/functions/server/changeSubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

import { SubscriberRow } from "knex/types/tables";
import { updateFxAProfileData } from "../../../db/tables/subscribers";
import { Profile } from "next-auth";

const MONITOR_PREMIUM_CAPABILITY = "monitor";

export async function changeSubscription(
subscriber: SubscriberRow,
enabled: boolean,
) {
const currentFxAProfile = subscriber?.fxa_profile_json as FxaProfile;
const currentFxAProfile = subscriber?.fxa_profile_json as Profile;
let subscriptions = currentFxAProfile.subscriptions ?? [];

if (enabled) {
Expand All @@ -26,22 +27,5 @@ export async function changeSubscription(
}

currentFxAProfile.subscriptions = subscriptions;
await updateFxAProfileData(subscriber, JSON.stringify(currentFxAProfile));
await updateFxAProfileData(subscriber, currentFxAProfile);
}

/**
* See https://github.com/mozilla/fxa/blob/564949dfc69f0f675ebb4e5f267282c2546a5767/packages/fxa-profile-server/lib/routes/profile.js#L63-L77
*/
type FxaProfile = {
email?: string;
uid?: string;
avatar?: string;
avatarDefault?: boolean;
displayName?: string;
locale?: string;
amrValues?: string[];
twoFactorAuthentication?: boolean;
subscriptions?: string[];
metricsEnabled?: boolean;
sub?: string;
};
Loading

0 comments on commit c39bece

Please sign in to comment.