我是应用开发新手,但我正在构建一个连接到我的Firebase的Google Login的应用程序。 我可以成功连接并显示用户ID,但我想在成功登录时转到另一个页面(主页(。
import { HomePage } from "../home/home";
import { RegisterPage } from "../register/register";
import * as firebase from "firebase/app";
import { AngularFireAuth } from "angularfire2/auth";
import { Observable } from "rxjs/Observable";
import { Router } from "@angular/router";
import { GooglePlus } from "@ionic-native/google-plus";
import { Platform } from "ionic-angular";
@IonicPage()
@Component({
selector: "page-login",
templateUrl: "login.html"
})
export class LoginPage {
user: Observable<firebase.User>;
constructor(
public nav: NavController,
public navParams: NavParams,
public forgotCtrl: AlertController,
public menu: MenuController,
public toastCtrl: ToastController,
private afAuth: AngularFireAuth,
private gplus: GooglePlus,
private router: Router,
private platform: Platform
) {
this.menu.swipeEnable(false);
this.user = this.afAuth.authState;
}
async nativeGoogleLogin(): Promise<void> {
try {
const gplusUser = await this.gplus.login({
webClientId:
"MyClientWebAPI",
offline: true,
scopes: "profile email"
});
return await this.afAuth.auth.signInWithCredential(
firebase.auth.GoogleAuthProvider.credential(gplusUser.idToken)
//Here is where i try to inject the router.
.then(success => {
this.router.navigate(["HomePage"]);
})
);
} catch (err) {
console.log(err);
}
}
googleLogin() {
if (this.platform.is("cordova")) {
this.nativeGoogleLogin();
} else {
this.webGoogleLogin();
}
}
我试图使用 then(成功 =>....( 在身份验证中注入一些代码,但我无法让它工作。
任何人都可以帮助代码吗?
谢谢
return await this.afAuth.auth
.signInWithCredential(
firebase.auth.GoogleAuthProvider.credential(gplusUser.idToken)
)
.then(data => {
this.nav.setRoot(HomePage);
});
} catch (err) {
console.log(err);
}
}
async webGoogleLogin(): Promise<void> {
try {
const provider = new firebase.auth.GoogleAuthProvider();
const credential = await this.afAuth.auth
.signInWithPopup(provider)
.then(data => {
this.nav.setRoot(HomePage);
});
} catch (err) {
console.log(err);
} }
也许你可以这样做:
async loginUser() {
this.user.email = this.myForm.value.email;
this.user.password = this.myForm.value.password;
this.presentLoading();
try {
this.authService
.signInWithEmailAndPassword(this.user)
.then(data => {
this.navCtrl.setRoot(ListaPage);
})
.catch(e => {
console.log("error");
});
} catch (e) {
console.error(e);
}
}
在身份验证服务中,您可以插入谷歌登录逻辑。