我得到 authManager is undefined
I get authManager is undefined
我在使用 Auth0 进行身份验证时遇到问题。
我得到 authManager 上的 authenticate is undefined。
authManager注入到authService中,添加到app中"auth0.lock","angular-jwt"添加到模块中
App.ts(运行 方法):
app.run(["$rootScope", "authManager", "AuthService",
function($rootScope, authManager, authService) {
// Put the authService on $rootScope so its methods
// can be accessed from the nav bar
$rootScope.authService = authService;
// Register the authentication listener that is
// set up in auth.service.js
authService.registerAuthenticationListener();
// Use the authManager from angular-jwt to check for
// the user's authentication state when the page is
// refreshed and maintain authentication
authManager.checkAuthOnRefresh();
// Listen for 401 unauthorized requests and redirect
// the user to the login page
authManager.redirectWhenUnauthenticated();
}]);
AuthService.ts:
class AuthService {
public userProfile: any;
constructor(private $rootScope, private lock, private authManager) {
this.userProfile = JSON.parse(localStorage.getItem("profile")) || {};
}
public login() {
this.lock.show();
}
// Logging out just requires removing the user's
// id_token and profile
public logout() {
localStorage.removeItem("id_token");
localStorage.removeItem("profile");
this.authManager.unauthenticate();
this.userProfile = {};
}
// Set up the logic for when a user authenticates
// This method is called from app.run.js
public registerAuthenticationListener(authManager) {
//const self = AuthService._instance;
this.lock.on("authenticated", function(authResult) {
localStorage.setItem("id_token", authResult.idToken);
authManager.authenticate(); // <-- here is the error
this.lock.getProfile(authResult.idToken, function(error, profile) {
if (error) {
console.log(error);
}
localStorage.setItem("profile", JSON.stringify(profile));
this.$rootScope.$broadcast("userProfileSet", profile);
});
});
}
}
AuthService.$inject = ["$rootScope", "lock", "authManager"];
export default AuthService;
我在服务中添加了一个静态工厂方法,authManager 不是未定义的,但它没有在 authService 构造函数中设置。
代码如下:
public static Factory($rootScope, lock, authManager) {
AuthService._instance = new AuthService($rootScope, lock, authManager);
console.log("authManager: " + authManager.authenticate);
return AuthService._instance;
}
使用TypeScript和ES6时的解决方法:
第一步:
app.ts
.service("AuthService", AuthService.Factory)
第二步:
授权服务
class AuthService {
private static _instance: AuthService;
public userProfile: any;
private _authManager: any;
private _lock: any;
private _$rootScope: any;
public static Factory($rootScope, lock, authManager) {
AuthService._instance = new AuthService($rootScope, lock, authManager);
return AuthService._instance;
}
constructor(private $rootScope, private lock, private authManager) {
this._authManager = authManager;
this._lock = lock;
this._$rootScope = $rootScope;
this.userProfile = JSON.parse(localStorage.getItem("profile")) || {};
}
public login() {
this.lock.show();
}
// Logging out just requires removing the user's
// id_token and profile
public logout() {
localStorage.removeItem("id_token");
localStorage.removeItem("profile");
this.authManager.unauthenticate();
this.userProfile = {};
}
// Set up the logic for when a user authenticates
// This method is called from app.run.js
public registerAuthenticationListener() {
const self = AuthService._instance;
this.lock.on("authenticated", function (authResult) {
localStorage.setItem("id_token", authResult.idToken);
self._authManager.authenticate();
self._lock.getProfile(authResult.idToken, function (error, profile) {
if (error) {
console.log(error);
}
localStorage.setItem("profile", JSON.stringify(profile));
self._$rootScope.$broadcast("userProfileSet", profile);
});
});
}
}
AuthService.$inject = ["$rootScope", "lock", "authManager"];
export default AuthService;
我正在工厂中创建一个新实例,然后通过获取实例在方法中使用它。 const self = AuthService._instance
.
我在使用 Auth0 进行身份验证时遇到问题。
我得到 authManager 上的 authenticate is undefined。
authManager注入到authService中,添加到app中"auth0.lock","angular-jwt"添加到模块中
App.ts(运行 方法):
app.run(["$rootScope", "authManager", "AuthService",
function($rootScope, authManager, authService) {
// Put the authService on $rootScope so its methods
// can be accessed from the nav bar
$rootScope.authService = authService;
// Register the authentication listener that is
// set up in auth.service.js
authService.registerAuthenticationListener();
// Use the authManager from angular-jwt to check for
// the user's authentication state when the page is
// refreshed and maintain authentication
authManager.checkAuthOnRefresh();
// Listen for 401 unauthorized requests and redirect
// the user to the login page
authManager.redirectWhenUnauthenticated();
}]);
AuthService.ts:
class AuthService {
public userProfile: any;
constructor(private $rootScope, private lock, private authManager) {
this.userProfile = JSON.parse(localStorage.getItem("profile")) || {};
}
public login() {
this.lock.show();
}
// Logging out just requires removing the user's
// id_token and profile
public logout() {
localStorage.removeItem("id_token");
localStorage.removeItem("profile");
this.authManager.unauthenticate();
this.userProfile = {};
}
// Set up the logic for when a user authenticates
// This method is called from app.run.js
public registerAuthenticationListener(authManager) {
//const self = AuthService._instance;
this.lock.on("authenticated", function(authResult) {
localStorage.setItem("id_token", authResult.idToken);
authManager.authenticate(); // <-- here is the error
this.lock.getProfile(authResult.idToken, function(error, profile) {
if (error) {
console.log(error);
}
localStorage.setItem("profile", JSON.stringify(profile));
this.$rootScope.$broadcast("userProfileSet", profile);
});
});
}
}
AuthService.$inject = ["$rootScope", "lock", "authManager"];
export default AuthService;
我在服务中添加了一个静态工厂方法,authManager 不是未定义的,但它没有在 authService 构造函数中设置。
代码如下:
public static Factory($rootScope, lock, authManager) {
AuthService._instance = new AuthService($rootScope, lock, authManager);
console.log("authManager: " + authManager.authenticate);
return AuthService._instance;
}
使用TypeScript和ES6时的解决方法:
第一步: app.ts
.service("AuthService", AuthService.Factory)
第二步: 授权服务
class AuthService {
private static _instance: AuthService;
public userProfile: any;
private _authManager: any;
private _lock: any;
private _$rootScope: any;
public static Factory($rootScope, lock, authManager) {
AuthService._instance = new AuthService($rootScope, lock, authManager);
return AuthService._instance;
}
constructor(private $rootScope, private lock, private authManager) {
this._authManager = authManager;
this._lock = lock;
this._$rootScope = $rootScope;
this.userProfile = JSON.parse(localStorage.getItem("profile")) || {};
}
public login() {
this.lock.show();
}
// Logging out just requires removing the user's
// id_token and profile
public logout() {
localStorage.removeItem("id_token");
localStorage.removeItem("profile");
this.authManager.unauthenticate();
this.userProfile = {};
}
// Set up the logic for when a user authenticates
// This method is called from app.run.js
public registerAuthenticationListener() {
const self = AuthService._instance;
this.lock.on("authenticated", function (authResult) {
localStorage.setItem("id_token", authResult.idToken);
self._authManager.authenticate();
self._lock.getProfile(authResult.idToken, function (error, profile) {
if (error) {
console.log(error);
}
localStorage.setItem("profile", JSON.stringify(profile));
self._$rootScope.$broadcast("userProfileSet", profile);
});
});
}
}
AuthService.$inject = ["$rootScope", "lock", "authManager"];
export default AuthService;
我正在工厂中创建一个新实例,然后通过获取实例在方法中使用它。 const self = AuthService._instance
.