55const axios = require ( 'axios' ) ;
66const crypto = require ( 'crypto' ) ;
77const { normalizeAxiosError, normalizeVerifyPayload } = require ( './licensechainApiNormalize' ) ;
8+ const { getLinkedUser } = require ( './DashboardClient' ) ;
89
910class LicenseChainClient {
1011 constructor ( config ) {
@@ -79,6 +80,12 @@ class LicenseChainClient {
7980 */
8081 async getUserLicenses ( userId ) {
8182 try {
83+ const linked = await getLinkedUser ( userId ) ;
84+ const linkedEmail = ( linked ?. email || '' ) . trim ( ) . toLowerCase ( ) ;
85+ if ( ! linkedEmail ) {
86+ throw new Error ( 'DISCORD_ACCOUNT_NOT_LINKED' ) ;
87+ }
88+
8289 // Get all app licenses and filter by user
8390 const appName = process . env . LICENSECHAIN_APP_NAME ;
8491 if ( ! appName ) {
@@ -98,10 +105,11 @@ class LicenseChainClient {
98105 const licensesData = await this . getAppLicenses ( appId ) ;
99106 const allLicenses = licensesData ?. licenses || licensesData || [ ] ;
100107
101- // Filter licenses by userId (email or issuedTo matching)
102- // Note: This is a simplified filter - adjust based on your data structure
108+ // Scope licenses by linked dashboard email to avoid discord-id/email drift.
103109 return allLicenses . filter ( license => {
104- return license . issuedEmail === userId || license . issuedTo === userId || license . email === userId ;
110+ const issuedEmail = ( license ?. issuedEmail || '' ) . trim ( ) . toLowerCase ( ) ;
111+ const email = ( license ?. email || '' ) . trim ( ) . toLowerCase ( ) ;
112+ return issuedEmail === linkedEmail || email === linkedEmail ;
105113 } ) ;
106114 } catch ( error ) {
107115 throw new Error ( `Failed to get user licenses: ${ error . response ?. data ?. message || error . message } ` ) ;
0 commit comments