如何获取认证用户的data/id?
How to get the data/id of the authenticated user?
我正在努力解决有关我的代码应该如何知道哪些是经过身份验证的用户的数据或我如何获取该数据的问题。
我已经完成了身份验证流程,这意味着我可以使用令牌成功注册和登录。我仍然通过刷新页面登录,我的令牌仍然存储在 ionic/storage.
我认为这实际上应该足以获取用户身份验证数据的特定用户data/permissions。但我不知道该怎么做。
当我从列表中获取任何用户并查看他的个人资料时,我可以简单地传递他的 ID。如何通过单击配置文件选项卡获取经过身份验证的用户的 ID?
我不是来自另一个我可以获取 ID 的页面。我还放了我的 auth.service 代码,如果这对你有帮助,以便因为令牌而帮助我,但重要的是最后两个片段(我认为如此)。
这是我的代码:
auth.service.ts
private token = null;
user = null;
authenticationState = new BehaviorSubject(false);
constructor(private http: HttpClient, private alertCtrl: AlertController, private storage: Storage, private helper: JwtHelperService,
private plt: Platform) {
this.plt.ready().then(() => {
this.checkToken();
});
}
checkToken() {
this.storage.get(TOKEN_KEY).then(access => {
if (access) {
this.user = this.helper.decodeToken(access);
this.authenticationState.next(true);
}
});
}
apilogin(username: string, password: string) {
return this.http.post<any>(`${this.url}/api/token/`, { username, password })
.pipe(
tap(res => {
this.storage.set(TOKEN_KEY, res['access']);
this.storage.set(USERNAME_KEY, username);
this.user = this.helper.decodeToken(res['access']);
console.log('my user: ', this.user);
this.authenticationState.next(true);
}),
catchError(e => {
this.showAlert('Oops smth went wrong!');
throw new Error(e);
}));
}
user.service.ts
// get a user's profile
getUserDetails(id: number): Observable<any> {
return this.http.get(`${this.url}/users/${id}/`);
}
profile.ts
information = null;
id: number;
ngOnInit() {
// How to get just the authenticated api?
this.activatedRoute.paramMap.subscribe(params => {
this.id = validateId(params.get('id'));
});
function validateId(id: any): number {
return parseInt(id);
}
// Get the information from the API
this.userService.getUserDetails(this.id).subscribe(result => {
this.information = result;
});
}
更新答案
您需要像这样
将 user_id
保存到存储中
this.storage.set(USER_ID, this.validateId(res['user_id']))
并在当前用户配置文件组件中获取此 user_id
并将其传递给 this.userService.getUserDetails
方法,如:
const currentUserId = this.storage.get(USER_ID);
this.userService.getUserDetails(currentUserId).subscribe(user => {
console.log('user data', user);
});
旧答案
假设任何用户的配置文件都在同一路由 (ex: profile/:userId)
上,包括当前经过身份验证的用户
您只需在 activatedRoute.paramMap
、
的订阅内添加此调用
尝试
ngOnInit() {
// How to get just the authenticated api?
this.activatedRoute.paramMap.subscribe(params => {
this.id = validateId(params.get('id'));
// Get the information from the API
this.userService.getUserDetails(this.id).subscribe(result => {
this.information = result;
});
});
function validateId(id: any): number {
return parseInt(id);
}
}
让我解释一下发生了什么,当你进入这个组件时,ngOnInit
hook被调用并通过id获取用户,当你尝试打开当前认证用户后,只有[=17=的回调] 订阅调用的不是所有的ngOnInit
方法。
我正在努力解决有关我的代码应该如何知道哪些是经过身份验证的用户的数据或我如何获取该数据的问题。
我已经完成了身份验证流程,这意味着我可以使用令牌成功注册和登录。我仍然通过刷新页面登录,我的令牌仍然存储在 ionic/storage.
我认为这实际上应该足以获取用户身份验证数据的特定用户data/permissions。但我不知道该怎么做。
当我从列表中获取任何用户并查看他的个人资料时,我可以简单地传递他的 ID。如何通过单击配置文件选项卡获取经过身份验证的用户的 ID?
我不是来自另一个我可以获取 ID 的页面。我还放了我的 auth.service 代码,如果这对你有帮助,以便因为令牌而帮助我,但重要的是最后两个片段(我认为如此)。
这是我的代码:
auth.service.ts
private token = null;
user = null;
authenticationState = new BehaviorSubject(false);
constructor(private http: HttpClient, private alertCtrl: AlertController, private storage: Storage, private helper: JwtHelperService,
private plt: Platform) {
this.plt.ready().then(() => {
this.checkToken();
});
}
checkToken() {
this.storage.get(TOKEN_KEY).then(access => {
if (access) {
this.user = this.helper.decodeToken(access);
this.authenticationState.next(true);
}
});
}
apilogin(username: string, password: string) {
return this.http.post<any>(`${this.url}/api/token/`, { username, password })
.pipe(
tap(res => {
this.storage.set(TOKEN_KEY, res['access']);
this.storage.set(USERNAME_KEY, username);
this.user = this.helper.decodeToken(res['access']);
console.log('my user: ', this.user);
this.authenticationState.next(true);
}),
catchError(e => {
this.showAlert('Oops smth went wrong!');
throw new Error(e);
}));
}
user.service.ts
// get a user's profile
getUserDetails(id: number): Observable<any> {
return this.http.get(`${this.url}/users/${id}/`);
}
profile.ts
information = null;
id: number;
ngOnInit() {
// How to get just the authenticated api?
this.activatedRoute.paramMap.subscribe(params => {
this.id = validateId(params.get('id'));
});
function validateId(id: any): number {
return parseInt(id);
}
// Get the information from the API
this.userService.getUserDetails(this.id).subscribe(result => {
this.information = result;
});
}
更新答案 您需要像这样
将user_id
保存到存储中
this.storage.set(USER_ID, this.validateId(res['user_id']))
并在当前用户配置文件组件中获取此 user_id
并将其传递给 this.userService.getUserDetails
方法,如:
const currentUserId = this.storage.get(USER_ID);
this.userService.getUserDetails(currentUserId).subscribe(user => {
console.log('user data', user);
});
旧答案
假设任何用户的配置文件都在同一路由 (ex: profile/:userId)
上,包括当前经过身份验证的用户
您只需在 activatedRoute.paramMap
、
尝试
ngOnInit() {
// How to get just the authenticated api?
this.activatedRoute.paramMap.subscribe(params => {
this.id = validateId(params.get('id'));
// Get the information from the API
this.userService.getUserDetails(this.id).subscribe(result => {
this.information = result;
});
});
function validateId(id: any): number {
return parseInt(id);
}
}
让我解释一下发生了什么,当你进入这个组件时,ngOnInit
hook被调用并通过id获取用户,当你尝试打开当前认证用户后,只有[=17=的回调] 订阅调用的不是所有的ngOnInit
方法。