提交 c99d592c 编写于 作者: 雪洛's avatar 雪洛

feat: auto logout while login, register with token

上级 ce3348ac
......@@ -8,6 +8,9 @@ const {
const {
ERROR
} = require('../../common/error')
const {
logout
} = require('./logout')
const PasswordUtils = require('./password')
async function realPreLogin (params = {}) {
......@@ -173,10 +176,14 @@ async function postLogin (params = {}) {
extraData,
isThirdParty = false
} = params
const {
clientIP,
uniIdToken
} = this.getClientInfo()
const uid = user._id
const updateData = {
last_login_date: Date.now(),
last_login_ip: this.getClientInfo().clientIP,
last_login_ip: clientIP,
...extraData
}
const {
......@@ -185,6 +192,13 @@ async function postLogin (params = {}) {
} = await this.uniIdCommon.createToken({
uid
})
if (uniIdToken) {
try {
await logout.call(this)
} catch (error) {}
}
await userCollection.doc(uid).update(updateData)
await this.middleware.uniIdLog({
data: {
......
const {
dbCmd,
LOG_TYPE,
deviceCollection,
userCollection
} = require('../../common/constants')
async function logout() {
const {
uniIdToken,
deviceId
} = this.getClientInfo()
const {
uid
} = await this.uniIdCommon.checkToken(
uniIdToken,
{
autoRefresh: false
}
)
// 删除token
await userCollection.doc(uid).update({
token: dbCmd.pull(uniIdToken)
})
// 仅当device表的device_id和user_id均对应时才进行更新
await deviceCollection.where({
device_id: deviceId,
user_id: uid
}).update({
token_expired: 0
})
await this.middleware.uniIdLog({
data: {
user_id: uid
},
type: LOG_TYPE.LOGOUT
})
return {
errCode: 0
}
}
module.exports = {
logout
}
\ No newline at end of file
......@@ -12,10 +12,13 @@ const {
getValidInviteCode,
generateInviteInfo
} = require('./fission')
const {
logout
} = require('./logout')
const PasswordUtils = require('./password')
const merge = require('lodash.merge')
async function realPreRegister (params = {}) {
async function realPreRegister(params = {}) {
const {
user
} = params
......@@ -30,7 +33,7 @@ async function realPreRegister (params = {}) {
}
}
async function preRegister (params = {}) {
async function preRegister(params = {}) {
try {
await realPreRegister.call(this, params)
} catch (error) {
......@@ -42,7 +45,7 @@ async function preRegister (params = {}) {
}
}
async function preRegisterWithPassword (params = {}) {
async function preRegisterWithPassword(params = {}) {
const {
user,
password
......@@ -69,7 +72,7 @@ async function preRegisterWithPassword (params = {}) {
}
}
async function thirdPartyRegister ({
async function thirdPartyRegister({
user = {}
} = {}) {
return {
......@@ -78,7 +81,7 @@ async function thirdPartyRegister ({
}
}
async function postRegister (params = {}) {
async function postRegister(params = {}) {
const {
user,
extraData = {},
......@@ -93,7 +96,8 @@ async function postRegister (params = {}) {
channel,
scene,
clientIP,
osName
osName,
uniIdToken
} = this.getClientInfo()
merge(user, extraData)
......@@ -147,6 +151,12 @@ async function postRegister (params = {}) {
user.invite_time = inviteTime
}
if (uniIdToken) {
try {
await logout.call(this)
} catch (error) {}
}
const beforeRegister = this.hooks.beforeRegister
let userRecord = user
if (beforeRegister) {
......
const {
dbCmd,
LOG_TYPE,
deviceCollection,
userCollection
} = require('../../common/constants')
logout
} = require('../../lib/utils/logout')
/**
* 用户退出登录
......@@ -11,37 +8,7 @@ const {
* @returns
*/
module.exports = async function () {
const {
uniIdToken,
deviceId
} = this.getClientInfo()
const {
uid
} = await this.uniIdCommon.checkToken(
uniIdToken,
{
autoRefresh: false
}
)
// 删除token
await userCollection.doc(uid).update({
token: dbCmd.pull(uniIdToken)
})
// 仅当device表的device_id和user_id均对应时才进行更新
await deviceCollection.where({
device_id: deviceId,
user_id: uid
}).update({
token_expired: 0
})
await this.middleware.uniIdLog({
data: {
user_id: uid
},
type: LOG_TYPE.LOGOUT
})
await logout.call(this)
return {
errCode: 0
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册