Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: auth-redux #196

Merged
merged 1 commit into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ app.get("/search", async (req, res) => {

app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
});
102 changes: 99 additions & 3 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
"preview": "vite preview"
},
"dependencies": {
"@reduxjs/toolkit": "^2.2.5",
"axios": "^1.6.8",
"he": "^1.2.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-h5-audio-player": "^3.9.1",
"react-icons": "^5.2.1",
"react-redux": "^9.1.2",
"redux-persist": "^6.0.0",
"swiper": "^11.1.3"
},
"devDependencies": {
Expand Down
28 changes: 19 additions & 9 deletions frontend/src/main.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App.jsx'
import './index.css'
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App.jsx";
import "./index.css";
import { Provider } from "react-redux";
import { store } from "./redux/store.js";
import { PersistGate } from "redux-persist/integration/react";
import { persistStore } from "redux-persist";

ReactDOM.createRoot(document.getElementById('root')).render(
<React.StrictMode>
<App />
</React.StrictMode>,
)
const persistor = persistStore(store);

ReactDOM.createRoot(document.getElementById("root")).render(
<PersistGate persistor={persistor}>
<Provider store={store}>
<React.StrictMode>
<App />
</React.StrictMode>
</Provider>
</PersistGate>
);
38 changes: 38 additions & 0 deletions frontend/src/redux/auth-slice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { createSlice } from "@reduxjs/toolkit";

const initialState = {
value:{
username:"",
isAuth:false,
email:"",
}
}

export const auth = createSlice({
name:"auth",
initialState,
reducers:{
logOut:()=>{
return initialState
},
logIn:(state,action)=>{
return {
value : {
isAuth:true,
email:action.payload.email
}
}
},
setUsername:(state,action) => {
return {
value : {
isAuth:true,
username:action.payload.username
}
}
}
}
})

export const {logIn,logOut,setUsername} = auth.actions;
export default auth.reducer;
20 changes: 20 additions & 0 deletions frontend/src/redux/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { configureStore } from "@reduxjs/toolkit";
import authReducer from "./auth-slice";
import storage from 'redux-persist/lib/storage';
import {persistReducer} from "redux-persist"
import { combineReducers } from "@reduxjs/toolkit";

const persistConfig = {
key: 'root',
storage
}

const reducer = combineReducers({
auth:authReducer,
})

const persistedReducer = persistReducer(persistConfig,reducer)

export const store=configureStore({
reducer: persistedReducer,
})
Loading