Erdogan Cihan
Erdogan Cihan

Reputation: 26

React and Loopback 3 verify user email

I am built a sign up component by using React and Loopback 3. At first I was not using email verification. In the code below I was getting response from rest api about user creation.

export function signUpUser(user) {
  let userEmail = user.email;
  let userPassword = user.password;
  let userName = user.userName;
  let privilege="user"
  let credentials = {
    email: userEmail,
    password: userPassword,
    userName: userName,
    privilege:privilege
  };
  //console.log(credentials);
  return dispatch => {
    axios
      .request({
        method: "post",
        url: loopBack + "/blogusers",
        data: credentials
      })
      .then(response => {
        console.log(response)
        return dispatch(signUpSuccess(response.data));
      })
     .catch(error => {
        return dispatch(signUpFailure(error));
      });
  };
}

My user model js was that

"use strict";
module.exports = function(Bloguser) {

};

After I add hook to create api my user model js is that.

"use strict";
var config = require("../../server/config.json");
var path = require("path");
var senderAddress = "[email protected]";

module.exports = function(Bloguser) {
  //send verification email after registration.

  Bloguser.afterRemote("create", function(context, userInstance, next) {
    console.log(">user.afterRemote triggered");
    var options = {
      type: "email",
      to: userInstance.email,
      from: senderAddress,
      subject: "Thanks for registering.",
      template: path.resolve(__dirname, "../../server/views/verify.ejs"),
      Bloguser: userInstance
    };
    userInstance.verify(options, function(err, response) {
      if (err) {
        Bloguser.deleteById(userInstance.id);
        return next(err);
      }
      console.log(">verification email sent.", response);
      context.res.render("response", {
        title: "Signed up successfully",
        content:
          "Please check your email and click on the verification link" +
          "before logging in.",
        redirectTo: "/login",
        redirectToLinkText: "Log in"
      });
    });
  });
};

By this code I can successfully send verification email and I can verify user by the link in the email. However I cannot get response from the Post api in the signUpUser() function. I need to get response to show the user status of his/her sign up process

Upvotes: 0

Views: 388

Answers (1)

Erdogan Cihan
Erdogan Cihan

Reputation: 26

I changed

context.res.render("response", {
    title: "Signed up successfully",
    content:
      "Please check your email and click on the verification      link" +
      "before logging in.",
    redirectTo: "/login",
    redirectToLinkText: "Log in"
  });

with

context.res.status(200).send(userInstance)

And Voile it worked.

Upvotes: 0

Related Questions