如何为学生、教师、超级管理员等用户身份验证设计数据库
how to database design for user authentication like student, teacher , super admin
我正在尝试为用户创建不同类型的注册。我为用户准备了三个 collection。我一直在老师和学生中引用用户 collection,因为我需要获取电子邮件和密码。
如果教师注册包括电子邮件、密码、名字、姓氏等,则有一个 collection .
如果学生注册包括电子邮件、密码、名字、姓氏等,还有另一个 collection。
我们所有的电子邮件和密码将是一个 collections
用户 - table/collection
- 电子邮件:test@gmail.com
- 密码:asdfasdf
学生 - table /collection
- 名字:“sujon”
老师 - table/collection
- 名字:"sujon"
const UserSchema = mongoose.Schema({
email: {
type: String,
required: true,
},
password: {
type: String,
required: true,
},
isAdmin: {
type: Boolean,
default: false,
},
})
const StudentSchema = mongoose.Schema({
user: {
type: Schema.Types.ObjectId,
ref: 'user',
},
firstname: {
type: String,
},
lastname: {
type: String,
},
photo: {
type: String,
},
education: {
type: String,
},
birth: {
type: Date,
},
sex: {
type: Boolean,
},
})
const TeacherSchema = mongoose.Schema({
user: {
type: mongoose.Schema.ObjectId,
ref: "user"
},
firstname: {
type: String
},
lastname: {
type: String
},
photo: {
type: String
},
designation: {
type: String
},
birth: {
type: Date
},
sex: {
type: Boolean
}
});
如何实现数据库设计
创建单个用户模式就可以了。您可以拥有一个包含所有属性的模式(因为所有三种类型的用户都具有几乎相同的属性),然后添加一个 'roles' 字段,如下所示:
roles: [{ type: String }]
角色字段可以有多个角色 ['Teacher', 'Student' ... ]。通过这种方式,单个用户可以拥有多个角色,例如教师也可以是管理员等。
此外,每当引入新角色时,您都不必创建新模型。
可以根据角色查询用户。然后,角色也可以用于身份验证,例如,角色 'Teacher' 的用户可以创建作业,角色 'Student' 的用户可以提交作业。注册用户时,您可以在 api 或客户端设置某种模型验证以接受特定模型。
我正在尝试为用户创建不同类型的注册。我为用户准备了三个 collection。我一直在老师和学生中引用用户 collection,因为我需要获取电子邮件和密码。
如果教师注册包括电子邮件、密码、名字、姓氏等,则有一个 collection .
如果学生注册包括电子邮件、密码、名字、姓氏等,还有另一个 collection。
我们所有的电子邮件和密码将是一个 collections
用户 - table/collection - 电子邮件:test@gmail.com - 密码:asdfasdf
学生 - table /collection - 名字:“sujon”
老师 - table/collection - 名字:"sujon"
const UserSchema = mongoose.Schema({
email: {
type: String,
required: true,
},
password: {
type: String,
required: true,
},
isAdmin: {
type: Boolean,
default: false,
},
})
const StudentSchema = mongoose.Schema({
user: {
type: Schema.Types.ObjectId,
ref: 'user',
},
firstname: {
type: String,
},
lastname: {
type: String,
},
photo: {
type: String,
},
education: {
type: String,
},
birth: {
type: Date,
},
sex: {
type: Boolean,
},
})
const TeacherSchema = mongoose.Schema({
user: {
type: mongoose.Schema.ObjectId,
ref: "user"
},
firstname: {
type: String
},
lastname: {
type: String
},
photo: {
type: String
},
designation: {
type: String
},
birth: {
type: Date
},
sex: {
type: Boolean
}
});
如何实现数据库设计
创建单个用户模式就可以了。您可以拥有一个包含所有属性的模式(因为所有三种类型的用户都具有几乎相同的属性),然后添加一个 'roles' 字段,如下所示:
roles: [{ type: String }]
角色字段可以有多个角色 ['Teacher', 'Student' ... ]。通过这种方式,单个用户可以拥有多个角色,例如教师也可以是管理员等。
此外,每当引入新角色时,您都不必创建新模型。
可以根据角色查询用户。然后,角色也可以用于身份验证,例如,角色 'Teacher' 的用户可以创建作业,角色 'Student' 的用户可以提交作业。注册用户时,您可以在 api 或客户端设置某种模型验证以接受特定模型。