D1ZZYD 23
D1ZZYD 23

Reputation: 439

Retrieving token from Email Angular 6

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

Upvotes: 1

Views: 1528

Answers (2)

SiddAjmera
SiddAjmera

Reputation: 39452

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 }); }

Upvotes: 3

A.Winnen
A.Winnen

Reputation: 1698

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.

Upvotes: 5

Related Questions