Angular 2 Google Login
This is a sample app showing how we can login using Google authentication in Angular2.
You can visit npm for more info on angular2-google-login package
To use the package in your app
Run npm install angular2-google-login
Import Package
Import this package where you want to use Google authentication service.
;
Providers
Supply the provider.
providers: AuthService;
Constructor
{}
Set your Secret Google Client ID
Set your Google client Id. preferably in ngOnInit()
interface.
AppGlobalsGOOGLE_CLIENT_ID = 'SECRET_CLIENT_ID';
Using the Google Login service
Use this code snippet to call the Google authentiation service. You can call it in a function triggered by a button click or in your desired event.
this_googleAuth;
Using fetched Google account details
In the package, localstorage
is used to hold the data -
localStorage;localStorage;localStorage;localStorage;
Alternatively, you can tweak the code and create an object that you can return to do post operation after successful login -
result = token: userDetailsid_token name: profile image: profile email: profile ; return result;
Logout user
this_googleAuth;
Development server
Run ng serve
for a dev server. Navigate to http://localhost:4200/
.
Known Issue
You might get error : gapi is not defined
It is because the service component may get loaded before gapi
is declared. Make sure you handle it when you use the service.
SUGGESTION : Use AfterViewInit
interface.