From 2a9cc5e7a27739c210a012caa2d85db61d85836c Mon Sep 17 00:00:00 2001 From: zamelane Date: Sun, 24 Nov 2024 12:39:07 +0700 Subject: [PATCH 1/2] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20=D0=BE=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=B4=D0=B0=D1=82=D1=8B=20=D0=BF=D0=BE=D1=81=D0=BB?= =?UTF-8?q?=D0=B5=D0=B4=D0=BD=D0=B5=D0=B3=D0=BE=20=D0=B8=D1=81=D0=BF=D0=BE?= =?UTF-8?q?=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F=20=D1=82?= =?UTF-8?q?=D0=BE=D0=BA=D0=B5=D0=BD=D0=B0=20=D0=BF=D1=80=D0=B8=20=D0=B5?= =?UTF-8?q?=D0=B3=D0=BE=20=D0=B8=D0=B7=D0=B2=D0=BB=D0=B5=D1=87=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + apps/api/.env.example | 1 - apps/api/src/helpers/getCookieFromToken.ts | 7 ++++++- .../Auth/actions/save/authSaveUseDate.ts | 15 +++++++++++++++ .../api/src/models/Auth/actions/save/index.ts | 1 + bun.lockb | Bin 147600 -> 147600 bytes 6 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 apps/api/src/models/Auth/actions/save/authSaveUseDate.ts create mode 100644 apps/api/src/models/Auth/actions/save/index.ts diff --git a/.gitignore b/.gitignore index d10453d9..6b067e94 100755 --- a/.gitignore +++ b/.gitignore @@ -42,3 +42,4 @@ coverage /target /Cargo.lock /apps/web/build.zip +bun.lockb \ No newline at end of file diff --git a/apps/api/.env.example b/apps/api/.env.example index d48edb43..d08d51fb 100755 --- a/apps/api/.env.example +++ b/apps/api/.env.example @@ -12,7 +12,6 @@ DATABASE_PORT=5432 DATABASE_NAME='databaseName' DATABASE_USERNAME='user' DATABASE_PASSWORD='password' -ENCRYPT_KEY='MySuperPassword!MySuperPassword!' POSTGRES_USER=postgres POSTGRES_PW=postgres diff --git a/apps/api/src/helpers/getCookieFromToken.ts b/apps/api/src/helpers/getCookieFromToken.ts index 89b18235..17a1cb76 100755 --- a/apps/api/src/helpers/getCookieFromToken.ts +++ b/apps/api/src/helpers/getCookieFromToken.ts @@ -1,10 +1,12 @@ import { API_ERRORS, ApiError } from '@api' -import type { DiaryUserId, Nullable } from '@diary-spo/shared' +import type { Nullable } from '@diary-spo/shared' import { caching } from 'cache-manager' import { AuthModel, type AuthModelType } from '../models/Auth' import { DiaryUserModel, type DiaryUserModelType } from '../models/DiaryUser' import { GroupModel, type GroupModelType } from '../models/Group' import { SPOModel, type SPOModelType } from '../models/SPO' +import { formatDate } from '@utils' +import { authSaveUseDate } from 'src/models/Auth/actions/save' const memoryCache = await caching('memory', { max: 1000, @@ -75,6 +77,9 @@ export const getCookieFromToken = async ( throw new ApiError(API_ERRORS.INVALID_TOKEN, 401) } + if(diaryUserAuth.lastUsedDate !== formatDate(new Date())) + authSaveUseDate(token, new Date()) + const user = diaryUserAuth.diaryUser const spoId = user.group.spo.id diff --git a/apps/api/src/models/Auth/actions/save/authSaveUseDate.ts b/apps/api/src/models/Auth/actions/save/authSaveUseDate.ts new file mode 100644 index 00000000..2d9f2552 --- /dev/null +++ b/apps/api/src/models/Auth/actions/save/authSaveUseDate.ts @@ -0,0 +1,15 @@ +import { formatDate } from '@utils' +import { AuthModel, IAuthModel } from '../../model' + +export const authSaveUseDate = async ( + token: string, + date: Date +): Promise<[affectedCount: number]> => + AuthModel.update({ + lastUsedDate: formatDate(date) + }, + { + where: { + token + } + }) \ No newline at end of file diff --git a/apps/api/src/models/Auth/actions/save/index.ts b/apps/api/src/models/Auth/actions/save/index.ts new file mode 100644 index 00000000..aae028d5 --- /dev/null +++ b/apps/api/src/models/Auth/actions/save/index.ts @@ -0,0 +1 @@ +export * from './authSaveUseDate' \ No newline at end of file diff --git a/bun.lockb b/bun.lockb index cb95cfe4d42a339fa2511a2f95ca00a029c22095..56d01f697eae1a25a3cd94e7633e0e5a54b672c0 100755 GIT binary patch delta 46 zcmbQx$T^{rvtbLP;C>ln6NaSHJO&Ur(0Fg<*?ZPklo;a-^$hilO}6XqXH0Gc0BkQ0 AvH$=8 delta 46 zcmbQx$T^{rvtbLP;C>k+ONOM Date: Sun, 24 Nov 2024 12:42:41 +0700 Subject: [PATCH 2/2] =?UTF-8?q?=D0=92=D0=B5=D1=80=D0=BD=D1=83=D1=82=D1=8C?= =?UTF-8?q?=20bun.lockb=20=D0=B8=D0=B7=20=D0=B8=D0=B3=D0=BD=D0=BE=D1=80?= =?UTF-8?q?=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +-- apps/api/src/helpers/getCookieFromToken.ts | 6 +++--- .../src/models/Auth/actions/save/authSaveUseDate.ts | 10 ++++++---- apps/api/src/models/Auth/actions/save/index.ts | 2 +- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 6b067e94..438b522c 100755 --- a/.gitignore +++ b/.gitignore @@ -41,5 +41,4 @@ coverage /target /Cargo.lock -/apps/web/build.zip -bun.lockb \ No newline at end of file +/apps/web/build.zip \ No newline at end of file diff --git a/apps/api/src/helpers/getCookieFromToken.ts b/apps/api/src/helpers/getCookieFromToken.ts index 17a1cb76..b07f649a 100755 --- a/apps/api/src/helpers/getCookieFromToken.ts +++ b/apps/api/src/helpers/getCookieFromToken.ts @@ -1,12 +1,12 @@ import { API_ERRORS, ApiError } from '@api' import type { Nullable } from '@diary-spo/shared' +import { formatDate } from '@utils' import { caching } from 'cache-manager' +import { authSaveUseDate } from 'src/models/Auth/actions/save' import { AuthModel, type AuthModelType } from '../models/Auth' import { DiaryUserModel, type DiaryUserModelType } from '../models/DiaryUser' import { GroupModel, type GroupModelType } from '../models/Group' import { SPOModel, type SPOModelType } from '../models/SPO' -import { formatDate } from '@utils' -import { authSaveUseDate } from 'src/models/Auth/actions/save' const memoryCache = await caching('memory', { max: 1000, @@ -77,7 +77,7 @@ export const getCookieFromToken = async ( throw new ApiError(API_ERRORS.INVALID_TOKEN, 401) } - if(diaryUserAuth.lastUsedDate !== formatDate(new Date())) + if (diaryUserAuth.lastUsedDate !== formatDate(new Date())) authSaveUseDate(token, new Date()) const user = diaryUserAuth.diaryUser diff --git a/apps/api/src/models/Auth/actions/save/authSaveUseDate.ts b/apps/api/src/models/Auth/actions/save/authSaveUseDate.ts index 2d9f2552..ce759cca 100644 --- a/apps/api/src/models/Auth/actions/save/authSaveUseDate.ts +++ b/apps/api/src/models/Auth/actions/save/authSaveUseDate.ts @@ -5,11 +5,13 @@ export const authSaveUseDate = async ( token: string, date: Date ): Promise<[affectedCount: number]> => - AuthModel.update({ - lastUsedDate: formatDate(date) - }, + AuthModel.update( + { + lastUsedDate: formatDate(date) + }, { where: { token } - }) \ No newline at end of file + } + ) diff --git a/apps/api/src/models/Auth/actions/save/index.ts b/apps/api/src/models/Auth/actions/save/index.ts index aae028d5..d352a3d8 100644 --- a/apps/api/src/models/Auth/actions/save/index.ts +++ b/apps/api/src/models/Auth/actions/save/index.ts @@ -1 +1 @@ -export * from './authSaveUseDate' \ No newline at end of file +export * from './authSaveUseDate'