@ng-solid/google
v22.0.9
Published
A library of Google UI components for Angular
Maintainers
Readme
This library was generated with Angular CLI version 10.0.9.
Google OAuth 2.0 sign-in (Authorization Code + PKCE)
NsGoogleService can drive a redirect-based Google sign-in on top of the
Authorization Code + PKCE flow. The package only builds the authorization URL,
navigates to it, and — on return — validates and parses the redirect; it never
talks to Google's token endpoint. Exchanging the returned code (with
codeVerifier and redirectUri) for tokens is the responsibility of the
consuming application's own backend.
// app.config.ts
provideNsGoogle({
analyticsId: 'G-XXXXXXX',
clientId: '1234567890-abc.apps.googleusercontent.com',
redirectUri: 'https://app.example.com/auth/google/callback',
// optional — defaults to ['openid', 'email', 'profile']
scopes: ['openid', 'email', 'profile'],
// optional
prompt: 'consent',
accessType: 'offline'
});// sign-in-button.component.ts
export class SignInButtonComponent {
private readonly google = inject(NsGoogleService);
signInWithGoogle(): void {
this.google.signIn().catch((error) => {
// clientId/redirectUri missing, or running outside the browser (SSR)
console.error(error);
});
}
}// auth-google-callback.component.ts — the route registered as `redirectUri`
export class AuthGoogleCallbackComponent {
private readonly google = inject(NsGoogleService);
private readonly http = inject(HttpClient);
constructor() {
try {
const { code, codeVerifier, redirectUri } =
this.google.handleRedirectCallback();
// Send these to your own backend to exchange the code for tokens —
// this package never performs that exchange itself.
this.http
.post('/api/auth/google/exchange', { code, codeVerifier, redirectUri })
.subscribe();
} catch (error) {
if (error instanceof GoogleAuthorizationError) {
// error.code is one of:
// 'access_denied' | 'oauth_error' | 'missing_state' |
// 'state_mismatch' | 'missing_code'
console.error(error.code, error.message);
}
}
}
}Code scaffolding
Run ng generate component component-name --project twitter to generate a new component. You can also use ng generate directive|pipe|service|class|guard|interface|enum|module --project twitter.
Note: Don't forget to add
--project twitteror else it will be added to the default project in yourangular.jsonfile.
Build
Run ng build twitter to build the project. The build artifacts will be stored in the dist/ directory.
Publishing
After building your library with ng build twitter, go to the dist folder cd dist/twitter and run npm publish.
Running unit tests
Run ng test twitter to execute the unit tests via Karma.
Further help
To get more help on the Angular CLI use ng help or go check out the Angular CLI README.
