为了账号安全,请及时绑定邮箱和手机立即绑定

如何获取经过身份验证的用户的数据/ID?

如何获取经过身份验证的用户的数据/ID?

繁花如伊 2021-12-23 18:05:40
我正在努力解决一个问题,即我的代码应该如何知道哪些是经过身份验证的用户的数据,或者我如何获取这些数据。我已经完成了我的身份验证流程,这意味着我可以使用令牌成功注册和登录。我仍然通过刷新页面登录,我的令牌仍然存储在 ionic/storage 中。我认为这实际上应该足以获取用户身份验证数据的特定用户数据/权限。但我不知道该怎么做。当我从列表中获取任何用户并查看他的个人资料时,我只需传递他的 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);        }));}用户服务.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);    }
查看完整描述

1 回答

?
鸿蒙传说

TA贡献1865条经验 获得超7个赞

更新的答案 您需要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会调用钩子并通过 id 获取用户,并且在您尝试打开当前已验证用户后,仅调用activatedRoute.paramMap订阅的回调而不是所有ngOnInit方法。


查看完整回答
反对 回复 2021-12-23
  • 1 回答
  • 0 关注
  • 262 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号