angular - 授权:承载未定义
问题描述
嗨,伙计们,我被困住了,我用 ionic 和 angular 创建了一个应用程序,让我们谈谈一个带有 wordpress 和 jwt auth 的新闻应用程序,当用户注册时,我收到此错误: 错误
现在我发布了用于进行身份验证的代码以及令牌我错在哪里?
setupHeaders(authToken?: string) {
let token = (authToken ? authToken : window.localStorage.getItem(Constants.ADMIN_AUTH_KEY));
this.myHeaders = token == null ? new HttpHeaders({
'Accept': 'application/json',
'Content-Type': 'application/json'
}) : new HttpHeaders({
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': ('Bearer ' + token)
});
}
public getAuthToken(credentials: AuthCredential): Observable<AuthResponse> {
return this.http.post<AuthResponse>(this.config.apiBase + 'jwt-auth/v1/token', JSON.stringify(credentials), { headers: new HttpHeaders({ 'Accept': 'application/json', 'Content-Type': 'application/json', }) });
}
public getUser(userId: string): Observable<UserResponse> {
return this.http.get<UserResponse>(this.config.apiBase + 'wp/v2/users/' + userId, { headers: this.myHeaders });
}
public createUser(credentials: RegisterRequest): Observable<RegisterResponse> {
return this.http.post<RegisterResponse>(this.config.apiBase + 'wp/v2/users', JSON.stringify(credentials), { headers: this.myHeaders });
}
现在让我们继续登录:这里它不允许我登录给我一个 403 禁止错误
singIn() {
if (this.credentials.username.length == 0 || this.credentials.password.length == 0) {
this.translate.get('Username_or_Password_cannot_be_empty').subscribe(value => this.uiElementService.presentToast(value));
} else {
this.translate.get('Logging_in').subscribe(value => this.uiElementService.presentLoading(value));
let subscription: Subscription = this.apiService.getAuthToken(this.credentials).subscribe(data => {
let authResponse: AuthResponse = data;
this.authResEmail = authResponse.user_email;
this.authResDisplayName = authResponse.user_display_name;
this.getUser(this.getUserIdFromToken(authResponse.token));
}, err => {
this.uiElementService.dismissLoading();
this.translate.get('Unable_to_login_with_provided_credentials').subscribe(value => this.uiElementService.presentToast(value));
});
this.subscriptions.push(subscription);
}
}
getUser(userId: string) {
let subscription: Subscription = this.apiService.getUser(userId).subscribe(data => {
console.log("getUser", data);
this.uiElementService.dismissLoading();
let userResponse: UserResponse = data;
userResponse.user_email = this.authResEmail;
userResponse.user_display_name = this.authResDisplayName;
window.localStorage.setItem(Constants.USER_KEY, JSON.stringify(userResponse));
this.myEvent.setUserMeData(userResponse);
}, err => {
console.log("getUser", err);
this.uiElementService.dismissLoading();
this.translate.get('Unable_to_login_with_provided_credentials').subscribe(value => this.uiElementService.presentToast(value));
});
this.subscriptions.push(subscription);
}
getUserIdFromToken(token: string): string {
let decodedString: string = window.atob(token.split(".")[1]);
return JSON.parse(decodedString).data.user.id;
}
注册:
register() {
var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{1,4})$/;
if (this.registerRequest.username.length < 4) {
this.translate.get('Enter_username_atleast_char_long').subscribe(value => this.uiElementService.presentToast(value));
} else if (this.registerRequest.email.length <= 5 || !reg.test(this.registerRequest.email)) {
this.translate.get('Enter_valid_email_address').subscribe(value => this.uiElementService.presentToast(value));
// } else if (this.registerRequest.password.length == 0 || !(this.registerRequest.password === this.registerRequestPasswordConfirm)) {
} else if (this.registerRequest.password.length == 0) {
this.translate.get('Enter_valid_passwords_twice').subscribe(value => this.uiElementService.presentToast(value));
} else {
this.translate.get('Registering_user').subscribe(value => this.uiElementService.presentLoading(value));
let subscription: Subscription = this.apiService.createUser(this.registerRequest).subscribe(data => {
let registerResponse: RegisterResponse = data;
this.authResEmail = registerResponse.email;
this.authResDisplayName = registerResponse.username;
this.getUser(String(registerResponse.id));
}, err => {
this.uiElementService.dismissLoading();
this.translate.get('Unable_to_register_with_provided_credentials').subscribe(value => this.uiElementService.presentToast(value));
});
this.subscriptions.push(subscription);
}
}
getUser(userId: string) {
let subscription: Subscription = this.apiService.getUser(userId).subscribe(data => {
this.uiElementService.dismissLoading();
let userResponse: UserResponse = data;
userResponse.user_email = this.authResEmail;
userResponse.user_display_name = this.authResDisplayName;
window.localStorage.setItem(Constants.USER_KEY, JSON.stringify(userResponse));
this.myEvent.setUserMeData(userResponse);
}, err => {
this.uiElementService.dismissLoading();
this.translate.get('Unable_to_register_with_provided_credentials').subscribe(value => this.uiElementService.presentToast(value));
});
this.subscriptions.push(subscription);
}
我已经在 -htaccess 和配置中输入了授权,有人可以帮我吗?我不知道该怎么办
解决方案
推荐阅读
- wordpress - 找不到wordpress页面,wordpress安装在子目录下
- c# - Neo4j 架构迁移?
- ios - UIViewControllerWrapperView 背景颜色问题
- ssis - 如何在 SSIS 中解析文本文件
- android - ConstraintLayout:按顺序动画多个ConstraintSet
- javascript - 如何使用 AJAX 和 PHP 将 JavaScript 变量发布到 MySQL 数据库中?
- android - 如何使按钮上的点击动作可见?
- react-native - 如何在 React Navigation 中为静态 navigationOptions 创建组件?
- python - Django 构建查询的最佳方法是什么
- mysql - 使用 OR 进行查询,使查询执行缓慢