0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-30 20:33:54 -05:00

chore(core): remove unnecessary updateLastSignInAt (#1982)

This commit is contained in:
Darcy Ye 2022-09-26 11:28:18 +08:00 committed by GitHub
parent 649958023d
commit ec3fa4715d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 22 additions and 24 deletions

View file

@ -17,7 +17,7 @@
"add-connector": "node build/cli/add-connector.js", "add-connector": "node build/cli/add-connector.js",
"add-official-connectors": "node build/cli/add-official-connectors.js", "add-official-connectors": "node build/cli/add-official-connectors.js",
"migration-deploy": "node build/cli/migration-deploy.js", "migration-deploy": "node build/cli/migration-deploy.js",
"test": "jest", "test": "jest --testPathIgnorePatterns=/core/connectors/",
"test:coverage": "jest --coverage --silent", "test:coverage": "jest --coverage --silent",
"test:report": "codecov -F core" "test:report": "codecov -F core"
}, },

View file

@ -2,7 +2,7 @@ import { UsersPasswordEncryptionMethod } from '@logto/schemas';
import { hasUserWithId, updateUserById } from '@/queries/user'; import { hasUserWithId, updateUserById } from '@/queries/user';
import { encryptUserPassword, generateUserId, updateLastSignInAt } from './user'; import { encryptUserPassword, generateUserId } from './user';
jest.mock('@/queries/user'); jest.mock('@/queries/user');
@ -66,7 +66,7 @@ describe('updateLastSignIn()', () => {
}); });
it('calls updateUserById with current timestamp', async () => { it('calls updateUserById with current timestamp', async () => {
await updateLastSignInAt('user-id'); await updateUserById('user-id', { lastSignInAt: Date.now() });
expect(updateUserById).toHaveBeenCalledWith( expect(updateUserById).toHaveBeenCalledWith(
'user-id', 'user-id',
expect.objectContaining({ lastSignInAt: new Date('2020-01-01').getTime() }) expect.objectContaining({ lastSignInAt: new Date('2020-01-01').getTime() })

View file

@ -5,7 +5,7 @@ import pRetry from 'p-retry';
import { buildInsertInto } from '@/database/insert-into'; import { buildInsertInto } from '@/database/insert-into';
import envSet from '@/env-set'; import envSet from '@/env-set';
import { findRolesByRoleNames, insertRoles } from '@/queries/roles'; import { findRolesByRoleNames, insertRoles } from '@/queries/roles';
import { findUserByUsername, hasUserWithId, updateUserById } from '@/queries/user'; import { findUserByUsername, hasUserWithId } from '@/queries/user';
import assertThat from '@/utils/assert-that'; import assertThat from '@/utils/assert-that';
import { buildIdGenerator } from '@/utils/id'; import { buildIdGenerator } from '@/utils/id';
import { encryptPassword } from '@/utils/password'; import { encryptPassword } from '@/utils/password';
@ -59,9 +59,6 @@ export const findUserByUsernameAndPassword = async (
return user; return user;
}; };
export const updateLastSignInAt = async (userId: string) =>
updateUserById(userId, { lastSignInAt: Date.now() });
const insertUserQuery = buildInsertInto<CreateUser, User>(Users, { const insertUserQuery = buildInsertInto<CreateUser, User>(Users, {
returning: true, returning: true,
}); });

View file

@ -13,7 +13,6 @@ const updateUserById = jest.fn(async (..._args: unknown[]) => ({ id: 'id' }));
jest.mock('@/lib/user', () => ({ jest.mock('@/lib/user', () => ({
generateUserId: () => 'user1', generateUserId: () => 'user1',
updateLastSignInAt: async (...args: unknown[]) => updateUserById(...args),
insertUser: async (...args: unknown[]) => insertUser(...args), insertUser: async (...args: unknown[]) => insertUser(...args),
})); }));

View file

@ -6,9 +6,10 @@ import { object, string } from 'zod';
import RequestError from '@/errors/RequestError'; import RequestError from '@/errors/RequestError';
import { createPasscode, sendPasscode, verifyPasscode } from '@/lib/passcode'; import { createPasscode, sendPasscode, verifyPasscode } from '@/lib/passcode';
import { assignInteractionResults } from '@/lib/session'; import { assignInteractionResults } from '@/lib/session';
import { generateUserId, insertUser, updateLastSignInAt } from '@/lib/user'; import { generateUserId, insertUser } from '@/lib/user';
import koaGuard from '@/middleware/koa-guard'; import koaGuard from '@/middleware/koa-guard';
import { import {
updateUserById,
hasUserWithEmail, hasUserWithEmail,
hasUserWithPhone, hasUserWithPhone,
findUserByEmail, findUserByEmail,
@ -67,7 +68,7 @@ export default function passwordlessRoutes<T extends AnonymousRouter>(
const { id } = await findUserByPhone(phone); const { id } = await findUserByPhone(phone);
ctx.log(type, { userId: id }); ctx.log(type, { userId: id });
await updateLastSignInAt(id); await updateUserById(id, { lastSignInAt: Date.now() });
await assignInteractionResults(ctx, provider, { login: { accountId: id } }, true); await assignInteractionResults(ctx, provider, { login: { accountId: id } }, true);
return next(); return next();
@ -115,7 +116,7 @@ export default function passwordlessRoutes<T extends AnonymousRouter>(
const { id } = await findUserByEmail(email); const { id } = await findUserByEmail(email);
ctx.log(type, { userId: id }); ctx.log(type, { userId: id });
await updateLastSignInAt(id); await updateUserById(id, { lastSignInAt: Date.now() });
await assignInteractionResults(ctx, provider, { login: { accountId: id } }, true); await assignInteractionResults(ctx, provider, { login: { accountId: id } }, true);
return next(); return next();
@ -163,8 +164,7 @@ export default function passwordlessRoutes<T extends AnonymousRouter>(
const id = await generateUserId(); const id = await generateUserId();
ctx.log(type, { userId: id }); ctx.log(type, { userId: id });
await insertUser({ id, primaryPhone: phone }); await insertUser({ id, primaryPhone: phone, lastSignInAt: Date.now() });
await updateLastSignInAt(id);
await assignInteractionResults(ctx, provider, { login: { accountId: id } }); await assignInteractionResults(ctx, provider, { login: { accountId: id } });
return next(); return next();
@ -212,8 +212,7 @@ export default function passwordlessRoutes<T extends AnonymousRouter>(
const id = await generateUserId(); const id = await generateUserId();
ctx.log(type, { userId: id }); ctx.log(type, { userId: id });
await insertUser({ id, primaryEmail: email }); await insertUser({ id, primaryEmail: email, lastSignInAt: Date.now() });
await updateLastSignInAt(id);
await assignInteractionResults(ctx, provider, { login: { accountId: id } }); await assignInteractionResults(ctx, provider, { login: { accountId: id } });
return next(); return next();

View file

@ -46,7 +46,6 @@ jest.mock('@/queries/user', () => ({
jest.mock('@/lib/user', () => ({ jest.mock('@/lib/user', () => ({
generateUserId: () => 'user1', generateUserId: () => 'user1',
updateLastSignInAt: async (...args: unknown[]) => updateUserById(...args),
insertUser: async (...args: unknown[]) => insertUser(...args), insertUser: async (...args: unknown[]) => insertUser(...args),
})); }));

View file

@ -12,7 +12,7 @@ import {
getUserInfoByAuthCode, getUserInfoByAuthCode,
getUserInfoFromInteractionResult, getUserInfoFromInteractionResult,
} from '@/lib/social'; } from '@/lib/social';
import { generateUserId, insertUser, updateLastSignInAt } from '@/lib/user'; import { generateUserId, insertUser } from '@/lib/user';
import koaGuard from '@/middleware/koa-guard'; import koaGuard from '@/middleware/koa-guard';
import { import {
hasUserWithIdentity, hasUserWithIdentity,
@ -98,8 +98,8 @@ export default function socialRoutes<T extends AnonymousRouter>(router: T, provi
// Update social connector's user info // Update social connector's user info
await updateUserById(id, { await updateUserById(id, {
identities: { ...identities, [target]: { userId: userInfo.id, details: userInfo } }, identities: { ...identities, [target]: { userId: userInfo.id, details: userInfo } },
lastSignInAt: Date.now(),
}); });
await updateLastSignInAt(id);
await assignInteractionResults(ctx, provider, { login: { accountId: id } }); await assignInteractionResults(ctx, provider, { login: { accountId: id } });
return next(); return next();
@ -133,8 +133,8 @@ export default function socialRoutes<T extends AnonymousRouter>(router: T, provi
await updateUserById(id, { await updateUserById(id, {
identities: { ...identities, [target]: { userId: userInfo.id, details: userInfo } }, identities: { ...identities, [target]: { userId: userInfo.id, details: userInfo } },
lastSignInAt: Date.now(),
}); });
await updateLastSignInAt(id);
await assignInteractionResults(ctx, provider, { login: { accountId: id } }); await assignInteractionResults(ctx, provider, { login: { accountId: id } });
return next(); return next();
@ -177,10 +177,10 @@ export default function socialRoutes<T extends AnonymousRouter>(router: T, provi
details: userInfo, details: userInfo,
}, },
}, },
lastSignInAt: Date.now(),
}); });
ctx.log(type, { userId: id }); ctx.log(type, { userId: id });
await updateLastSignInAt(id);
await assignInteractionResults(ctx, provider, { login: { accountId: id } }); await assignInteractionResults(ctx, provider, { login: { accountId: id } });
return next(); return next();

View file

@ -139,6 +139,9 @@ describe('sessionRoutes', () => {
it('assign result and redirect', async () => { it('assign result and redirect', async () => {
interactionDetails.mockResolvedValueOnce({ params: {} }); interactionDetails.mockResolvedValueOnce({ params: {} });
const fakeTime = Date.now();
jest.useFakeTimers().setSystemTime(fakeTime);
const response = await sessionRequest const response = await sessionRequest
.post(registerRoute) .post(registerRoute)
.send({ username: 'username', password: 'password' }); .send({ username: 'username', password: 'password' });
@ -149,6 +152,7 @@ describe('sessionRoutes', () => {
passwordEncrypted: 'password_user1', passwordEncrypted: 'password_user1',
passwordEncryptionMethod: 'Argon2i', passwordEncryptionMethod: 'Argon2i',
roleNames: [], roleNames: [],
lastSignInAt: fakeTime,
}) })
); );
expect(response.body).toHaveProperty('redirectTo'); expect(response.body).toHaveProperty('redirectTo');
@ -158,6 +162,7 @@ describe('sessionRoutes', () => {
expect.objectContaining({ login: { accountId: 'user1' } }), expect.objectContaining({ login: { accountId: 'user1' } }),
expect.anything() expect.anything()
); );
jest.useRealTimers();
}); });
it('register user with admin role for admin console if no active user found', async () => { it('register user with admin role for admin console if no active user found', async () => {

View file

@ -10,11 +10,10 @@ import {
encryptUserPassword, encryptUserPassword,
generateUserId, generateUserId,
findUserByUsernameAndPassword, findUserByUsernameAndPassword,
updateLastSignInAt,
insertUser, insertUser,
} from '@/lib/user'; } from '@/lib/user';
import koaGuard from '@/middleware/koa-guard'; import koaGuard from '@/middleware/koa-guard';
import { hasUser, hasActiveUsers } from '@/queries/user'; import { hasUser, hasActiveUsers, updateUserById } from '@/queries/user';
import assertThat from '@/utils/assert-that'; import assertThat from '@/utils/assert-that';
import { AnonymousRouter } from '../types'; import { AnonymousRouter } from '../types';
@ -46,7 +45,7 @@ export default function usernamePasswordRoutes<T extends AnonymousRouter>(
const { id } = await findUserByUsernameAndPassword(username, password); const { id } = await findUserByUsernameAndPassword(username, password);
ctx.log(type, { userId: id }); ctx.log(type, { userId: id });
await updateLastSignInAt(id); await updateUserById(id, { lastSignInAt: Date.now() });
await assignInteractionResults(ctx, provider, { login: { accountId: id } }, true); await assignInteractionResults(ctx, provider, { login: { accountId: id } }, true);
return next(); return next();
@ -94,8 +93,8 @@ export default function usernamePasswordRoutes<T extends AnonymousRouter>(
passwordEncrypted, passwordEncrypted,
passwordEncryptionMethod, passwordEncryptionMethod,
roleNames, roleNames,
lastSignInAt: Date.now(),
}); });
await updateLastSignInAt(id);
await assignInteractionResults(ctx, provider, { login: { accountId: id } }); await assignInteractionResults(ctx, provider, { login: { accountId: id } });
return next(); return next();