Skip to content

Commit 608cb85

Browse files
committed
Adds correct checks after types has been fixed
1 parent de089a5 commit 608cb85

File tree

12 files changed

+44
-23
lines changed

12 files changed

+44
-23
lines changed

demos/taco-chat/src/hooks/useTeam.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { type AlertInfo, type PeerState } from '../types.js'
55
import { assert } from '@localfirst/shared'
66
import { ConnectionManager } from 'ConnectionManager.js'
77
import { teamContext } from 'components/TeamProvider.js'
8-
import { randomTeamName } from 'util/randomTeamName.js'
8+
import { randomTeamName } from '../util/randomTeamName.js'
99

1010
// TODO: make this an environment var
1111
const relayUrls = ['ws://localhost:8080']

packages/auth-provider-automerge-repo/src/AuthProvider.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type {
88
import { EventEmitter } from '@herbcaudill/eventemitter42'
99
import * as Auth from '@localfirst/auth'
1010
import { hash } from '@localfirst/crypto'
11-
import { debug, memoize, pause } from '@localfirst/shared'
11+
import { assert, debug, memoize, pause } from '@localfirst/shared'
1212
import { type AbstractConnection } from './AbstractConnection.js'
1313
import { AnonymousConnection } from './AnonymousConnection.js'
1414
import { buildServerUrl } from './buildServerUrl.js'
@@ -211,7 +211,11 @@ export class AuthProvider extends EventEmitter<AuthProviderEvents> {
211211
* Creates a team and registers it with all of our sync servers.
212212
*/
213213
public async createTeam(teamName: string) {
214-
const team = await Auth.createTeam(teamName, {
214+
if (!this.#user) {
215+
throw new Error('Cannot create team as user is missing on AuthProvider')
216+
}
217+
218+
const team = Auth.createTeam(teamName, {
215219
device: this.#device,
216220
user: this.#user,
217221
})
@@ -661,6 +665,10 @@ export class AuthProvider extends EventEmitter<AuthProviderEvents> {
661665

662666
const savedShares = unpack(serializedState) as SerializedState
663667

668+
if (!this.#user) {
669+
throw new Error('Cannot load state as user is missing on AuthProvider')
670+
}
671+
664672
await Promise.all(
665673
Object.values(savedShares).map(async share => {
666674
if ('encryptedTeam' in share) {
@@ -672,9 +680,12 @@ export class AuthProvider extends EventEmitter<AuthProviderEvents> {
672680
this.#device.keys.secretKey
673681
) as Auth.KeysetWithSecrets
674682

675-
const context = { device: this.#device, user: this.#user }
683+
// By this point it is defined
684+
assert(this.#user)
685+
686+
const context: Auth.LocalContext = { device: this.#device, user: this.#user }
676687

677-
const team = await Auth.loadTeam(encryptedTeam, context, teamKeys)
688+
const team = Auth.loadTeam(encryptedTeam, context, teamKeys)
678689
return this.addTeam(team)
679690
} else {
680691
return this.joinPublicShare(share.shareId)

packages/auth-provider-automerge-repo/src/types.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,12 @@ export type AuthProviderEvents = {
134134
* this is how we get the user's keys.) This event gives the application a chance to persist the
135135
* team graph and the user's info.
136136
*/
137-
joined: (payload: { shareId: ShareId; peerId: PeerId; team: Auth.Team; user: Auth.User }) => void
137+
joined: (payload: {
138+
shareId: ShareId
139+
peerId: PeerId
140+
team: Auth.Team
141+
user: Auth.UserWithSecrets
142+
}) => void
138143

139144
/**
140145
* We're connected to a peer and have been mutually authenticated.

packages/auth/src/connection/getDeviceUserFromGraph.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ export const getDeviceUserFromGraph = ({
4242

4343
const userKeyring = select.keyring(state, { type: USER, name: userId }, starterKeys)
4444
const keys = getLatestGeneration(userKeyring)
45-
const user = { userName, userId, keys }
45+
46+
if (!keys) {
47+
throw new Error('Failed to get device user from graph as there are no keys for user keyring')
48+
}
49+
50+
const user: UserWithSecrets = { userName, userId, keys }
4651

4752
return { user, userKeyring }
4853
}

packages/auth/src/connection/test/identity.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { challenge, prove, verify } from '../identity.js'
22
import { ADMIN_SCOPE, TEAM_SCOPE } from '../../team/index.js'
33
import { setup } from '../../util/testing/index.js'
4-
import 'util/testing/expect/toBeValid.js'
4+
import '../../util/testing/expect/toBeValid.js'
55
import { type KeyScope, KeyType, createKeyset, redactKeys } from '@localfirst/crdx'
66
import { describe, expect, it } from 'vitest'
77

packages/auth/src/team/test/members.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ADMIN } from '../../role/index.js'
22
import { setup } from '../../util/testing/index.js'
3-
import 'util/testing/expect/toLookLikeKeyset.js'
3+
import '../../util/testing/expect/toLookLikeKeyset.js'
44
import { describe, expect, it } from 'vitest'
55

66
describe('Team', () => {

packages/crdx/src/graph/test/append.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { TEST_GRAPH_KEYS as keys, setup } from '../../util/testing/setup.js'
22
import { describe, expect, test } from 'vitest'
33
import { append, createGraph, getHead, getRoot } from '../index.js'
44
import { validate } from '../../validator/index.js'
5-
import 'util/testing/expect/toBeValid'
5+
import '../../util/testing/expect/toBeValid'
66

77
const { alice } = setup('alice')
88
const defaultUser = alice

packages/crdx/src/graph/test/create.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { TEST_GRAPH_KEYS as keys, setup } from '../../util/testing/setup.js'
22
import { describe, expect, test } from 'vitest'
33
import { createGraph, deserialize, getHead, getRoot, serialize } from '../index.js'
44
import { validate } from '../../validator/index.js'
5-
import 'util/testing/expect/toBeValid.js'
5+
import '../../util/testing/expect/toBeValid.js'
66

77
const { alice } = setup('alice')
88
const defaultUser = alice

packages/crdx/src/graph/test/merge.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { TEST_GRAPH_KEYS as keys, setup } from '../../util/testing/setup.js'
22
import { clone } from 'lodash-es'
3-
import 'util/testing/expect/toBeValid'
3+
import '../../util/testing/expect/toBeValid'
44
import { describe, expect, test } from 'vitest'
55
import { append, createGraph, merge } from '../index.js'
66

packages/crdx/src/store/test/createStore.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { asymmetric } from '@localfirst/crypto'
22
import { createGraph, getRoot, serialize } from '../../graph/index.js'
33
import { createStore } from '../index.js'
44
import { createUser } from '../../user/index.js'
5-
import 'util/testing/expect/toBeValid'
5+
import '../../util/testing/expect/toBeValid'
66
import { TEST_GRAPH_KEYS as keys } from '../../util/testing/setup.js'
77
import { describe, expect, test } from 'vitest'
88
import {

0 commit comments

Comments
 (0)