问题
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:
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 }Now in the
VerifyComponentTypeScript Class, you can injectActivatedRouteas a Dependency.import { ActivatedRoute } from '@angular/router' ... constructor(..., private route: ActivatedRoute) {}Now in the the
ngOnInitof 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