我试图将firebase身份验证代码放在另一个.js文件中,然后将其导入我的app.js文件,但这样做我遇到了错误,我可以&#39 ;解决。
以下是auth.js文件的样子:
export class AuthFirebase
{
constructor(
userLoggedIn = false,
authToken = null,
user = null,
) {
// This mostly gets called on subsequent page loads to determine
// what the current status of the user is with "user" being an object
// return by Firebase with credentials and other info inside of it
firebase.auth().onAuthStateChanged(user => {
this.userLoggedIn = user ? true : false;
this.user = user;
});
}
/* this.auth = AuthService;
this.authenticated = this.auth.isAuthenticated();
this.auth.authNotifier.on('authChange', authState => {
this.authenticated = authState.authenticated;
});*/
login(type) {
let provider;
// Determine which provider to use depending on provided type
// which is passed through from app.html
if (type === 'google') {
provider = new firebase.auth.GoogleAuthProvider();
} else if (type === 'facebook') {
provider = new firebase.auth.FacebookAuthProvider();
} else if (type === 'twitter') {
provider = new firebase.auth.TwitterAuthProvider();
}
// Call the Firebase signin method for our provider
// then take the successful or failed result and deal with
// it accordingly.
firebase.auth().signInWithPopup(provider).then((result: any) => {
// The token for this session
this.authToken = result.credential.accessToken;
// The user object containing information about the current user
this.user = result.user;
// Set a class variable to true to state we are logged in
this.userLoggedIn = true;
this.router.navigateToRoute('contacts');
}).catch(error => {
console.log('Erro no signIn');
let errorCode = error.code;
let errorMessage = error.message;
let email = error.email;
let credential = error.credential;
});
}
logout() {
// Self-explanatory signout code
firebase.auth().signOut().then(() => {
this.userLoggedIn = false;
this.router.navigateToRoute('inicio');
}).catch(error => {
throw new Error(error);
});
}
}
以下是我如何在app.js文件中导入它:
import { inject } from 'aurelia-framework';
import { Redirect } from 'aurelia-router';
import {AuthFirebase} from './common/auth';
function findDefaultRoute(router) {
return router.navigation[0].relativeHref;
}
@inject(AuthFirebase)
export class App {
constructor(authfirebase) {
this.authfirebase = authfirebase;
}
login() {
this.authfirebase.login(type);
}
logout() {
this.authfirebase.logout();
}
//...
我认为logout()只需要在需要时调用,但我需要在login()中添加一些东西,因为当我调用它时我得到了这个错误:
Uncaught ReferenceError: type is not defined
at App.login (app.js:25)
at CallScope.evaluate (vendor-bundle.js:23250)
at Listener.callSource (vendor-bundle.js:26902)
at Listener.handleEvent (vendor-bundle.js:26911)
at HTMLDocument.handleDelegatedEvent (vendor-bundle.js:24981)
如果有人能帮助我,我会很感激,谢谢。
更新
auth.js代码
import { inject } from 'aurelia-framework';
import { ContactGateway } from '../contacts/services/gateway';
import { Router } from 'aurelia-router';
@inject(Router, ContactGateway)
export class AuthFirebase
{
constructor(userLoggedIn = false, authToken = null, user = null, router, contactGateway) {
this.router = router;
this.contactGateway = contactGateway;
// This mostly gets called on subsequent page loads to determine
// what the current status of the user is with "user" being an object
// return by Firebase with credentials and other info inside of it
firebase.auth().onAuthStateChanged(user => {
this.userLoggedIn = user ? true : false;
this.user = user;
});
}
/* this.auth = AuthService;
this.authenticated = this.auth.isAuthenticated();
this.auth.authNotifier.on('authChange', authState => {
this.authenticated = authState.authenticated;
});*/
login(type) {
let provider;
// Determine which provider to use depending on provided type
// which is passed through from app.html
if (type === 'google') {
provider = new firebase.auth.GoogleAuthProvider();
} else if (type === 'facebook') {
provider = new firebase.auth.FacebookAuthProvider();
} else if (type === 'twitter') {
provider = new firebase.auth.TwitterAuthProvider();
}
// Call the Firebase signin method for our provider
// then take the successful or failed result and deal with
// it accordingly.
firebase.auth().signInWithPopup(provider).then((result: any) => {
// The token for this session
this.authToken = result.credential.accessToken;
// The user object containing information about the current user
this.user = result.user;
// Set a class variable to true to state we are logged in
this.userLoggedIn = true;
console.log('antes do navigate');
this.router.navigateToRoute('contacts');
console.log('depois do navigate');
}).catch(error => {
let errorCode = error.code;
let errorMessage = error.message;
let email = error.email;
let credential = error.credential;
console.log('Erro no signIn - ' + errorMessage);
});
}
logout() {
// Self-explanatory signout code
firebase.auth().signOut().then(() => {
this.userLoggedIn = false;
this.router.navigateToRoute('inicio');
}).catch(error => {
throw new Error(error);
});
}
}