【问题标题】:Schema for User table with multi strategy sigup具有多策略 sigup 的用户表的架构
【发布时间】:2015-01-08 09:47:03
【问题描述】:

在处理一个项目时,面临存储不同护照策略(本地、facebook、twitter)的用户信息的问题。 一开始我的UserSchema是这样的:

    User = mongoose.Schema(
    {
        "email" : { type : String , lowercase: true , trim : true , unique : true } ,
        "providerId" : { type : String } ,
        "providerName" : { type : String } ,
        "hashedPassword" : { type : String } ,
        "salt" : { type : String } ,
        "strategy" : { type : String } ,
        "created" : { type : Date , default : new Date().valueOf() } ,
        "lastConnected" : { type : Date , default : new Date().valueOf() } ,
        "accessToken" : { type : String } , 
        "securityToken" : { type : String , default : "" } ,
        "roles" : [ { type : String } ]
    }
);

但是独特的电子邮件会给电子邮件带来问题,因为两个使用 twitter 策略的用户将拥有空电子邮件,这会导致错误。

我想过不要让电子邮件独一无二,但这会带来很多问题(从第一眼看)。 我看到的解决方案之一是为每种策略制作不同的架构,但这很难维护。

有没有其他方法可以解决这个问题。最佳做法是什么?

附:我发誓我用谷歌搜索但没有找到任何解决方案

【问题讨论】:

    标签: javascript node.js mongodb express passport.js


    【解决方案1】:

    您似乎希望能够说 unique:true 并指定 allowNulls。现在这似乎是可能的,你可以在这里看到:https://stackoverflow.com/a/9693138/1935918

    【讨论】:

    • 对于稀疏索引,两个null 条目仍然会违反唯一性约束。但是,如果您不将 email 属性在其为 null 时添加到文档中(即在它具有适当的值时添加它),它将起作用。
    猜你喜欢
    • 1970-01-01
    • 2014-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-06
    • 1970-01-01
    相关资源
    最近更新 更多