@schibsted/account-sdk-browser 5.2.3 → 5.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.md CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2024 Schibsted Products & Technology AS
1
+ Copyright (c) 2026 Schibsted Products & Technology AS
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
4
4
  associated documentation files (the "Software"), to deal in the Software without restriction,
package/README.md CHANGED
@@ -61,23 +61,38 @@ You can use that code as inspiration or just fork and play with it. The account-
61
61
  module is used for authenticating the user with Schibsted account. Take a look at how the SDK is
62
62
  initialized.
63
63
 
64
- When a user wants to log in to your site, you direct them to a UI flow hosted by **Schibsted Account**.
65
- We authenticate the user and redirect them back to your site. This final redirect back to your site is performed in accordance with the OAuth2 specification.
64
+ When a user wants to log in to your site, you direct them to a UI flow hosted by **Schibsted Account**.
65
+ We authenticate the user and redirect them back to your site. This final redirect back to your site is performed in accordance with the OAuth2 specification.
66
66
  This means we pass a `code` in the query string of that redirect URI.
67
- You can use that `code` on your site's backend, along with your client credentials (client ID and secret), to obtain an *Access Token* (AT) and a *Refresh Token* (RT).
68
- You should not send the AT (and **never** the RT!) to the browser. Instead, keep them on the server side and associate them with the specific user session.
67
+ You can use that `code` on your site's backend, along with your client credentials (client ID and secret), to obtain an *Access Token* (AT) and a *Refresh Token* (RT).
68
+ You should not send the AT (and **never** the RT!) to the browser. Instead, keep them on the server side and associate them with the specific user session.
69
69
  This allows you to call Schibsted Account APIs on behalf of that user.
70
70
 
71
-
72
71
  ## Events
73
72
 
74
- The SDK fires events when something we deem interesting is happening. For example the
73
+ The SDK fires events on the instances when something we deem interesting is happening. For example the
75
74
  [Identity](https://schibsted.github.io/account-sdk-browser/Identity.html) class
76
75
  emits some events when the user is logged in or logged out. This SDK uses a familar interface that's
77
76
  very similar to Node's [EventEmitter](https://nodejs.org/api/events.html). The most important
78
77
  methods are `.on(eventName, listener)` (to subscribe to an event) and `.off(eventName, listener)`
79
78
  (to unsubscribe to an event).
80
79
 
80
+ The SDK also emits global events on the `window` when a new instance is created.
81
+ Events are emitted for:
82
+
83
+ | Class | event name |
84
+ | ------------ | ----------------------- |
85
+ | Identity | `schIdentity:ready` |
86
+ | Monetization | `schMonetization:ready` |
87
+ | Payment | `schPayment:ready` |
88
+
89
+ ```js
90
+ window.addEventListener('schIdentity:ready', e => {
91
+ // The event contains the initialized instance (e.detail.instance);
92
+ }
93
+
94
+ ```
95
+
81
96
  ## Identity
82
97
 
83
98
  Let's start with a bit of example code:
@@ -85,29 +100,29 @@ Let's start with a bit of example code:
85
100
  #### Example
86
101
 
87
102
  ```javascript
88
- import { Identity } from '@schibsted/account-sdk-browser'
103
+ import { Identity } from '@schibsted/account-sdk-browser';
89
104
 
90
105
  const identity = new Identity({
91
106
  clientId: '56e9a5d1eee0000000000000',
92
107
  redirectUri: 'https://awesomenews.site', // ensure it's listed in selfservice
93
108
  env: 'PRE', // Schibsted account env. A url or a special key: 'PRE', 'PRO', 'PRO_NO', 'PRO_FI' or 'PRO_DK'
94
109
  sessionDomain: 'https://id.awesomenews.site', // client-configured session-service domain
95
- })
110
+ });
96
111
 
97
112
  async function whenSiteLoaded() {
98
- const loginContainer = document.getElementById('login-container')
113
+ const loginContainer = document.getElementById('login-container');
99
114
  if (await identity.isLoggedIn()) {
100
- const user = await identity.getUser()
101
- const span = document.createElement('span')
102
- span.textContent = `Hello ${user.givenName}`
103
- loginContainer.appendChild(span)
115
+ const user = await identity.getUser();
116
+ const span = document.createElement('span');
117
+ span.textContent = `Hello ${user.givenName}`;
118
+ loginContainer.appendChild(span);
104
119
  } else {
105
- loginContainer.innerHTML = '<button class="login-button">Log in</button>'
120
+ loginContainer.innerHTML = '<button class="login-button">Log in</button>';
106
121
  }
107
122
  }
108
123
 
109
124
  function userClicksLogIn() {
110
- identity.login({ state: 'some-random-string-1234-foobar-wonky-pig' })
125
+ identity.login({ state: 'some-random-string-1234-foobar-wonky-pig' });
111
126
  }
112
127
  ```
113
128
 
@@ -126,10 +141,11 @@ with the auth `code` parameter.
126
141
 
127
142
  It is recommended that you provide a unique identifier as part of the state, to prevent CSRF
128
143
  attacks. For example this can be accomplished by:
144
+
129
145
  1. Your backend generates random token: `1234abcd`, saves it in some tokenCache, and forwards to
130
146
  your browser frontend
131
147
  1. Your frontend calls `Identity.login` with `state = base64Urlencode({ token: '1234abcd', article:
132
- '1234', ... })`
148
+ '1234', ... })`
133
149
  1. When auth flow completes, the user is redirected back to your site. Then, your backend sees the
134
150
  query parameters `code` (which it can exchange for OAuth tokens for the user) and `state`
135
151
  1. Your backend can do `decodedState = base64Urldecode(query.state)` and then verify that its
@@ -143,21 +159,22 @@ Although Schibsted account abstracts away the details of how the users sign up o
143
159
  mentioning that your end users have a few ways to log in:
144
160
 
145
161
  * Username & password: pretty self-explanatory; users register using an email address and a
146
- self-chosen password
147
- * Passwordless - email: here, the users enter their email address and receive a one-time code that
148
- they can use to log in
149
- * Multifactor authentication: first client indicates which methods should be preferred, later these
150
- will be included (if fulfilled) in `AMR` claim of IDToken
162
+ self-chosen password
163
+ * Passwordless - email: here, the users enter their email address and receive a one-time code that
164
+ they can use to log in
165
+ * Multifactor authentication: first client indicates which methods should be preferred, later these
166
+ will be included (if fulfilled) in `AMR` claim of IDToken
151
167
 
152
168
  The default is username & password. If you wish to use one of the passwordless login methods, the
153
169
  `login()` function takes an optional parameter called `acrValues` (Authentication Context Class Reference).
154
170
  The `acrValues` parameter with multifactor authentication can take following values:
155
- - `eid` - authentication using BankID (for DEV and PRE environments you can choose between country specific solution by specifying `eid-no` or `eid-se` instead)
156
- - `otp-email` - passwordless authentication using code sent to registered email
157
- - `password` - force password authentication (even if user is already logged in)
158
- - `otp` - authentication using registered one time code generator (https://tools.ietf.org/html/rfc6238)
159
- - `sms` - authentication using SMS code sent to phone number
160
- - `password otp sms` - those authentication methods might be combined
171
+
172
+ * `eid` - authentication using BankID (for DEV and PRE environments you can choose between country specific solution by specifying `eid-no` or `eid-se` instead)
173
+ * `otp-email` - passwordless authentication using code sent to registered email
174
+ * `password` - force password authentication (even if user is already logged in)
175
+ * `otp` - authentication using registered one time code generator (https://tools.ietf.org/html/rfc6238)
176
+ * `sms` - authentication using SMS code sent to phone number
177
+ * `password otp sms` - those authentication methods might be combined
161
178
 
162
179
  The classic way to authenticate a user, is to send them from your site to the Schibsted account
163
180
  domain, let the user authenticate there, and then have us redirect them back to your site. If you
@@ -176,12 +193,13 @@ Schibsted account relies on browser cookies to determine whether a user is recog
176
193
  The SDK provides functions that can be used to check if the user that's visiting your site is
177
194
  already a Schibsted user or not.
178
195
 
179
- * [Identity#isLoggedIn](https://schibsted.github.io/account-sdk-browser/Identity.html#isLoggedIn)
180
- tells you if the user that is visiting your site is already logged in to Schibsted account or not.
181
- * [Identity#isConnected](https://schibsted.github.io/account-sdk-browser/Identity.html#isConnected)
182
- tells you if the user is connected to your client. A user might have `isLoggedIn=true` and at the
183
- same time `isConnected=false` if they have logged in to Schibsted account, but not accepted terms
184
- and privacy policy for your site.
196
+ * [Identity#isLoggedIn](https://schibsted.github.io/account-sdk-browser/Identity.html#isLoggedIn)
197
+ tells you if the user that is visiting your site is already logged in to Schibsted account or not.
198
+
199
+ * [Identity#isConnected](https://schibsted.github.io/account-sdk-browser/Identity.html#isConnected)
200
+ tells you if the user is connected to your client. A user might have `isLoggedIn=true` and at the
201
+ same time `isConnected=false` if they have logged in to Schibsted account, but not accepted terms
202
+ and privacy policy for your site.
185
203
 
186
204
  If you've lately changed your terms & conditions, maybe the user still hasn't accepted them. In that
187
205
  case they are considered *not connected*. In that case, if they click "Log in" from your site, we
@@ -201,8 +219,9 @@ It requires using Session Service, and supports both Schibsted account productId
201
219
  feature id's.
202
220
 
203
221
  #### Example
222
+
204
223
  ```javascript
205
- import { Monetization } from '@schibsted/account-sdk-browser'
224
+ import { Monetization } from '@schibsted/account-sdk-browser';
206
225
 
207
226
  const monetization = new Monetization({
208
227
  clientId: '56e9a5d1eee0000000000000',
@@ -215,9 +234,9 @@ try {
215
234
  // Check if the user has access to a a particular product
216
235
  const userId = await identity.getUserId();
217
236
  const data = await monetization.hasAccess([productId], userId);
218
- alert(`User has access to ${productId}? ${data.entitled}`)
237
+ alert(`User has access to ${productId}? ${data.entitled}`);
219
238
  } catch (err) {
220
- alert(`Could not query if the user has access to ${productId} because ${err}`)
239
+ alert(`Could not query if the user has access to ${productId} because ${err}`);
221
240
  }
222
241
  ```
223
242
 
@@ -229,20 +248,20 @@ pages for redeeming voucher codes, reviewing payment history, and more.
229
248
  #### Example
230
249
 
231
250
  ```javascript
232
- import { Payment } from '@schibsted/account-sdk-browser'
251
+ import { Payment } from '@schibsted/account-sdk-browser';
233
252
 
234
253
  const paymentSDK = new Payment({
235
254
  clientId: '56e9a5d1eee0000000000000',
236
255
  redirectUri: 'https://awesomenews.site', // ensure it's listed in selfservice
237
256
  env: 'PRE', // Schibsted account env. A url or a special key: 'PRE', 'PRO' or 'PRO_NO'
238
- })
257
+ });
239
258
 
240
259
  // Get the url to paymentSDK with paylink
241
- const paylink = '...'
242
- const paylinkUrl = paymentSDK.purchasePaylinkUrl(paylink)
260
+ const paylink = '...';
261
+ const paylinkUrl = paymentSDK.purchasePaylinkUrl(paylink);
243
262
 
244
263
  // Or another example --- pay with paylink in a popup
245
- paymentSDK.payWithPaylink(paylink)
264
+ paymentSDK.payWithPaylink(paylink);
246
265
  ```
247
266
 
248
267
  ## Appendix
@@ -273,27 +292,28 @@ browser side. Nevertheless, here is a short description of them.
273
292
  production environments is `vgs_email`, because reasons (on PRE, it is called `spid-pre-data`).
274
293
  It's a JSON string that's encoded using the standard `encodeURIComponent()` function and is an
275
294
  object that contains two pieces of information that's important:
276
- * `remember`: if set to `true`, the user chose to be remembered and this means we usually support
277
- auto-login (that is, if you call the Schibsted account hassession service, and no session can
278
- be found in the session database, it will automatically create a new one for the user so that
279
- they don't have to authenticate again. If it is `false`, it should be interpreted as the user
280
- does not want to be automatically logged in to any site when their session expires
281
- * `v`: the version number
295
+ * `remember`: if set to `true`, the user chose to be remembered and this means we usually support
296
+ auto-login (that is, if you call the Schibsted account hassession service, and no session can
297
+ be found in the session database, it will automatically create a new one for the user so that
298
+ they don't have to authenticate again. If it is `false`, it should be interpreted as the user
299
+ does not want to be automatically logged in to any site when their session expires
300
+ * `v`: the version number
282
301
  1. The **session** cookies: Cookie names in production environments are `identity`, and `SPID_SE` or
283
302
  `SPID_NO`. It contains:
284
- * `user`: an object (if it's missing, a call to hassession will return a `401` with a
285
- `UserException` that says `No session found`)
286
- * `userId` identifies the user. We use this property to compare "old" user with "new" user and
287
- fire events that indicate that the user has changed
288
- * `is_logged_in` indicates if the user is logged in
289
- * `user_tags`: a map that contains some flags about the user; namely:
290
- * `is_logged_in` indicates if the user is logged in (this seems to be a duplicate of a
291
- property with a similar name in the parent `user` object)
292
- * `terms`: a map of term ids that indicate if they've been accepted by the user.
293
- * `referer` (yep, missing the double "rr"..): If this is missing, a call to hassession will
294
- return a `401` with a `UserException` that says `No session found`.
303
+ * `user`: an object (if it's missing, a call to hassession will return a `401` with a
304
+ `UserException` that says `No session found`)
305
+ * `userId` identifies the user. We use this property to compare "old" user with "new" user and
306
+ fire events that indicate that the user has changed
307
+ * `is_logged_in` indicates if the user is logged in
308
+ * `user_tags`: a map that contains some flags about the user; namely:
309
+ * `is_logged_in` indicates if the user is logged in (this seems to be a duplicate of a
310
+ property with a similar name in the parent `user` object)
311
+ * `terms`: a map of term ids that indicate if they've been accepted by the user.
312
+ * `referer` (yep, missing the double "rr"..): If this is missing, a call to hassession will
313
+ return a `401` with a `UserException` that says `No session found`.
295
314
 
296
315
  ## Releasing
316
+
297
317
  Tags are pushed to NPM via Travis. To release a new version, run in master
298
318
 
299
319
  ```bash
@@ -305,7 +325,7 @@ and push.
305
325
 
306
326
  ## LICENSE
307
327
 
308
- Copyright (c) 2024 Schibsted Products & Technology AS
328
+ Copyright (c) 2026 Schibsted Products & Technology AS
309
329
 
310
330
  Licensed under the [MIT
311
331
  License](https://github.com/schibsted/account-sdk-browser/blob/master/LICENSE.md)