From d2846afd059d9ee85fccf9309ca3b77575fa25d3 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Sun, 11 Dec 2022 21:13:57 +0100 Subject: [PATCH] current stand --- src/AuthContext/AuthContextProvider.tsx | 46 ++++++++++++++++++++----- src/AuthContext/types.ts | 23 +++++++++++++ 2 files changed, 60 insertions(+), 9 deletions(-) create mode 100644 src/AuthContext/types.ts diff --git a/src/AuthContext/AuthContextProvider.tsx b/src/AuthContext/AuthContextProvider.tsx index 890a5e7..00b87e9 100644 --- a/src/AuthContext/AuthContextProvider.tsx +++ b/src/AuthContext/AuthContextProvider.tsx @@ -1,5 +1,5 @@ import {ReactElement, ReactNode, useCallback, useEffect, useMemo, useRef, useState} from "react" -import {useEvent, useLocalStorage} from "react-use" +import {createReducerContext, useEvent, useLocalStorage} from "react-use" import {AxiosError} from "axios" import {decrypt, readMessage, readPrivateKey} from "openpgp" import {useNavigate} from "react-router-dom" @@ -7,30 +7,58 @@ import {useNavigate} from "react-router-dom" import {useMutation, useQuery} from "@tanstack/react-query" import {AuthenticationDetails, ServerUser, User} from "~/server-types" -import { - REFRESH_TOKEN_URL, - RefreshTokenResult, - getMe, - logout as logoutUser, - refreshToken, -} from "~/apis" +import {getMe, logout as logoutUser, REFRESH_TOKEN_URL, refreshToken, RefreshTokenResult} from "~/apis" import {client} from "~/constants/axios-client" import {decryptString, encryptString} from "~/utils" import {ExtensionKleckEvent} from "~/extension-types" import PasswordShareConfirmationDialog from "~/AuthContext/PasswordShareConfirmationDialog" +import {Action, State} from "./types" import AuthContext, {AuthContextType, EncryptionStatus} from "./AuthContext" export interface AuthContextProviderProps { children: ReactNode } +const INITIAL_STATE: State = { + user: null, + masterPassword: null, +} + +const reducer = (state: State, action: Action): State => { + switch (action.type) { + case "SET_USER": { + return { + ...state, + user: action.payload, + } + } + case "SET_PASSWORD": { + const masterPassword = decryptString(state.user!.encryptedPassword, action.payload) + + return { + ...state, + masterPassword, + } + } + case "LOGOUT": { + return INITIAL_STATE + } + } +} + +const [useAuth, AuthContext] = createReducerContext(reducer, INITIAL_STATE) + export default function AuthContextProvider({children}: AuthContextProviderProps): ReactElement { const navigate = useNavigate() + + const [auth, dispatch] = useAuth(); + const {mutateAsync: refresh} = useMutation(refreshToken, { - onError: () => logout(false), + onError: () => dispatch({type: "LOGOUT"}), }) + // Required for extension const $enterPasswordAmount = useRef(0) const [askForPassword, setAskForPassword] = useState(false) const [doNotAskForPassword, setDoNotAskForPassword] = useState(false) diff --git a/src/AuthContext/types.ts b/src/AuthContext/types.ts new file mode 100644 index 0000000..8e31373 --- /dev/null +++ b/src/AuthContext/types.ts @@ -0,0 +1,23 @@ +import {ServerUser, User} from "~/server-types" + +export interface ActionSetUser { + type: "SET_USER" + payload: User | ServerUser +} + +export interface ActionSetPassword { + type: "SET_PASSWORD" + payload: string +} + +export interface ActionLogout { + type: "LOGOUT" + payload?: null +} + +export type Action = ActionSetUser | ActionSetPassword | ActionLogout + +export interface State { + user: User | ServerUser | null + masterPassword: string | null +}