mirror of
https://github.com/sasjs/server.git
synced 2026-01-08 07:00:04 +00:00
chore(merge): Merge branch 'master' into authentication-with-jwt
This commit is contained in:
26
api/src/utils/saveTokensInDB.ts
Normal file
26
api/src/utils/saveTokensInDB.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import User from '../model/User'
|
||||
|
||||
export const saveTokensInDB = async (
|
||||
userId: number,
|
||||
clientId: string,
|
||||
accessToken: string,
|
||||
refreshToken: string
|
||||
) => {
|
||||
const user = await User.findOne({ id: userId })
|
||||
if (!user) return
|
||||
|
||||
const currentTokenObj = user.tokens.find(
|
||||
(tokenObj: any) => tokenObj.clientId === clientId
|
||||
)
|
||||
if (currentTokenObj) {
|
||||
currentTokenObj.accessToken = accessToken
|
||||
currentTokenObj.refreshToken = refreshToken
|
||||
} else {
|
||||
user.tokens.push({
|
||||
clientId: clientId,
|
||||
accessToken: accessToken,
|
||||
refreshToken: refreshToken
|
||||
})
|
||||
}
|
||||
await user.save()
|
||||
}
|
||||
Reference in New Issue
Block a user