node.js - How to deal with roles on a MVC nodejs service (jsonwebtoken,mongoose) -
i developing service using mvc pattern support of nodejs, jsonwebtoken, , mongodb (mongoose). in application, have modeled 2 main actors: normal users (that can sign-up via facebook, amazon or local signup), drivers (sign-up through website). have model transactions between user & drivers can modified crud paradigm users , drivers.
here post models of users , drivers:
var userschema = mongoose.schema({ name: string, surname: string, email: string, password: { type: string, required: true } }); var driverschema = mongoose.schema({ name: { type: string, required: true }, surname: { type: string, required: true }, email: { type:string, required: true, unique:true }, password: { type: string, required: true, minlength: minimum } });
my issue in authentication , data access endpoints. how can create distinction on token between users , drivers? should add information payload of token?
well, there several approaches solving issue. 1 of approaches sign user's token role. since have separate authentication logic different types, can sign token specific usertype.
for driver, code this:
var drivertoken = jwt.sign({email:'xxx@gmail.com', usertype: 'driver'}, 'your_secret');
for normal user, code this:
var normaltoken = jwt.sign({email:'xxx@gmail.com', usertype: 'normal'}, 'your_secret');
now when verifying token, can this:
var user = jwt.verify(token, 'your_secret'); if(user.usertype === 'driver') { //hey, driver! } else if(user.usertype === 'normal') { //hey, normal... }
also, noticed driver , user schema same. suggest take @ mongoose's discriminators
. basically, discriminators
schema inheritance mechanism mongoose.
Comments
Post a Comment