Google Login frontend

This commit is contained in:
2023-10-08 12:27:31 +08:00
parent 5c79fe982e
commit 1eba696b8a
13 changed files with 171 additions and 18 deletions
+15 -2
View File
@@ -2,6 +2,8 @@ import { Component } from '@angular/core';
import { BnNgIdleService } from 'bn-ng-idle';
import {SessionDataProviderService} from "./store/session-data-provider.service";
import {Router} from "@angular/router"; // import it to your component
import { Platform } from '@ionic/angular';
import { GoogleAuth } from '@codetrix-studio/capacitor-google-auth';
@Component({
selector: 'app-root',
@@ -9,7 +11,11 @@ import {Router} from "@angular/router"; // import it to your component
styleUrls: ['app.component.scss'],
})
export class AppComponent {
constructor(private bnIdle: BnNgIdleService,private sessionDataProviderService : SessionDataProviderService, public router: Router) {
constructor(
private bnIdle: BnNgIdleService,
private sessionDataProviderService : SessionDataProviderService,
public router: Router,
public platform: Platform) {
// this.OneSignalInit();
this.bnIdle.startWatching(2500).subscribe((res) => {
if(res) {
@@ -18,11 +24,18 @@ export class AppComponent {
this.sessionDataProviderService.DestroySessionOnLogout();
this.router.navigate(['login']);
}
})
});
this.initializeApp();
}
// Call this function when your app starts
OneSignalInit(): void {
console.log('Moved to home');
}
initializeApp(): void {
this.platform.ready().then(() => {
//GoogleAuth.initialize()
})
}
}
+2 -2
View File
@@ -72,12 +72,12 @@
</ion-buttons>
<ion-buttons>
<ion-button>
<ion-button (click) ="onLoginGoogle()" >
<ion-icon name="logo-google"></ion-icon>
</ion-button>
</ion-buttons>
<ion-buttons>
<ion-buttons *ngIf="ios">
<ion-button (click) ="onLoginApple()" >
<ion-icon name="logo-apple"></ion-icon>
</ion-button>
+67 -3
View File
@@ -4,7 +4,7 @@ import { WrenchService } from 'src/app/services/wrench.service';
import { SessionDataProviderService } from 'src/app/store/session-data-provider.service';
import { BlogDataService } from 'src/app/store/blog-data.service';
import { environment} from "../../../environments/environment";
import { AlertController } from '@ionic/angular';
import { AlertController, Platform } from '@ionic/angular';
import { Preferences } from '@capacitor/preferences';
import { Capacitor } from '@capacitor/core';
import jwt_decode from "jwt-decode";
@@ -17,6 +17,7 @@ import {
FacebookLogin,
FacebookLoginResponse,
} from '@capacitor-community/facebook-login';
import { GoogleAuth, User } from '@codetrix-studio/capacitor-google-auth';
@Component({
selector: 'app-login',
@@ -32,15 +33,30 @@ export class LoginPage implements OnInit {
appleSignInAbortController: any;
appleSignInWindow: any;
state: any;
ios: boolean;
android: boolean;
constructor(
private router: Router,
private wrenchService: WrenchService,
public sessionDataProviderService:SessionDataProviderService,
public blogDataService:BlogDataService,
private alertController: AlertController
private alertController: AlertController,
public platform: Platform
) {
// https://ionicframework.com/docs/angular/platform
this.ios = platform.is('ios');
this.android = platform.is('android');
}
ionViewDidEnter() {
// GoogleAuth.init();
// use hook after platform dom ready
GoogleAuth.initialize({
clientId: '817021856543-ad9nsjgdpsu2s2jrl63j3ihrv7lbf6ma.apps.googleusercontent.com',
scopes: ['profile', 'email'],
grantOfflineAccess: true,
});
}
ngOnInit() {
@@ -115,6 +131,54 @@ getBlogData(){
this.router.navigate(['startscan']);
}
onLoginGoogle() {
GoogleAuth.signIn().then((user: User) => {
//const credential = auth.GoogleAuthProvider.credential(user.authentication.idToken);
//return this.afAuth.auth.signInAndRetrieveDataWithCredential(credential);
if (user) {
const oauth2Data = {
"auth_type": "GOOGLE",
"access_token": user.authentication.accessToken,
"refresh_token": user.authentication.refreshToken,
"idToken": user.authentication.idToken,
"auth_code": user.serverAuthCode,
"user": user
};
console.log(oauth2Data);
// Validate token with server and create new session
this.wrenchService.authStart(oauth2Data).subscribe(
loginResult => {
console.log(loginResult);
this.loginResult = loginResult;
console.log("INTERNAL RETURN->" + this.loginResult.internal_return);
if (loginResult != null && loginResult.internal_return == 100 && this.sessionDataProviderService.ConstructGlobalSessionData(this.loginResult) == true) {
const setName = async () => {
await Preferences.set({
key: 'username',
value: this.username,
});
};
this.getBlogData();
this.router.navigate(['tabs/tab1']);
}
else{
this.showAlert("Error","Invalid username/password");
}
}
);
} else {
console.log(user);
this.showAlert("Error", "Login failed!");
}
}).catch((error: any) => {
this.showAlert("Error", error);
console.log(error);
});
}
onLoginFacebook() {
const FACEBOOK_PERMISSIONS = [
'email',