Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a referral link in nodejs

Am trying to figure out a way of creating a unique referral link that directs to my site for each user that signs up for my site, but to be sincere i don't really know how it works out for sure, I have tried googling for it, but i can't find a perfect answer for it, any plugin needed or any way around that.

The user model, no much code i don't have any idea of this

var UserSchema = new mongoose.Schema({
    username: String,
    password: String,
    referralLink: String
})

UserSchema.plugin(passortLocalMongoose);
module.exports = mongoose.model("User", UserSchema ) 

user sign up

router.post("/register", function(req,res){
    var newUser = new User({username: req.body.username,referral: req.body.referral});

    User.register(newUser, req.body.password, function(error, user){
        if(error){
            console.log(error)
            req.flash("signerror", error.message)
            return res.redirect("/register")
        }
        //Do the referral link creation stuff here, don't know for sure

    }) 
})

if there is a way for this, would be glad if you help out

like image 920
Fillipo Sniper Avatar asked Mar 31 '18 08:03

Fillipo Sniper


1 Answers

How to create a referral link?

Let this be a platform/language agnostic how-to:

  • Every time new User is created, generate unique link for him to be his unique referral link and store it in DB (For simplicity this could be _id which is unique in MongoDB and is auto-generated - in this case you don't have to create another field). Let's also keep count of how many people registered through this person's link, let it be referredCount.

    Referral link could look for example like this: /register?referrer=${_id}, where ${_id} is user unique ID.

  • Send referral link to user, so he can pass it along to other people, or make it visible in his /account page - obvious step
  • When someone registers via this link, create new User as usual and after succesful creation, get req.query.referrer from the URL, look into DB for user with this particular ID and increase his referredCount.

Note that it's just a way of doing that, I'm sure there might be many different strategies for it.

like image 94
Tomasz Bubała Avatar answered Sep 22 '22 12:09

Tomasz Bubała