Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/lib/auth/getUserFromToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ async function getUserFromToken(ctx: Context) {
const userId: number = ctx.state.user.sub
const user = await em.repo(User).findOneOrFail(
userId,
getResultCacheOptions(`user-from-token-${userId}-${ctx.state.user.iat}`)
getResultCacheOptions(`user-from-token-${userId}-${ctx.state.user.iat}`, 600_000)
)

// populate after so the cache doesn't include from circular structures
if (!user.organisation.games.isInitialized()) {
await em.populate(user, ['organisation.games'])
}

return user
}

Expand Down
2 changes: 1 addition & 1 deletion src/policies/policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default class Policy extends ServicePolicy {

async canAccessGame(gameId: number): Promise<boolean> {
const game = await this.em.repo(Game).findOne(gameId, {
...getResultCacheOptions(`can-access-game-${gameId}`),
...getResultCacheOptions(`can-access-game-${gameId}`, 600_000),
populate: ['organisation.id']
})

Expand Down
11 changes: 9 additions & 2 deletions src/services/public/user-public.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ export default class UserPublicService extends Service {
user.emailConfirmed = process.env.AUTO_CONFIRM_EMAIL === 'true'

if (inviteToken) {
const invite = await em.getRepository(Invite).findOne({ token: inviteToken })
const invite = await em.getRepository(Invite).findOne({
token: inviteToken
}, {
populate: ['organisation.games']
})

if (!invite || invite.email !== email) req.ctx.throw(404, 'Invite not found')

user.organisation = invite.organisation
Expand Down Expand Up @@ -319,7 +324,9 @@ export default class UserPublicService extends Service {
const { code, userId } = req.body
const em: EntityManager = req.ctx.em

const user = await em.getRepository(User).findOneOrFail(userId, { populate: ['recoveryCodes'] })
const user = await em.getRepository(User).findOneOrFail(userId, {
populate: ['recoveryCodes', 'organisation.games']
})

const redis: Redis = req.ctx.redis
const hasSession = (await redis.get(`2fa:${user.id}`)) === 'true'
Expand Down
3 changes: 0 additions & 3 deletions src/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,7 @@ export default class UserService extends Service {
path: '/me'
})
async me(req: Request): Promise<Response> {
const em: EntityManager = req.ctx.em
const user = await getUserFromToken(req.ctx)
// cache doesn't include the full organisation
await em.populate(user, ['organisation.games'])

return {
status: 200,
Expand Down
3 changes: 2 additions & 1 deletion tests/services/_public/user-public/login.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ describe('User public service - login', () => {
expect(res.body.accessToken).toBeTruthy()
expect(res.body.user).toBeTruthy()
expect(res.body.user.organisation).toBeTruthy()
expect(new Date(res.body.user.lastSeenAt).getDay()).toBe(new Date().getDay())
expect(res.body.user.organisation.games).toEqual([])
expect(new Date(res.body.user.lastSeenAt).getDay()).toEqual(new Date().getDay())
})

it('should not let a user login with the wrong password', async () => {
Expand Down
5 changes: 4 additions & 1 deletion tests/services/_public/user-public/refresh.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ describe('User public service - refresh', () => {

expect(res.body.accessToken).toBeTruthy()
expect(res.body.user).toBeTruthy()
expect(new Date(res.body.user.lastSeenAt).getDay()).toBe(new Date().getDay())
expect(res.body.user.organisation).toBeTruthy()
expect(res.body.user.organisation.games).toEqual([])

expect(new Date(res.body.user.lastSeenAt).getDay()).toEqual(new Date().getDay())
})

it('should not let a user refresh their session if they don\'t have one', async () => {
Expand Down
5 changes: 4 additions & 1 deletion tests/services/_public/user-public/register.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import InviteFactory from '../../../fixtures/InviteFactory'
import GameActivity, { GameActivityType } from '../../../../src/entities/game-activity'
import PricingPlanFactory from '../../../fixtures/PricingPlanFactory'
import { randEmail, randUserName } from '@ngneat/falso'
import createOrganisationAndGame from '../../../utils/createOrganisationAndGame'

describe('User public service - register', () => {
beforeAll(async () => {
Expand All @@ -27,6 +28,7 @@ describe('User public service - register', () => {
expect(res.body.user.username).toBe(username)
expect(res.body.user.password).not.toBeDefined()
expect(res.body.user.organisation.name).toBe('Talo')
expect(res.body.user.organisation.games).toEqual([])
})

it('should not let a user register if the email already exists', async () => {
Expand Down Expand Up @@ -60,7 +62,7 @@ describe('User public service - register', () => {
})

it('should let a user register with an invite', async () => {
const organisation = await new OrganisationFactory().one()
const [organisation] = await createOrganisationAndGame()
const invite = await new InviteFactory().construct(organisation).one()
await em.persistAndFlush(invite)

Expand All @@ -77,6 +79,7 @@ describe('User public service - register', () => {
expect(res.body.user.username).toBe(username)
expect(res.body.user.password).not.toBeDefined()
expect(res.body.user.organisation.id).toBe(organisation.id)
expect(res.body.user.organisation.games).toHaveLength(1)

const activity = await em.getRepository(GameActivity).findOne({
type: GameActivityType.INVITE_ACCEPTED
Expand Down
3 changes: 3 additions & 0 deletions tests/services/_public/user-public/use-recovery-code.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ describe('User public service - use recovery code', () => {
.expect(200)

expect(res.body.user).toBeTruthy()
expect(res.body.user.organisation).toBeTruthy()
expect(res.body.user.organisation.games).toEqual([])

expect(res.body.accessToken).toBeTruthy()
expect(res.body.newRecoveryCodes).toBeUndefined()

Expand Down
3 changes: 3 additions & 0 deletions tests/services/_public/user-public/verify-2fa.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ describe('User public service - verify 2fa', () => {
.expect(200)

expect(res.body.user).toBeTruthy()
expect(res.body.user.organisation).toBeTruthy()
expect(res.body.user.organisation.games).toEqual([])

expect(res.body.accessToken).toBeTruthy()

const hasSession = await redis.get(`2fa:${user.id}`)
Expand Down
3 changes: 3 additions & 0 deletions tests/services/user/confirm-2fa.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ describe('User service - confirm 2fa', () => {
.expect(200)

expect(res.body.user).toBeTruthy()
expect(res.body.user.organisation).toBeTruthy()
expect(res.body.user.organisation.games).toEqual([])

expect(res.body.recoveryCodes).toHaveLength(8)

await wrap(user.twoFactorAuth!).init()
Expand Down
2 changes: 2 additions & 0 deletions tests/services/user/confirm-email.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ describe('User service - confirm email', () => {
.expect(200)

expect(res.body.user.emailConfirmed).toBe(true)
expect(res.body.user.organisation).toBeTruthy()
expect(res.body.user.organisation.games).toEqual([])

const updatedAccessCode = await em.getRepository(UserAccessCode).findOne({ code: accessCode.code })
expect(updatedAccessCode).toBeNull()
Expand Down
2 changes: 2 additions & 0 deletions tests/services/user/disable-2fa.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ describe('User service - disable 2fa', () => {
.expect(200)

expect(res.body.user.has2fa).toBe(false)
expect(res.body.user.organisation).toBeTruthy()
expect(res.body.user.organisation.games).toEqual([])

const recoveryCodes = await em.getRepository(UserRecoveryCode).find({ user })
expect(recoveryCodes).toHaveLength(0)
Expand Down