Commit 3fb537fb authored by Emiliano Balbuena's avatar Emiliano Balbuena

(feat): Reverse SSO

1 merge request!656WIP: SSO for Pro sites
Pipeline #96255662 running with stages
......@@ -11,7 +11,17 @@ export class SsoService {
protected site: SiteService,
protected client: Client,
protected session: Session
) {}
) {
this.listen();
}
listen() {
this.session.isLoggedIn((is: boolean) => {
if (is) {
this.auth();
}
});
}
isRequired(): boolean {
return this.site.isProDomain;
......@@ -32,8 +42,25 @@ export class SsoService {
}
);
window.Minds.user = authorization.user;
this.session.login(window.Minds.user);
this.session.inject(window.Minds.user);
}
} catch (e) {
console.error(e);
}
}
async auth() {
try {
const connect: any = await this.client.post('api/v2/sso/connect');
if (connect && connect.token && connect.status === 'success') {
// TODO: Use headers
await this.client.postRaw(
`${this.minds.site_url}api/v2/sso/authorize`,
{
token: connect.token,
}
);
}
} catch (e) {
console.error(e);
......
......@@ -112,6 +112,7 @@ export const MINDS_PROVIDERS: any[] = [
{
provide: Session,
useFactory: Session._,
deps: [SiteService],
},
{
provide: ThirdPartyNetworksService,
......
......@@ -56,16 +56,30 @@ export class Session {
return false;
}
/**
* Emit login event
*/
login(user: any = null) {
//clear stale local storage
inject(user: any = null) {
// Clear stale localStorage
window.localStorage.clear();
// Emit new user info
this.userEmitter.next(user);
window.Minds.user = user;
if (user.admin === true) window.Minds.Admin = true;
// Set globals
window.Minds.LoggedIn = true;
window.Minds.user = user;
if (user.admin === true) {
window.Minds.Admin = true;
}
}
/**
* Inject user and emit login event
*/
login(user: any = null) {
this.inject(user);
this.loggedinEmitter.next(true);
}
......
Please register or to comment