Reputation: 716
After registration user get a link, which consist of userId and token. When user click on it - angular project opens, then angular takes userId and token from link, and send a post method to backend for verifying email.
Link example
Post method, which verify email:
[HttpPost("confirmEmail/{userId}")]
public async Task<IActionResult> ConfirmEmail(string userId, [FromBody]string token)
{
var codeDecodedBytes = WebEncoders.Base64UrlDecode(token);
var codeDecoded = Encoding.UTF8.GetString(codeDecodedBytes);
var user = await _userManager.FindByIdAsync(userId);
var result = await _userManager.ConfirmEmailAsync(user, codeDecoded);
return Ok(result);
}
Getting userId and token in Angular
userId: string;
token: string;
constructor(private activatedRoute: ActivatedRoute, private authService: AuthService) {
this.activatedRoute.queryParams.subscribe(params => {
this.userId = params['userId'];
this.token = params['token'];
});
}
ngOnInit(): void {
this.confirmEmail();
}
confirmEmail(){
this.authService.confirmEmail(this.userId, this.token).subscribe(data => { console.log("success")});
}
(AuthService) Sending data to backend
confirmEmail(userId: string, token: string){
console.log(userId);
console.log(token);
return this.http.post(this.authUrl + `confirmemail/${userId}`, token);
}
Upvotes: 1
Views: 476
Reputation: 38199
Try to set Content-Type
to json
type:
confirmEmail(userId: string, token: string) {
const body = JSON.stringify(token);
const options = {
headers: new HttpHeaders().append('Content-Type', 'application/json')
};
return this.http.post(this.authUrl + `confirmemail/${userId}`, body, options)
.pipe(
map(res => res)
, catchError(this.handleError())
);
}
Upvotes: 2