@@ -3,7 +3,7 @@ import isEqual from 'lodash/isEqual';
3
3
import throttle from 'lodash/throttle' ;
4
4
5
5
const settings = window . SETTINGS ;
6
- const accountsUrl = `${ settings . accountHref } /api/user` ;
6
+ const accountsUrl = `${ settings . accountHref } /api/user?always_200=true ` ;
7
7
8
8
function cached ( fn ) {
9
9
let valid = false ;
@@ -64,33 +64,25 @@ const accountsModel = {
64
64
// ]
65
65
// });
66
66
67
- return fetch ( accountsUrl , { credentials : 'include' } )
68
- . then (
69
- ( response ) => {
70
- if ( response . status === 403 ) {
71
- return { } ;
72
- }
73
- return response . json ( ) . then (
74
- ( result ) => {
75
- if ( window . dataLayer ) {
76
- window . dataLayer . push ( {
77
- faculty_status : result . faculty_status
78
- } ) ;
79
- }
80
- return result ;
81
- } ,
82
- ( err ) => {
83
- console . warn ( 'No JSON in Accounts response' ) ;
84
- return { err} ;
85
- }
86
- ) ;
87
- } ,
88
- ( err ) => {
89
- console . warn ( '"Error fetching user info"' ) ;
90
- return { err} ;
67
+ window . _OX_USER_PROMISE ||= fetch ( '/accounts/api/user?always_200=true' , { credentials : 'include' } ) . then (
68
+ ( response ) => {
69
+ if ( response . status === 200 ) {
70
+ return response . json ( ) . catch ( ( error ) => {
71
+ throw new Error ( `Failed to parse user JSON: ${ error . message } ` ) ;
72
+ } ) ;
73
+ } else {
74
+ throw new Error ( `Failed to load user: HTTP ${ response . status } status code` ) ;
91
75
}
92
- )
93
- . catch ( ( err ) => { throw new Error ( `Unable to fetch user data: ${ err } ` ) ; } ) ;
76
+ }
77
+ ) ;
78
+ return window . _OX_USER_PROMISE . then ( ( user ) => {
79
+ if ( window . dataLayer ) {
80
+ window . dataLayer . push ( {
81
+ faculty_status : user . faculty_status
82
+ } ) ;
83
+ }
84
+ return user ;
85
+ } ) ;
94
86
} )
95
87
} ;
96
88
@@ -165,6 +157,7 @@ const userModel = {
165
157
166
158
const throttledLoginCheck = throttle ( ( setData ) => {
167
159
accountsModel . load ( ) . then ( ( oldUser ) => {
160
+ window . _OX_USER_PROMISE = undefined ;
168
161
accountsModel . load . invalidate ( ) ;
169
162
accountsModel . load ( ) . then ( ( newUser ) => {
170
163
if ( ! isEqual ( oldUser , newUser ) ) {
0 commit comments