@leancodepl/login-manager 10.3.1 → 10.5.0

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/CHANGELOG.md CHANGED
@@ -3,6 +3,24 @@
3
3
  All notable changes to this project will be documented in this file. See
4
4
  [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [10.5.0](https://github.com/leancodepl/js_corelibrary/compare/v10.4.0...v10.5.0) (2026-07-01)
7
+
8
+ **Note:** Version bump only for package @leancodepl/login-manager
9
+
10
+ # Change Log
11
+
12
+ All notable changes to this project will be documented in this file. See
13
+ [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
14
+
15
+ # [10.4.0](https://github.com/leancodepl/js_corelibrary/compare/v10.3.1...v10.4.0) (2026-06-23)
16
+
17
+ **Note:** Version bump only for package @leancodepl/login-manager
18
+
19
+ # Change Log
20
+
21
+ All notable changes to this project will be documented in this file. See
22
+ [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
23
+
6
24
  ## [10.3.1](https://github.com/leancodepl/js_corelibrary/compare/v10.3.0...v10.3.1) (2026-05-20)
7
25
 
8
26
  **Note:** Version bump only for package @leancodepl/login-manager
package/dist/index.js CHANGED
@@ -211,7 +211,7 @@ class c {
211
211
  return;
212
212
  }
213
213
  const n = document.createElement("script");
214
- n.id = s, n.async = !0, n.src = "//connect.facebook.net/pl_PL/sdk.js", t.parentNode != null && t.parentNode.insertBefore(n, t), n.onload = () => {
214
+ n.id = s, n.async = !0, n.src = "//connect.facebook.net/pl_PL/sdk.js", t?.parentNode != null && t.parentNode.insertBefore(n, t), n.onload = () => {
215
215
  this.initializeSDK(), this.getLoginStatus(e);
216
216
  };
217
217
  }
@@ -1 +1 @@
1
- (function(n,r){typeof exports=="object"&&typeof module<"u"?r(exports):typeof define=="function"&&define.amd?define(["exports"],r):(n=typeof globalThis<"u"?globalThis:n||self,r(n["@leancodepl/login-manager"]={}))})(this,(function(n){"use strict";const r="token_refresh_lock";class h{constructor(e,t,s,i,y,u){this.storage=e,this.endpoint=t,this.clientSecret=s,this.clientId=i,this.scopes=y,this.additionalParams=u,s||(this.additionalParams={...u,client_id:i})}callbacks=[];refreshTokenCallbacks=[];isRefreshingToken=!1;trySignIn(e,t){return this.acquireToken(this.buildSignInRequest(e,t))}trySignInWithFacebook(e){return this.acquireToken(this.buildSignInWithFacebookRequest(e))}trySignInWithOneTimeToken(e){return this.acquireToken(this.buildSignInWithOneTimeTokenRequest(e))}trySignInWithGoogle(e){return this.acquireToken(this.buildSignInWithGoogleRequest(e))}trySignInWithLinkedIn(e){return this.acquireToken(this.buildSignInWithLinkedInRequest(e))}trySignInWithApple(e){return this.acquireToken(this.buildSignInWithAppleRequest(e))}async tryRefreshToken(){const e=await this.storage.getToken();return e!==null?await this.tryRefreshTokenInternal(e):null}async tryRefreshTokenInternal(e){return typeof navigator<"u"&&"locks"in navigator?await this.tryRefreshWithLock():(this.isRefreshingToken||(this.isRefreshingToken=!0,(async()=>{try{const s=await this.refreshToken(e);this.refreshTokenCallbacks.forEach(i=>i(s))}catch{this.refreshTokenCallbacks.forEach(s=>s(!1))}finally{this.isRefreshingToken=!1,this.refreshTokenCallbacks=[]}})()),new Promise(t=>{this.refreshTokenCallbacks.push(t)}))}async tryRefreshWithLock(){const e=await navigator.locks.request(r,{ifAvailable:!0},async t=>{if(!t)return null;const s=await this.storage.getToken();if(s===null)return!1;try{return await this.refreshToken(s)}catch{return!1}});return e!==null?e:await this.waitForRefreshLockRelease()}async waitForRefreshLockRelease(){return await navigator.locks.request(r,{mode:"shared"},async()=>await this.storage.getToken()!==null)}async refreshToken(e){return(await this.acquireToken(this.buildRefreshRequest(e))).type==="success"}onChange(e){this.callbacks.push(e)}removeOnChange(e){const t=this.callbacks.indexOf(e);t!==-1&&this.callbacks.splice(t,1)}async acquireToken(e){try{const t=await fetch(this.endpoint+"/connect/token",e);if(!t.ok)return t.status===400&&await this.signOut(),{type:"failure"};const s=await t.json(),i=new Date;return i.setSeconds(new Date().getSeconds()+s.expires_in),await this.storage.storeToken({token:s.access_token,refreshToken:s.refresh_token,expirationDate:i}),this.notify(!0),{type:"success"}}catch(t){return console.warn("Cannot call Auth server, error: ",t),{type:"networkError"}}}buildSignInRequest(e,t){const s={grant_type:"password",scope:this.scopes,username:e,password:t,...this.additionalParams};return{method:"POST",headers:this.prepareHeaders(),body:new URLSearchParams(s)}}buildSignInWithOneTimeTokenRequest(e){const t={grant_type:"onetime",scope:this.scopes,token:e,...this.additionalParams};return{method:"POST",headers:this.prepareHeaders(),body:new URLSearchParams(t)}}buildAssertionSignInRequest(e,t){const s={grant_type:t,scope:this.scopes,assertion:e,...this.additionalParams};return{method:"POST",headers:this.prepareHeaders(),body:new URLSearchParams(s)}}buildSignInWithFacebookRequest(e){return this.buildAssertionSignInRequest(e,"facebook")}buildSignInWithGoogleRequest(e){return this.buildAssertionSignInRequest(e,"google")}buildSignInWithLinkedInRequest(e){return this.buildAssertionSignInRequest(e,"linkedin")}buildSignInWithAppleRequest(e){return this.buildAssertionSignInRequest(e,"apple")}buildRefreshRequest(e){const t={grant_type:"refresh_token",scope:this.scopes,refresh_token:e.refreshToken||"",...this.additionalParams};return{method:"POST",headers:this.prepareHeaders(),body:new URLSearchParams(t)}}prepareHeaders(){const e=new Headers;if(this.clientSecret){const t=btoa(`${this.clientId}:${this.clientSecret}`);e.append("Authorization","Basic "+t)}return e.append("Content-Type","application/x-www-form-urlencoded"),e}notify(e){for(const t of this.callbacks)t(e)}}class a extends Error{constructor(e){super(e),this.name="CannotRefreshTokenError",Object.setPrototypeOf(this,a.prototype)}}class l extends h{async signOut(){await this.storage.resetToken(),this.notify(!1)}async isSigned(){return await this.storage.getToken()!==null}async getToken(){const e=await this.storage.getToken();if(e===null)return null;if(e.expirationDate<new Date){if(await this.tryRefreshTokenInternal(e))return(await this.storage.getToken())?.token??null;throw new a("Cannot refresh access token after it has expired")}else return e.token}async load(){const e=await this.isSigned();this.notify(e)}}class c{constructor(e,t){this.facebookAppId=e,this.facebookPermissions=t,this.isSignedIn=void 0,this.token=""}isSignedIn;token;get accessToken(){return this.token}setup(e){const t=document.getElementsByTagName("script")[0],s="facebook-jssdk";if(document.getElementById(s)){this.initializeSDK(),this.getLoginStatus(e);return}const i=document.createElement("script");i.id=s,i.async=!0,i.src="//connect.facebook.net/pl_PL/sdk.js",t.parentNode!=null&&t.parentNode.insertBefore(i,t),i.onload=()=>{this.initializeSDK(),this.getLoginStatus(e)}}login(e){FB.login(t=>{t.status==="connected"?(this.isSignedIn=!0,this.token=t.authResponse.accessToken,e&&e(t.authResponse.accessToken)):this.isSignedIn=!1},{scope:this.facebookPermissions})}getLoginStatus(e){FB.getLoginStatus(t=>{t.status==="connected"?(this.isSignedIn=!0,this.token=t.authResponse.accessToken,e&&e(t.authResponse.accessToken)):this.isSignedIn=!1})}initializeSDK(){FB.init({appId:this.facebookAppId,xfbml:!0,version:"v2.9"})}}class k{constructor(e="token",t="refresh_token",s="expiration_date"){this.tokenKey=e,this.refreshKey=t,this.expiryKey=s}getToken(){return this.hasValue(this.tokenKey)?{token:this.getValue(this.tokenKey),refreshToken:this.getValue(this.refreshKey),expirationDate:new Date(Number(this.getValue(this.expiryKey)))}:null}storeToken(e){this.setValue(this.tokenKey,e.token),this.setValue(this.refreshKey,e.refreshToken),this.setValue(this.expiryKey,e.expirationDate.getTime().toString())}resetToken(){this.remove(this.tokenKey),this.remove(this.refreshKey),this.remove(this.expiryKey)}hasValue(e){return localStorage.getItem(e)!==null}getValue(e){return localStorage.getItem(e)}setValue(e,t){localStorage.setItem(e,t)}remove(e){localStorage.removeItem(e)}}class g{token=null;getToken(){return this.token}storeToken(e){return this.token={token:e.token,refreshToken:e.refreshToken,expirationDate:e.expirationDate},Promise.resolve()}resetToken(){return this.token=null,Promise.resolve()}}class f{constructor(e="token",t="refresh_token",s="expiration_date"){this.tokenKey=e,this.refreshKey=t,this.expiryKey=s}getToken(){return this.hasValue(this.tokenKey)?{token:this.getValue(this.tokenKey),refreshToken:this.getValue(this.refreshKey),expirationDate:new Date(Number(this.getValue(this.expiryKey)))}:null}storeToken(e){this.setValue(this.tokenKey,e.token),this.setValue(this.refreshKey,e.refreshToken),this.setValue(this.expiryKey,e.expirationDate.getTime().toString())}resetToken(){this.remove(this.tokenKey),this.remove(this.refreshKey),this.remove(this.expiryKey)}hasValue(e){return sessionStorage.getItem(e)!==null}getValue(e){return sessionStorage.getItem(e)}setValue(e,t){sessionStorage.setItem(e,t)}remove(e){sessionStorage.removeItem(e)}}class d extends h{signOut(){this.storage.resetToken(),this.notify(!1)}isSigned(){return this.storage.getToken()!==null}async getToken(){const e=this.storage.getToken();if(e===null)return null;if(e.expirationDate<new Date){if(await this.tryRefreshTokenInternal(e))return this.storage.getToken()?.token??null;throw new a("Cannot refresh access token after it has expired")}else return e.token}}n.AsyncLoginManager=l,n.BaseLoginManager=h,n.CannotRefreshTokenError=a,n.FacebookClient=c,n.LocalTokenStorage=k,n.MemoryTokenStorage=g,n.SessionTokenStorage=f,n.SyncLoginManager=d,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})}));
1
+ (function(n,r){typeof exports=="object"&&typeof module<"u"?r(exports):typeof define=="function"&&define.amd?define(["exports"],r):(n=typeof globalThis<"u"?globalThis:n||self,r(n["@leancodepl/login-manager"]={}))})(this,(function(n){"use strict";const r="token_refresh_lock";class h{constructor(e,t,s,i,y,u){this.storage=e,this.endpoint=t,this.clientSecret=s,this.clientId=i,this.scopes=y,this.additionalParams=u,s||(this.additionalParams={...u,client_id:i})}callbacks=[];refreshTokenCallbacks=[];isRefreshingToken=!1;trySignIn(e,t){return this.acquireToken(this.buildSignInRequest(e,t))}trySignInWithFacebook(e){return this.acquireToken(this.buildSignInWithFacebookRequest(e))}trySignInWithOneTimeToken(e){return this.acquireToken(this.buildSignInWithOneTimeTokenRequest(e))}trySignInWithGoogle(e){return this.acquireToken(this.buildSignInWithGoogleRequest(e))}trySignInWithLinkedIn(e){return this.acquireToken(this.buildSignInWithLinkedInRequest(e))}trySignInWithApple(e){return this.acquireToken(this.buildSignInWithAppleRequest(e))}async tryRefreshToken(){const e=await this.storage.getToken();return e!==null?await this.tryRefreshTokenInternal(e):null}async tryRefreshTokenInternal(e){return typeof navigator<"u"&&"locks"in navigator?await this.tryRefreshWithLock():(this.isRefreshingToken||(this.isRefreshingToken=!0,(async()=>{try{const s=await this.refreshToken(e);this.refreshTokenCallbacks.forEach(i=>i(s))}catch{this.refreshTokenCallbacks.forEach(s=>s(!1))}finally{this.isRefreshingToken=!1,this.refreshTokenCallbacks=[]}})()),new Promise(t=>{this.refreshTokenCallbacks.push(t)}))}async tryRefreshWithLock(){const e=await navigator.locks.request(r,{ifAvailable:!0},async t=>{if(!t)return null;const s=await this.storage.getToken();if(s===null)return!1;try{return await this.refreshToken(s)}catch{return!1}});return e!==null?e:await this.waitForRefreshLockRelease()}async waitForRefreshLockRelease(){return await navigator.locks.request(r,{mode:"shared"},async()=>await this.storage.getToken()!==null)}async refreshToken(e){return(await this.acquireToken(this.buildRefreshRequest(e))).type==="success"}onChange(e){this.callbacks.push(e)}removeOnChange(e){const t=this.callbacks.indexOf(e);t!==-1&&this.callbacks.splice(t,1)}async acquireToken(e){try{const t=await fetch(this.endpoint+"/connect/token",e);if(!t.ok)return t.status===400&&await this.signOut(),{type:"failure"};const s=await t.json(),i=new Date;return i.setSeconds(new Date().getSeconds()+s.expires_in),await this.storage.storeToken({token:s.access_token,refreshToken:s.refresh_token,expirationDate:i}),this.notify(!0),{type:"success"}}catch(t){return console.warn("Cannot call Auth server, error: ",t),{type:"networkError"}}}buildSignInRequest(e,t){const s={grant_type:"password",scope:this.scopes,username:e,password:t,...this.additionalParams};return{method:"POST",headers:this.prepareHeaders(),body:new URLSearchParams(s)}}buildSignInWithOneTimeTokenRequest(e){const t={grant_type:"onetime",scope:this.scopes,token:e,...this.additionalParams};return{method:"POST",headers:this.prepareHeaders(),body:new URLSearchParams(t)}}buildAssertionSignInRequest(e,t){const s={grant_type:t,scope:this.scopes,assertion:e,...this.additionalParams};return{method:"POST",headers:this.prepareHeaders(),body:new URLSearchParams(s)}}buildSignInWithFacebookRequest(e){return this.buildAssertionSignInRequest(e,"facebook")}buildSignInWithGoogleRequest(e){return this.buildAssertionSignInRequest(e,"google")}buildSignInWithLinkedInRequest(e){return this.buildAssertionSignInRequest(e,"linkedin")}buildSignInWithAppleRequest(e){return this.buildAssertionSignInRequest(e,"apple")}buildRefreshRequest(e){const t={grant_type:"refresh_token",scope:this.scopes,refresh_token:e.refreshToken||"",...this.additionalParams};return{method:"POST",headers:this.prepareHeaders(),body:new URLSearchParams(t)}}prepareHeaders(){const e=new Headers;if(this.clientSecret){const t=btoa(`${this.clientId}:${this.clientSecret}`);e.append("Authorization","Basic "+t)}return e.append("Content-Type","application/x-www-form-urlencoded"),e}notify(e){for(const t of this.callbacks)t(e)}}class a extends Error{constructor(e){super(e),this.name="CannotRefreshTokenError",Object.setPrototypeOf(this,a.prototype)}}class l extends h{async signOut(){await this.storage.resetToken(),this.notify(!1)}async isSigned(){return await this.storage.getToken()!==null}async getToken(){const e=await this.storage.getToken();if(e===null)return null;if(e.expirationDate<new Date){if(await this.tryRefreshTokenInternal(e))return(await this.storage.getToken())?.token??null;throw new a("Cannot refresh access token after it has expired")}else return e.token}async load(){const e=await this.isSigned();this.notify(e)}}class c{constructor(e,t){this.facebookAppId=e,this.facebookPermissions=t,this.isSignedIn=void 0,this.token=""}isSignedIn;token;get accessToken(){return this.token}setup(e){const t=document.getElementsByTagName("script")[0],s="facebook-jssdk";if(document.getElementById(s)){this.initializeSDK(),this.getLoginStatus(e);return}const i=document.createElement("script");i.id=s,i.async=!0,i.src="//connect.facebook.net/pl_PL/sdk.js",t?.parentNode!=null&&t.parentNode.insertBefore(i,t),i.onload=()=>{this.initializeSDK(),this.getLoginStatus(e)}}login(e){FB.login(t=>{t.status==="connected"?(this.isSignedIn=!0,this.token=t.authResponse.accessToken,e&&e(t.authResponse.accessToken)):this.isSignedIn=!1},{scope:this.facebookPermissions})}getLoginStatus(e){FB.getLoginStatus(t=>{t.status==="connected"?(this.isSignedIn=!0,this.token=t.authResponse.accessToken,e&&e(t.authResponse.accessToken)):this.isSignedIn=!1})}initializeSDK(){FB.init({appId:this.facebookAppId,xfbml:!0,version:"v2.9"})}}class k{constructor(e="token",t="refresh_token",s="expiration_date"){this.tokenKey=e,this.refreshKey=t,this.expiryKey=s}getToken(){return this.hasValue(this.tokenKey)?{token:this.getValue(this.tokenKey),refreshToken:this.getValue(this.refreshKey),expirationDate:new Date(Number(this.getValue(this.expiryKey)))}:null}storeToken(e){this.setValue(this.tokenKey,e.token),this.setValue(this.refreshKey,e.refreshToken),this.setValue(this.expiryKey,e.expirationDate.getTime().toString())}resetToken(){this.remove(this.tokenKey),this.remove(this.refreshKey),this.remove(this.expiryKey)}hasValue(e){return localStorage.getItem(e)!==null}getValue(e){return localStorage.getItem(e)}setValue(e,t){localStorage.setItem(e,t)}remove(e){localStorage.removeItem(e)}}class g{token=null;getToken(){return this.token}storeToken(e){return this.token={token:e.token,refreshToken:e.refreshToken,expirationDate:e.expirationDate},Promise.resolve()}resetToken(){return this.token=null,Promise.resolve()}}class f{constructor(e="token",t="refresh_token",s="expiration_date"){this.tokenKey=e,this.refreshKey=t,this.expiryKey=s}getToken(){return this.hasValue(this.tokenKey)?{token:this.getValue(this.tokenKey),refreshToken:this.getValue(this.refreshKey),expirationDate:new Date(Number(this.getValue(this.expiryKey)))}:null}storeToken(e){this.setValue(this.tokenKey,e.token),this.setValue(this.refreshKey,e.refreshToken),this.setValue(this.expiryKey,e.expirationDate.getTime().toString())}resetToken(){this.remove(this.tokenKey),this.remove(this.refreshKey),this.remove(this.expiryKey)}hasValue(e){return sessionStorage.getItem(e)!==null}getValue(e){return sessionStorage.getItem(e)}setValue(e,t){sessionStorage.setItem(e,t)}remove(e){sessionStorage.removeItem(e)}}class d extends h{signOut(){this.storage.resetToken(),this.notify(!1)}isSigned(){return this.storage.getToken()!==null}async getToken(){const e=this.storage.getToken();if(e===null)return null;if(e.expirationDate<new Date){if(await this.tryRefreshTokenInternal(e))return this.storage.getToken()?.token??null;throw new a("Cannot refresh access token after it has expired")}else return e.token}}n.AsyncLoginManager=l,n.BaseLoginManager=h,n.CannotRefreshTokenError=a,n.FacebookClient=c,n.LocalTokenStorage=k,n.MemoryTokenStorage=g,n.SessionTokenStorage=f,n.SyncLoginManager=d,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leancodepl/login-manager",
3
- "version": "10.3.1",
3
+ "version": "10.5.0",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",