31 lines
765 B
TypeScript
31 lines
765 B
TypeScript
import { inject, Injectable, NgZone } from '@angular/core';
|
|
import { Subject } from 'rxjs';
|
|
|
|
@Injectable({
|
|
providedIn: 'root',
|
|
})
|
|
export class GoogleOAuth {
|
|
private ngZone = inject(NgZone);
|
|
private codeClient?: google.accounts.oauth2.CodeClient;
|
|
private codeSubject = new Subject<string>();
|
|
|
|
readonly code$ = this.codeSubject.asObservable();
|
|
|
|
init(clientId: string): void {
|
|
this.codeClient = google.accounts.oauth2.initCodeClient({
|
|
client_id: clientId,
|
|
scope: 'openid email profile',
|
|
ux_mode: 'popup',
|
|
callback: (response) => {
|
|
if (response.code) {
|
|
this.ngZone.run(() => this.codeSubject.next(response.code));
|
|
}
|
|
},
|
|
});
|
|
}
|
|
|
|
openSignIn(): void {
|
|
this.codeClient?.requestCode();
|
|
}
|
|
}
|