Last active
April 12, 2023 16:45
-
-
Save travist/7f6a161726eb192f09ab29b509cc859a to your computer and use it in GitHub Desktop.
Angular SSO Init
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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