feat : add redux

This commit is contained in:
alij
2024-06-16 01:24:40 +04:30
parent 2a8a2b6c71
commit db5840e0b7
5 changed files with 116 additions and 2 deletions

26
src/redux/slices/token.ts Normal file
View File

@@ -0,0 +1,26 @@
// src/slices/token.ts
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
// Initial type for the token state
interface TokenState {
token: string;
}
// Initial value for the token state
const initialState: TokenState = {
token: "",
};
const tokenSlice = createSlice({
name: "token",
initialState,
reducers: {
setToken: (state, action: PayloadAction<string>) => {
state.token = action.payload;
},
},
});
export const { setToken } = tokenSlice.actions;
export default tokenSlice.reducer;

61
src/redux/store.ts Normal file
View File

@@ -0,0 +1,61 @@
// src/store.ts
import { configureStore } from "@reduxjs/toolkit";
import { encryptTransform } from "redux-persist-transform-encrypt";
import storage from "redux-persist/lib/storage";
import {
persistStore,
persistReducer,
PersistConfig,
FLUSH,
REHYDRATE,
PAUSE,
PERSIST,
PURGE,
REGISTER,
} from "redux-persist";
import tokenReducer from "./slices/token";
// Encryption settings
const secretKey = "token-encrypt-api";
const encryptor = encryptTransform({
secretKey,
onError: (error) => {
console.log(error);
},
});
// Type definition for the token state
interface TokenState {
token: string;
}
// Persist configuration for the token
const persistConfigToken: PersistConfig<TokenState> = {
key: "token",
storage,
transforms: [encryptor], // Correct the typo (transform -> transforms)
};
// Use persistReducer with the configuration and token reducer
const persistedTokenReducer = persistReducer(persistConfigToken, tokenReducer);
// Store configuration
const store = configureStore({
reducer: {
token: persistedTokenReducer,
},
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware({
serializableCheck: {
ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER],
},
}),
});
// Types for AppDispatch and RootState
export type AppDispatch = typeof store.dispatch;
export type RootState = ReturnType<typeof store.getState>;
export const persistor = persistStore(store);
export default store;

12
src/types/props.d.ts vendored
View File

@@ -57,6 +57,7 @@ import React, {
ReactNode,
SetStateAction,
} from "react";
import { FormikProps } from "formik";
export type TutorialList = { id: number; name: string }[];
@@ -135,3 +136,14 @@ export interface ProgressSpaceProps {
startSpace: number;
endSpace: number | "ultimate";
}
export interface LoginFormValues {
mobile: string;
email: string;
mobileCode: string;
emailPassword: string;
}
export interface LoginFromikProps {
formik: FormikProps<LoginFormValues>;
}

0
src/types/type.d.ts vendored Normal file
View File

View File

@@ -1,3 +1,18 @@
export const isAuthenticated = () => {
return false;
import { useSelector } from "react-redux";
import { RootState } from "../redux/store";
const isTokenValid = (token: string): boolean => {
try {
if (!token) return false;
return !!token;
} catch (error) {
console.error("Token validation error:", error);
return false;
}
};
export const isAuthenticated = () : boolean => {
const {token} = useSelector((state : RootState) => state.token)
return isTokenValid(token);
};