Skip to content

Commit

Permalink
fix: remove built-in $authentication store (#25)
Browse files Browse the repository at this point in the history
This feature was intended to be an easier API for a built-in
hybrid message[^1], but turned out to be problematic. By design
the store will emit its value on the message bus if the message
bus has no previous message for the channel and topic.

For authentication in a hybrid webview there is no client-side
initial value (it's an Authorization HTTP request header). Unless
a client-side script primes the message bus with a fake message
on "system/authentication", this store ends up sending a signal
to the native side that the user has logged out (authentication
token is null).

Since the method to detect login state and prime the message
bus will be different from case to case, this built-in store
is removed.

BREAKING CHANGE: $authentication is removed

[1]: https://podium-lib.io/docs/guides/hybrid#message-contracts
  • Loading branch information
wkillerud authored Oct 25, 2024
1 parent 80a1847 commit d726b24
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 43 deletions.
31 changes: 0 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,6 @@ To install:
npm install @podium/store
```

Use an [included store](#included-stores) in your client-side application:

```js
// store/user.js
import { $authentication } from '@podium/store';
import { computed } from 'nanostores';

// You can optionally make a computed value based on the included store
export const $loggedIn = computed($authentication, (authentication) =>
Boolean(authentication.token),
);

// Colocating actions with the store makes it easier to
// see what can trigger updates.
export function logIn(token) {
$authentication.set({ token });
}

export function logOut() {
$authentication.set({ token: null });
}
```

Use the reactive store to do minimal updates of your UI when state changes. Nanostores supports multiple view frameworks:

- [React and Preact](https://github.com/nanostores/nanostores?tab=readme-ov-file#react--preact)
Expand Down Expand Up @@ -83,14 +60,6 @@ By using the [included helper](#mapchannel-topic-initialvalue) you can make your

## API

### `$authentication`

Type: [`map`](https://github.com/nanostores/nanostores?tab=readme-ov-file#maps)

```js
import { $authentication } from '@podium/store';
```

### `atom(channel, topic, initialValue)`

Create your own [`atom`](https://github.com/nanostores/nanostores?tab=readme-ov-file#atoms) that syncs between parts of a Podium application using the [MessageBus](https://github.com/podium-lib/browser).
Expand Down
7 changes: 0 additions & 7 deletions src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,3 @@ export function deepMap(channel, topic, initialValue) {

return $store;
}

/**
* @typedef {object} Authentication
* @property {string | null} token The authentication token. `null` == not logged in.
*/

export const $authentication = map('system', 'authentication', { token: null });
6 changes: 1 addition & 5 deletions tests/store.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ const dom = new JSDOM(html);
globalThis.window = dom.window;

const { MessageBus } = await import('@podium/browser');
const { $authentication, atom, map, deepMap } = await import('../src/store.js');

test('$authentication initial state', () => {
assert.deepStrictEqual($authentication.value, { token: null });
});
const { atom, map, deepMap } = await import('../src/store.js');

test('atom returns a value connected to the Podium MessageBus', () => {
const $reminders = atom('reminders', 'list', []);
Expand Down

0 comments on commit d726b24

Please sign in to comment.