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-official-connectors": "node build/cli/add-official-connectors.js",
"migration-deploy": "node build/cli/migration-deploy.js",
"test": "jest",
"test": "jest --testPathIgnorePatterns=/core/connectors/",
"test:coverage": "jest --coverage --silent",
"test:report": "codecov -F core"
},

View file

@ -2,7 +2,7 @@ import { UsersPasswordEncryptionMethod } from '@logto/schemas';
import { hasUserWithId, updateUserById } from '@/queries/user';
import { encryptUserPassword, generateUserId, updateLastSignInAt } from './user';
import { encryptUserPassword, generateUserId } from './user';
jest.mock('@/queries/user');
@ -66,7 +66,7 @@ describe('updateLastSignIn()', () => {
});
it('calls updateUserById with current timestamp', async () => {
await updateLastSignInAt('user-id');
await updateUserById('user-id', { lastSignInAt: Date.now() });
expect(updateUserById).toHaveBeenCalledWith(
'user-id',
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 envSet from '@/env-set';
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 { buildIdGenerator } from '@/utils/id';
import { encryptPassword } from '@/utils/password';
@ -59,9 +59,6 @@ export const findUserByUsernameAndPassword = async (
return user;
};
export const updateLastSignInAt = async (userId: string) =>
updateUserById(userId, { lastSignInAt: Date.now() });
const insertUserQuery = buildInsertInto<CreateUser, User>(Users, {
returning: true,
});

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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