@@ -18,8 +18,8 @@ const NewspapersResponseSchema = z.object({
18
18
url : z . string ( ) ,
19
19
} ) ;
20
20
21
- const UserAttributesSchema = z . object ( {
22
- contentAccess : z . record ( z . string ( ) , z . boolean ( ) ) ,
21
+ const userBenefitsSchema = z . object ( {
22
+ benefits : z . array ( z . string ( ) ) ,
23
23
} ) ;
24
24
25
25
type NewspaperArchiveConfig = {
@@ -47,13 +47,16 @@ router.get('/auth', async (req: Request, res: Response) => {
47
47
return res . sendStatus ( 500 ) ;
48
48
}
49
49
50
+ console . log ( 'Checking supporter entitlement' ) ;
50
51
const hasCorrectEntitlement = await checkSupporterEntitlement ( req ) ;
51
52
52
53
if ( ! hasCorrectEntitlement ) {
53
54
// ToDo: show the user an error/info page
55
+ console . log ( 'User does not have the newspaper archive entitlement' ) ;
54
56
return res . redirect ( '/' ) ;
55
57
}
56
58
59
+ console . log ( 'User has the newspaper archive entitlement' ) ;
57
60
const authHeader = base64 ( `${ authString } ` ) ;
58
61
const requestBody : NewspapersRequestBody = { } ;
59
62
@@ -107,22 +110,16 @@ router.get('/auth', async (req: Request, res: Response) => {
107
110
export { router } ;
108
111
109
112
async function checkSupporterEntitlement ( req : Request ) : Promise < boolean > {
110
- const supporterAttributesResponse = await getSupporterStatus ( req ) ;
111
- const supporterAttributes = UserAttributesSchema . parse (
112
- await supporterAttributesResponse . json ( ) ,
113
- ) ;
114
-
115
- // ToDo: this should return a flag that represents either Tier 3 or a newspaperArchive specific entitlement
116
- return (
117
- supporterAttributes . contentAccess [ 'guardianWeeklySubscriber' ] &&
118
- supporterAttributes . contentAccess [ 'supporterPlus' ]
119
- ) ;
113
+ const supporterAttributes = await getSupporterStatus ( req )
114
+ . then ( ( res ) => res . json ( ) )
115
+ . then ( ( json ) => userBenefitsSchema . parse ( json ) ) ;
116
+ return supporterAttributes . benefits . includes ( 'newspaperArchive' ) ;
120
117
}
121
118
122
119
async function getSupporterStatus ( req : Request ) {
123
- const host = 'members-data-api .' + conf . DOMAIN ;
120
+ const host = 'user-benefits .' + conf . API_DOMAIN ;
124
121
125
- return fetch ( `https://${ host } /user-attributes /me` , {
122
+ return fetch ( `https://${ host } /benefits /me` , {
126
123
method : 'GET' ,
127
124
headers : {
128
125
...( await authorizationOrCookieHeader ( { req, host } ) ) ,
0 commit comments