Skip to content

Instantly share code, notes, and snippets.

@enappd
Last active July 26, 2021 18:36
Show Gist options
  • Select an option

  • Save enappd/6e2e7cf0332933a155a2f77d2a68d0d8 to your computer and use it in GitHub Desktop.

Select an option

Save enappd/6e2e7cf0332933a155a2f77d2a68d0d8 to your computer and use it in GitHub Desktop.
Ionic Angular Capacitor Google Login
import { Component } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { GoogleAuth } from '@codetrix-studio/capacitor-google-auth';
import { Http } from '@capacitor-community/http';
@Component({
selector: 'app-landing',
templateUrl: './landing.page.html',
styleUrls: ['./landing.page.scss'],
})
export class LandingPage {
user: any;
constructor(private route: ActivatedRoute, private router: Router) {
this.route.queryParams.subscribe(params => {
let data = this.router.getCurrentNavigation().extras.state;
if (data) {
if (data.user.type === 'existing') {
let token = data.user.accessToken;
this.getUserProfileData(token);
}
else {
this.user = data.user;
}
}
});
}
signOut() {
GoogleAuth.signOut().then(() => {
this.router.navigate(['home']);
});
}
async getUserProfileData(token) {
const options = {
url: `https://www.googleapis.com/oauth2/v2/userinfo?key=YOUR_WEB_API_KEY&oauth_token=${token}`,
headers:{'Content-Type': 'application/json'}
};
const response = await Http.request({ ...options, method: 'GET' });
this.user = response.data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment