mirror of
https://github.com/Steffo99/festa.git
synced 2024-12-22 22:54:22 +00:00
Properly save auth state
This commit is contained in:
parent
45c3e2071f
commit
5d633f51bd
3 changed files with 8 additions and 16 deletions
|
@ -1,21 +1,13 @@
|
||||||
import { useCallback, useState } from "react";
|
import React, { useCallback, useState } from "react";
|
||||||
import { localStorageSaveJSON, useLocalStorageJSONLoad, useLocalStorageJSONState } from "../generic/storage/json";
|
import { localStorageSaveJSON, useLocalStorageJSONLoad, useLocalStorageJSONState } from "../generic/storage/json";
|
||||||
import { AuthContextContents } from "./base";
|
import { AuthContextContents } from "./base";
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Hook holding as state the {@link AuthContextContents}.
|
|
||||||
*/
|
|
||||||
export function useStateAuth() {
|
|
||||||
return useState<AuthContextContents>(null)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hook which combines {@link useState}, {@link useLocalStorageJSONLoad}, and {@link localStorageSaveJSON}.
|
* Hook which combines {@link useState}, {@link useLocalStorageJSONLoad}, and {@link localStorageSaveJSON}.
|
||||||
*/
|
*/
|
||||||
export function useLocalStorageAuthState(key: string) {
|
export function useLocalStorageAuthState(key: string): [AuthContextContents | null, React.Dispatch<AuthContextContents | null>] {
|
||||||
const [state, setStateInner] = useState<AuthContextContents | undefined>(undefined);
|
const [state, setStateInner] = useState<AuthContextContents | null>(null);
|
||||||
|
|
||||||
const validateAndSetState = useCallback(
|
const validateAndSetState = useCallback(
|
||||||
(data: any) => {
|
(data: any) => {
|
||||||
|
@ -36,8 +28,8 @@ export function useLocalStorageAuthState(key: string) {
|
||||||
|
|
||||||
const setState = useCallback(
|
const setState = useCallback(
|
||||||
(value: AuthContextContents) => {
|
(value: AuthContextContents) => {
|
||||||
validateAndSetState(value);
|
|
||||||
localStorageSaveJSON(key, value);
|
localStorageSaveJSON(key, value);
|
||||||
|
validateAndSetState(value);
|
||||||
},
|
},
|
||||||
[key, validateAndSetState]
|
[key, validateAndSetState]
|
||||||
);
|
);
|
||||||
|
|
|
@ -4,13 +4,13 @@ import { AxiosSWRFetcherProvider } from '../components/auth/requests'
|
||||||
import { useStatePostcard } from '../components/postcard/storage'
|
import { useStatePostcard } from '../components/postcard/storage'
|
||||||
import { PageErrorBoundary } from '../components/generic/errors/boundaries'
|
import { PageErrorBoundary } from '../components/generic/errors/boundaries'
|
||||||
import { PostcardContext } from '../components/postcard/base'
|
import { PostcardContext } from '../components/postcard/base'
|
||||||
import { useStateAuth } from '../components/auth/storage'
|
|
||||||
import { AuthContext } from '../components/auth/base'
|
import { AuthContext } from '../components/auth/base'
|
||||||
import { PostcardRenderer } from '../components/postcard/renderer'
|
import { PostcardRenderer } from '../components/postcard/renderer'
|
||||||
import '../styles/globals.css'
|
import '../styles/globals.css'
|
||||||
import defaultPostcard from "../public/postcards/adi-goldstein-Hli3R6LKibo-unsplash.jpg"
|
import defaultPostcard from "../public/postcards/adi-goldstein-Hli3R6LKibo-unsplash.jpg"
|
||||||
import { config as fontAwesomeConfig } from '@fortawesome/fontawesome-svg-core'
|
import { config as fontAwesomeConfig } from '@fortawesome/fontawesome-svg-core'
|
||||||
import '@fortawesome/fontawesome-svg-core/styles.css'
|
import '@fortawesome/fontawesome-svg-core/styles.css'
|
||||||
|
import { useLocalStorageAuthState } from '../components/auth/storage'
|
||||||
|
|
||||||
|
|
||||||
fontAwesomeConfig.autoAddCss = false
|
fontAwesomeConfig.autoAddCss = false
|
||||||
|
@ -19,7 +19,7 @@ fontAwesomeConfig.autoAddCss = false
|
||||||
const App = ({ Component, pageProps }: AppProps): JSX.Element => {
|
const App = ({ Component, pageProps }: AppProps): JSX.Element => {
|
||||||
const { t } = useTranslation()
|
const { t } = useTranslation()
|
||||||
const postcardState = useStatePostcard(defaultPostcard)
|
const postcardState = useStatePostcard(defaultPostcard)
|
||||||
const authState = useStateAuth()
|
const authState = useLocalStorageAuthState("auth")
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<PageErrorBoundary text={t("genericError")}>
|
<PageErrorBoundary text={t("genericError")}>
|
||||||
|
|
|
@ -6,6 +6,6 @@ import { createDefinedContext } from "./definedContext"
|
||||||
*
|
*
|
||||||
* @returns The created context.
|
* @returns The created context.
|
||||||
*/
|
*/
|
||||||
export function createStateContext<T>(): Context<[T, Dispatch<SetStateAction<T>>] | undefined> {
|
export function createStateContext<T>(): Context<[T, Dispatch<T>] | undefined> {
|
||||||
return createDefinedContext<[T, React.Dispatch<React.SetStateAction<T>>]>()
|
return createDefinedContext<[T, React.Dispatch<T>]>()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue