feat : add redux
This commit is contained in:
26
src/redux/slices/token.ts
Normal file
26
src/redux/slices/token.ts
Normal 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
61
src/redux/store.ts
Normal 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
12
src/types/props.d.ts
vendored
@@ -57,6 +57,7 @@ import React, {
|
|||||||
ReactNode,
|
ReactNode,
|
||||||
SetStateAction,
|
SetStateAction,
|
||||||
} from "react";
|
} from "react";
|
||||||
|
import { FormikProps } from "formik";
|
||||||
|
|
||||||
export type TutorialList = { id: number; name: string }[];
|
export type TutorialList = { id: number; name: string }[];
|
||||||
|
|
||||||
@@ -135,3 +136,14 @@ export interface ProgressSpaceProps {
|
|||||||
startSpace: number;
|
startSpace: number;
|
||||||
endSpace: number | "ultimate";
|
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
0
src/types/type.d.ts
vendored
Normal file
@@ -1,3 +1,18 @@
|
|||||||
export const isAuthenticated = () => {
|
import { useSelector } from "react-redux";
|
||||||
return false;
|
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);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user