我正在使用自己的身份验证来开发API。 我创建了一个从User继承的帐户模型,并创建了一个从AccessToken继承的会话模型。
//帐户
{
"name": "account",
"plural": "accounts",
"base": "User",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"id": {
"type": "string",
"id": true,
"required": true,
"defaultFn": "uuidv4"
},
"name": {
"type": "string"
},
"firstname": {
"type": "string"
},
"lastname": {
"type": "string"
},
"alias": {
"type": "string"
},
"email": {
"type": "string"
},
"birthdate": {
"type": "date"
},
"gender": {
"type": "number"
},
"location": {
"type": "string"
},
"local": {
"type": "string"
},
"company": {
"type": "string"
},
"occupation": {
"type": "string"
},
"raw": {
"type": "string"
},
"providerId": {
"type": "string"
},
"phone": {
"type": "string"
},
"phoneVerified": {
"type": "boolean",
"default": false
},
"emailVerified": {
"type": "boolean",
"default": false
},
"sfid": {
"type": "string"
},
"provider": {
"type": "string",
"default": "local"
}
},
"validations": [],
"relations": {
"roleMappings": {
"type": "hasOne",
"model": "RoleMapping",
"foreignKey": "principalId"
},
"accessTokens": {
"type": "hasMany",
"model": "session",
"foreignKey": "userId",
"options": {
"disableInclude": true
}
}
},
"acls": [
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$unauthenticated",
"permission": "DENY"
},
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$owner",
"permission": "ALLOW"
},
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "Admin",
"permission": "ALLOW"
},
{
"accessType": "EXECUTE",
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "ALLOW",
"property": "signIn"
}
],
"methods": {
"signIn": {
"accepts": [
{
"arg": "provider",
"type": "string",
"required": true,
"description": "",
"http": {
"source": "query"
}
},
{
"arg": "data",
"type": "any",
"required": true,
"description": "",
"http": {
"source": "body"
}
}
],
"returns": [
{
"arg": "token",
"type": "string",
"root": true,
"description": "Auth Token"
}
],
"description": "Method for get Auth Token",
"http": [
{
"path": "/signIn",
"verb": "post"
}
]
}
}
}
//会话
{
"name": "session",
"plural": "sessions",
"base": "AccessToken",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"sfid": {
"type": "string"
},
"providerId": {
"type": "string"
},
"email": {
"type": "string"
},
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"ipaddress": {
"type": "string"
},
"provider": {
"type": "string",
"default": "local"
}
},
"validations": [],
"relations": {
"user": {
"type": "belongsTo",
"model": "account",
"foreignKey": "userId"
}
},
"acls": [
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$unauthenticated",
"permission": "DENY"
},
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$owner",
"permission": "ALLOW"
},
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "Admin",
"permission": "ALLOW"
}
],
"methods": {}
}
我在帐户模型中有一个方法登录,它将验证一系列数据,如果它们有效,则需要创建一个令牌并将其返回。 我的问题是,当我使用create方法时,如果会话表中已经存在一条具有相同userId的记录,则会替换它而不是创建它。
async function storeSession(user) {
var Session = Account.app.models.session;
var result = await Session.upsert({
userId: user.id,
sfid: user.sfid,
providerId: user.providerId,
provider: user.provider,
firstName: user.firstname,
lastName: user.lastname,
email: user.email,
ipaddress: getIpAddress(),
});
return {
token: result.id,
user: {
userId: result.userId,
sfid: result.sfid,
providerId: result.providerId,
provider: result.provider,
firstName: user.firstname,
lastName: user.lastname,
email: result.email,
ipaddress: result.ipaddress
}
}
};
我希望在会话表中创建一条新记录,但是该方法用一个新ID替换具有相同userId的记录。
谢谢