Retrieving token from Email Angular 6

谁说胖子不能爱 提交于 2019-12-24 10:23:01

问题


When a user register an email is sent to them from the API with a token to verify the user. an endpoint of the API requires the token to verify the user, how do I get the token and post it to the API


回答1:


Include a clickable url in your registrationmail which includes the token as query parameter. e.g. https://website.com/confirmRegistration?token=insertTokenHere

then create a component and a route for /confirmRegistration, inject ActivatedRoute into that component and read the token with this.route.snapshot.queryParamMap.get('token');. use the Value to call the API endpoint.




回答2:


Assuming that you have a URL like this in your Email:

https://www.hello.com/verify?token=YOUR_TOKEN_HERE

Let's break it down into steps:

  1. You might have routing implemented that will load a Component for a Specific Route. So you might have something like this:

    { path: 'verify', component: VerifyComponent }

  2. Now in the VerifyComponent TypeScript Class, you can inject ActivatedRoute as a Dependency.

    import { ActivatedRoute } from '@angular/router' ... constructor(..., private route: ActivatedRoute) {}

  3. Now in the the ngOnInit of this Component, you can have access to the token like this:

    ngOnInit() { this.route.queryParams.subscribe(queryParams => { const token = queryParams['token']; // Call your Backend API with the token after this }); }



来源:https://stackoverflow.com/questions/53222039/retrieving-token-from-email-angular-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!