Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

feat(walletkit-ui): add authentication to store #83

Closed
wants to merge 4 commits into from
Closed
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
33 changes: 33 additions & 0 deletions packages/walletkit-ui/src/store/authentication.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { createSlice, PayloadAction } from "@reduxjs/toolkit";

export interface Authentication<T> {
consume: (passphrase: string) => Promise<T>; // also serve as passphrase validation logic
onAuthenticated: (result: T) => Promise<void>;
// OPTIONAL error handler
// (CRITICAL invalid passphare error is auto handled, include wipe wallet if necessary)
onError?: (e: Error) => void;

// messages in passcode UI
// message and loading are mandatory, default message is for signing transaction
title?: string;
message: string;
loading: string;
additionalMessage?: string;
additionalMessageUrl?: string;
successMessage?: string;
}

const initialState: { authentication?: Authentication<any> } = {};

export const authentication = createSlice({
name: "authentication",
initialState,
reducers: {
prompt: (state, action: PayloadAction<Authentication<any>>) => {
state.authentication = action.payload;
},
dismiss: (state) => {
delete state.authentication;
},
},
});
43 changes: 43 additions & 0 deletions packages/walletkit-ui/src/store/authentication.unit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Authentication, authentication } from "./authentication";

describe("authentication reducer", () => {
let initialState: { authentication?: Authentication<any> };

beforeEach(() => {
initialState = {
authentication: {
consume: () => new Promise<void>(() => {}),
onAuthenticated: () => new Promise<void>(() => {}),
message: "Enter passcode to continue",
loading: "Verifying access",
},
};
});

it("should handle initial state", () => {
expect(authentication.reducer(undefined, { type: "unknown" })).toEqual({});
});

it("should handle prompt", () => {
const payload: Authentication<any> = {
consume: () => new Promise<void>(() => {}),
onAuthenticated: () => new Promise(() => {}),
message: "Enter change passcode to continue",
loading: "Verifying access",
};

const actual = authentication.reducer(
initialState,
authentication.actions.prompt(payload)
);
expect(actual.authentication).toMatchObject(payload);
});

it("should handle dismiss state", () => {
const actual = authentication.reducer(
initialState,
authentication.actions.dismiss()
);
expect(actual.authentication).toStrictEqual(undefined);
});
});
1 change: 1 addition & 0 deletions packages/walletkit-ui/src/store/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./auctions";
export * from "./authentication";
export * from "./block";
export * from "./loans";
export * from "./ocean";
Expand Down