https://github.com/rxlabz/ng-sign2g
Angular2 (rc1) Google Sign-In service
https://github.com/rxlabz/ng-sign2g
Last synced: over 1 year ago
JSON representation
Angular2 (rc1) Google Sign-In service
- Host: GitHub
- URL: https://github.com/rxlabz/ng-sign2g
- Owner: rxlabz
- Created: 2016-05-15T12:31:32.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2016-05-18T11:11:44.000Z (about 10 years ago)
- Last Synced: 2025-03-09T15:08:49.746Z (over 1 year ago)
- Language: TypeScript
- Homepage:
- Size: 28.3 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Google Sign-In service in Angular 2 App
## Gauth.service
The Gauth service exposes :
- **currentProfile$** : an Observable which allows to get the user profile.
- get **isSignedIn()**:boolean
- **signIn()**
- **grant(scope:string, offline:boolean = false)** : request additionnal permissions
- revoke() : cancel all app permissions
- logout()
### Usage
**Configuration**
- google js api
```html
```
- Injected CLIENT_ID
```typescript
// app/config.ts
export interface Config {
client_id:string;
}
export let CONFIG:Config = {
client_id: 'YOUR_APP_ID_LIKE_0123456789.apps.googleusercontent.com'
}
// create token used for DI
export let APP_CONFIG = new OpaqueToken('app.config');
// app/gauth.service.ts
constructor(@Inject(APP_CONFIG) private cfg:Config, private http:Http, private ngZone:NgZone) {
this.CLIENT_ID = cfg.client_id;
this.currentProfile$ = new Observable(obs => this.profileOb$r = obs).share();
this.loadApi(this.CLIENT_ID);
}
loadApi(CLIENT_ID:string) {
gapi['load']('auth2', this.onApi.bind(this));
}
```
In this example, the service is injected in Ng2gAuthApp component.
```typescript
// ng2-auth2.components.ts
constructor(public service:GauthService) {}
ngOnInit(){
this.currentProfile$ = this.service.currentProfile$;
this.currentProfile$.subscribe((value)=>this.profile = value);
}
onProfile(p:Profile){
this.currentProfile = p;
this.grantedScopes = p ? p.scope.split(' ') : [];
}
```