Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8749529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:42:32+00:00 2026-06-13T12:42:32+00:00

I need to maintain a collection ‘users’ of [User] documents for implementing an authentication

  • 0

I need to maintain a collection ‘users’ of [User] documents for implementing an authentication mechanism for my site.

[User] ~ {color: string, username:string, password-hash:string, password-salt: string}

Additionally, each [User] can be one of 3 colors – [RedUser], [BlueUser] and [GreenUser] and will have differing schemas depending on color. All extending from the [User] schema:

[RedUser] ~ Union( [User],  {redfield:string} )
[GreenUser] ~ Union( [User],  {greenfield:boolean} )
[BlueUser] ~ Union( [User],  {bluefield:number} )

e.g. a green user – {color:’green’,username:’bob’,password-hash:’1313a…’,password-salt:’…’}

For authentication I want to query the ‘users’ collection using the general [User] schema, as all I need to know is the password-hash.

However, I would like a function as follows (pseudocode):

Authenticate( username, password )
    userDoc <- users.findOne({username:username})
    if badPassword(usersDoc.password-hash,password) throw error
    else
        if userDoc.color=='red'
            return recast(userDoc, RedUser)
        if userDoc.color=='green'
            return recast(userDoc, BlueUser)
        if userDoc.color=='blue'
            return recast(userDoc, GreenUser)

So how could I do the up-casting from a [User] document to a [RedUser] for example?

I am using Node.js/MongooseJs/Coffeescript

thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:42:33+00:00Added an answer on June 13, 2026 at 12:42 pm

    Pretty simple really, we just make use of the tiny but useful mongoose-schema-extend package from https://github.com/briankircho/mongoose-schema-extend

    ###
        Dependencies.
    ###
    
    mongoose = require('mongoose')
    extend = require('mongoose-schema-extend')
    
    ###
        Main
    ### 
    
    class Demo
        constructor: (dbUrl)->
            @db = mongoose.createConnection( dbUrl )
    
            userSchema = new mongoose.Schema
                username                    : { type: String, default: "" }                 
                password_hash               : { type: String, default: "" }     
                ,
                {collection : 'users',  discriminatorKey : 'color'}
    
            userSchema.methods.verifyPassword = (pass)->
                console.log("is it a good password? " + (this.password_hash==pass))
    
            redSchema = userSchema.extend
                redfield                : { type: String, default: "red" }          
            redSchema.methods.printColor = ->
                console.log("RED!") 
    
            blueSchema = userSchema.extend
                bluefield           : { type: String, default: "blue" }             
            blueSchema.methods.printColor = ->
                console.log("BLUE!")        
    
            greenSchema = userSchema.extend
                greenfield          : { type: String, default: "green" }                
            greenSchema.methods.printColor = ->
                console.log("GREEN!")               
    
            # create the mongoose db models
            @UserModel = @db.model('User', userSchema)
            @RedModel = @db.model('Red', redSchema)
            @BlueModel = @db.model('Blue', blueSchema)      
            @GreenModel = @db.model('Green', greenSchema)
    
        populate: ->
            user1 = new @RedModel({username:'user1',password_hash:'pass1',redfield:'blabla'})
            user1.save()
            user2 = new @GreenModel({username:'user2',password_hash:'pass2',greenfield:'blabla2'})
            user2.save()    
            user3 = new @RedModel({username:'user3',password_hash:'pass3',redfield:'blabla3'})
            user3.save()        
            user4 = new @GreenModel({username:'user4',password_hash:'pass1',greenfield:'dflabla'})
            user4.save()
            user5 = new @BlueModel({username:'user5',password_hash:'pass2',bluefield:'blabla2'})
            user5.save()    
            user6 = new @BlueModel({username:'user6',password_hash:'pass3',bluefield:'blabla3'})
            user6.save()                
    
        auth:(username,callback)->
            @UserModel.findOne({ username: username }, callback)
    
    ###
        Start the demo
    ###
    
    demo = new Demo('mongodb://localhost/testpolymorph')
    demo.populate()
    demo.auth 'user5', (err,userDoc)->  
        userDoc.verifyPassword("pass")
        userDoc.printColor()
    demo.auth 'user3', (err,userDoc)->
        userDoc.verifyPassword("pass3") 
        userDoc.printColor()
    demo.auth 'user4', (err,userDoc)->
        userDoc.verifyPassword("pass3") 
        userDoc.printColor()    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to maintain lists of 40 recent added, most popular/ most liked items
There is a project that I need to maintain that talks to mysql via
We have an Excel application that I need to maintain that has shown his
Folks, I need to maintain a C#/.Net desktop application. So, I need to set
I was advised in a previous question that I didn't need to maintain the
I need to print XML that I manipulated using groovy and maintain the attribute
Calling image = Image.open(data) image.thumbnail((36,36), Image.NEAREST) will maintain the aspect ratio. But I need
I need a collection data-structure that can do the following: Be sorted Allow me
I have a collection of data. I need to insert these records into SQL
I have a custom list on the top level site of my collection and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.