Search code examples
javascriptnode.jsangularexpressnodemailer

Node JS and Angular Email Verification: Anyway to send html in a response?


To start off, I do want to clarify that I know how to use APi's created in NodeJS in Angular. The problem I have is a little tricky.

I have a function that verifies the email used in registering:

exports.confirmEmail = function (req, res) {
    ConfirmToken.findOne({
        token: req.params.token
    }, function (err, token) {

        if (err) {
            return res.status(500).send({
                message: "Internal Server Error " + err
            })
        }

        // token is not found into database i.e. token may have expired 
        if (!token) {
            return res.status(400).send({
                message: 'Your verification link may have expired. Please click on resend for verify your Email.'
            });
        }
        // if token is found then check valid user 
        else {
            Account.findOne({
                _id: token._accountId,
                email: req.params.email
            }, function (err, user) {

                if (err) {
                    return res.status(500).send({
                        message: "Internal Server Error " + err
                    })
                }
                // User does not exist
                if (!user) {
                    return res.status(401).send({
                        message: 'The account does not exist'
                    });
                }
                // user is already verified
                else if (user.isVerified) {
                    return res.status(200).send('User has been already verified. Please Login');
                }
                // verify user
                else {
                    // change isVerified to true
                    user.isVerified = true;
                    user.save(function (err) {
                        // error occur
                        if (err) {
                            return res.status(500).send({
                                message: err.message
                            });
                        }
                        // account successfully verified
                        else {
                            return res.status(200).send('Your account has been successfully verified');
                        }
                    });
                }
            });
        }

    })
}

This is the response I get when I register an account After Registration

Now my question is: is there a way to pass in html code or have it show in a custom Angular component instead of displaying as simple plain text on the web browser as such

Response after clicking verify email


Solution

  • Your service should send a isVerified status back to the client. You are sending only a string at the moment

    return res.status(200).send('Your account has been successfully verified');
    

    based on this status, let's call it, isVerified your angular app would render a isVerfiedComponent.ts or notVerifiedComponent.ts