Skip to content

Instantly share code, notes, and snippets.

@travist
Last active April 12, 2023 16:45
Show Gist options
  • Save travist/7f6a161726eb192f09ab29b509cc859a to your computer and use it in GitHub Desktop.
Save travist/7f6a161726eb192f09ab29b509cc859a to your computer and use it in GitHub Desktop.
Angular SSO Init
import { Formio } from 'formiojs';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements AfterViewInit, OnInit {
constructor(public auth: FormioAuthService) {}
ngOnInit() {
const namespace = localStorage.getItem('ssoNamespace');
this.authTenant(namespace);
}
async authTenant(namespace = 'formio') {
Formio.namespace = namespace;
Formio.setProjectUrl(`${Formio.getBaseUrl()}/${namespace}`);
Formio.setAuthUrl(`${Formio.getBaseUrl()}/${namespace}`);
const query: any = Formio.pageQuery();
if (Formio.getToken()) {
await this.onAuthenticated(namespace);
}
else {
this.router.navigate(['auth']);
localStorage.setItem('ssoNamespace', namespace);
Formio.ssoInit('saml', {
forceAuth: true
}).then(() => {
this.onAuthenticated(namespace);
});
}
}
async onAuthenticated(namespace) {
this.auth.init();
await this.auth.ready.then(async () => {
if (namespace === 'formio') {
const tenants = await this.getTenants();
for (let i = 0; i < tenants.length; i++) {
await this.authTenant(tenants[i].name);
}
localStorage.setItem('ssoNamespace', '');
}
});
}
async getTenants() {
const result = await fetch(....);
return await result.json();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment