mirror of
https://github.com/Myzel394/kleckrelay-website.git
synced 2025-06-18 23:45:26 +02:00
current stand
This commit is contained in:
parent
c2f6f434c7
commit
d2846afd05
@ -1,5 +1,5 @@
|
|||||||
import {ReactElement, ReactNode, useCallback, useEffect, useMemo, useRef, useState} from "react"
|
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 {AxiosError} from "axios"
|
||||||
import {decrypt, readMessage, readPrivateKey} from "openpgp"
|
import {decrypt, readMessage, readPrivateKey} from "openpgp"
|
||||||
import {useNavigate} from "react-router-dom"
|
import {useNavigate} from "react-router-dom"
|
||||||
@ -7,30 +7,58 @@ import {useNavigate} from "react-router-dom"
|
|||||||
import {useMutation, useQuery} from "@tanstack/react-query"
|
import {useMutation, useQuery} from "@tanstack/react-query"
|
||||||
|
|
||||||
import {AuthenticationDetails, ServerUser, User} from "~/server-types"
|
import {AuthenticationDetails, ServerUser, User} from "~/server-types"
|
||||||
import {
|
import {getMe, logout as logoutUser, REFRESH_TOKEN_URL, refreshToken, RefreshTokenResult} from "~/apis"
|
||||||
REFRESH_TOKEN_URL,
|
|
||||||
RefreshTokenResult,
|
|
||||||
getMe,
|
|
||||||
logout as logoutUser,
|
|
||||||
refreshToken,
|
|
||||||
} from "~/apis"
|
|
||||||
import {client} from "~/constants/axios-client"
|
import {client} from "~/constants/axios-client"
|
||||||
import {decryptString, encryptString} from "~/utils"
|
import {decryptString, encryptString} from "~/utils"
|
||||||
import {ExtensionKleckEvent} from "~/extension-types"
|
import {ExtensionKleckEvent} from "~/extension-types"
|
||||||
import PasswordShareConfirmationDialog from "~/AuthContext/PasswordShareConfirmationDialog"
|
import PasswordShareConfirmationDialog from "~/AuthContext/PasswordShareConfirmationDialog"
|
||||||
|
|
||||||
|
import {Action, State} from "./types"
|
||||||
import AuthContext, {AuthContextType, EncryptionStatus} from "./AuthContext"
|
import AuthContext, {AuthContextType, EncryptionStatus} from "./AuthContext"
|
||||||
|
|
||||||
export interface AuthContextProviderProps {
|
export interface AuthContextProviderProps {
|
||||||
children: ReactNode
|
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 {
|
export default function AuthContextProvider({children}: AuthContextProviderProps): ReactElement {
|
||||||
const navigate = useNavigate()
|
const navigate = useNavigate()
|
||||||
|
|
||||||
|
const [auth, dispatch] = useAuth();
|
||||||
|
|
||||||
const {mutateAsync: refresh} = useMutation<RefreshTokenResult, AxiosError, void>(refreshToken, {
|
const {mutateAsync: refresh} = useMutation<RefreshTokenResult, AxiosError, void>(refreshToken, {
|
||||||
onError: () => logout(false),
|
onError: () => dispatch({type: "LOGOUT"}),
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Required for extension
|
||||||
const $enterPasswordAmount = useRef<number>(0)
|
const $enterPasswordAmount = useRef<number>(0)
|
||||||
const [askForPassword, setAskForPassword] = useState<boolean>(false)
|
const [askForPassword, setAskForPassword] = useState<boolean>(false)
|
||||||
const [doNotAskForPassword, setDoNotAskForPassword] = useState<boolean>(false)
|
const [doNotAskForPassword, setDoNotAskForPassword] = useState<boolean>(false)
|
||||||
|
23
src/AuthContext/types.ts
Normal file
23
src/AuthContext/types.ts
Normal file
@ -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
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user