Last active
July 26, 2021 18:36
-
-
Save enappd/6e2e7cf0332933a155a2f77d2a68d0d8 to your computer and use it in GitHub Desktop.
Ionic Angular Capacitor Google Login
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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