Skip to content

Commit

Permalink
feat: support silentSignin (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakiuncle authored Feb 27, 2023
1 parent 3b34df2 commit bb573ac
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,39 @@ class Sdk {
credentials: "include",
}).then(res => res.json());
}

public isSilentSigninRequired(): boolean{
const params = new URLSearchParams(window.location.search);
return params.get("silentSignin") === "1";
}

public silentSignin(isLoggedIn: boolean, onSuccess: (message: any) => void, onFailure: (message: any) => void) {
const iframe = document.createElement('iframe');
iframe.style.display = 'none';
iframe.src = `${this.getSigninUrl()}&silentSignin=1`;

const handleMessage = (event: MessageEvent) => {
if (window !== window.parent) {
return null;
}

if (isLoggedIn){
return null;
}

const message = event.data;
if (message.tag !== "Casdoor" || message.type !== "SilentSignin") {
return;
}
if (message.data === 'success') {
onSuccess(message);
} else {
onFailure(message);
}
};
window.addEventListener('message', handleMessage);
document.body.appendChild(iframe);
}
}

export default Sdk;

0 comments on commit bb573ac

Please sign in to comment.