Rails和Angular,电子邮件确认错误网址

时间:2018-07-19 08:08:37

标签: angular ruby-on-rails-5

当我收到确认电子邮件时,我单击链接并获得空白页,该电子邮件未得到确认,浏览器控制台中没有错误,顺便说一句,所有操作都在本地计算机上进行,但是当我将其加载到服务器上,确认将停止工作。

users_controller.rb

def confirm_email
user = User.find_by_confirm_token(params[:id])
if user
  user.update_attribute(:email_confirmed, true)
  render json: {
    message: 'Email address has already confirmed',
    errors: user.errors.full_messages
  }, status: 200
else
  render json: {
    message: 'Email confirmation failed'
  }, status: 422
end
end

registration_confirmation.html

<p>Hello <%= @user.first_name %>!</p>
<p>
  Thanks for registering! To confirm your registration click the link below.
</p>
<p>
  <%= link_to("Confirm my Email", "#{@origin}/confirm_email?confirmation_token=#{@user.confirm_token}") %>
</p>

角度:

auth.th

checkConfirmationToken(confirmation_token: string): Promise<Object> {
    const url = `${this.baseUrl}/api/users/${confirmation_token}/confirm_email`;
    return this.http.get(url)
      .toPromise()
      .then(res => res.json())
      .catch(error => {
        this.router.navigate(['/login']);
        this.handleError(error, 'Could not confirm email address!');
      });
 }

confirm-email.ts

import { Component,  Inject, forwardRef } from '@angular/core';
import { Router, ActivatedRoute, Params } from '@angular/router';
import { Location }                       from '@angular/common';
import { AuthService }                    from '../services/auth';

@Component({
  templateUrl: '../templates/confirm-email.html'
})

export class ConfirmEmailComponent {
  loading: boolean = true;

  constructor(
    private router: Router,
    private route: ActivatedRoute,
    private location: Location,
    @Inject(forwardRef(() => AuthService)) public _authService: AuthService,
  ) {
    this.checkConfirmationToken();
  }

  checkConfirmationToken() {
    this.route.queryParams
      .subscribe((params: Params) => {
        let token: string = params['confirmation_token'];
        if (params && token) {
          this._authService.checkConfirmationToken(token)
            .then(res => {
              this.loading = false;
            });
        } else {
          this.router.navigate(['/login']);
        }
      });
  }
}

0 个答案:

没有答案