Created
July 26, 2021 17:51
-
-
Save enappd/642667ad906330c2a599effcc4359a74 to your computer and use it in GitHub Desktop.
Ionic Angular Capacitor Google Login
This file contains 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 { GoogleAuth } from '@codetrix-studio/capacitor-google-auth'; | |
import { Router, NavigationExtras } from '@angular/router'; | |
@Component({ | |
selector: 'app-home', | |
templateUrl: 'home.page.html', | |
styleUrls: ['home.page.scss'], | |
}) | |
export class HomePage { | |
constructor(private router: Router) { } | |
ionViewDidEnter() { | |
GoogleAuth.init(); | |
} | |
checkLoggedIn() { | |
GoogleAuth.refresh().then((data) => { | |
if (data.accessToken) { | |
let navigationExtras: NavigationExtras = { | |
state: { | |
user: { type: 'existing', accessToken: data.accessToken, idToken: data.idToken } | |
} | |
}; | |
this.router.navigate(['landing'], navigationExtras); | |
} | |
}).catch(e => { | |
if (e.type === 'userLoggedOut') { | |
this.doLogin(); | |
} | |
}); | |
} | |
async doLogin() { | |
const user = await GoogleAuth.signIn(); | |
if (user) { | |
this.goToHome(user); | |
} | |
} | |
goToHome(user) { | |
let navigationExtras: NavigationExtras = { state: { user: user } }; | |
this.router.navigate(['landing'], navigationExtras); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment