From eacb1623ca0483a9090a02b9b64d7d37431eeebd Mon Sep 17 00:00:00 2001 From: andres Date: Fri, 26 May 2023 21:32:42 +0200 Subject: [PATCH] part 3 extra reducers --- .env.development | 1 + .env.example | 1 + .env.production | 1 + .idea/git_toolbox_prj.xml | 15 ++++++++++ src/App.tsx | 16 +++++++++-- src/app/store.ts | 2 ++ src/features/auth/auth.api.ts | 39 ++++++++++++++++++++++--- src/features/auth/auth.instance.ts | 2 +- src/features/auth/auth.slice.ts | 46 ++++++++++++++++++++++++++++++ 9 files changed, 116 insertions(+), 7 deletions(-) create mode 100644 .env.development create mode 100644 .env.example create mode 100644 .env.production create mode 100644 .idea/git_toolbox_prj.xml create mode 100644 src/features/auth/auth.slice.ts diff --git a/.env.development b/.env.development new file mode 100644 index 0000000..7f9e2b4 --- /dev/null +++ b/.env.development @@ -0,0 +1 @@ +VITE_BASE_API_URL="http://localhost:7542/2.0/" \ No newline at end of file diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..7f9e2b4 --- /dev/null +++ b/.env.example @@ -0,0 +1 @@ +VITE_BASE_API_URL="http://localhost:7542/2.0/" \ No newline at end of file diff --git a/.env.production b/.env.production new file mode 100644 index 0000000..18b100e --- /dev/null +++ b/.env.production @@ -0,0 +1 @@ +VITE_BASE_API_URL="https://neko-back.herokuapp.com/2.0/" \ No newline at end of file diff --git a/.idea/git_toolbox_prj.xml b/.idea/git_toolbox_prj.xml new file mode 100644 index 0000000..02b915b --- /dev/null +++ b/.idea/git_toolbox_prj.xml @@ -0,0 +1,15 @@ + + + + + + + \ No newline at end of file diff --git a/src/App.tsx b/src/App.tsx index 3ff9b11..4ecef4d 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -7,7 +7,7 @@ import { createTheme, ThemeProvider } from "@mui/material" import { useAppDispatch, useAppSelector } from "@/app/hooks" import { useEffect } from "react" import { appActions } from "@/features/app/app.slice" -import { instance } from "@/app/instance" +import { authThunks } from "@/features/auth/auth.slice" export const Test = () => { const isLoading = useAppSelector((state) => state.app.isLoading) @@ -22,12 +22,24 @@ export const Test = () => { setTimeout(() => { dispatch(appActions.setIsLoading({ isLoading: false })) }, 3000) - instance.get("/ping") }, [dispatch]) if (isLoading) return
loading...
return (
+ {!!error &&

{error}

} diff --git a/src/app/store.ts b/src/app/store.ts index 8911542..e3c6994 100644 --- a/src/app/store.ts +++ b/src/app/store.ts @@ -1,11 +1,13 @@ import { configureStore, ThunkAction, Action } from "@reduxjs/toolkit" import counterReducer from "../features/counter/counterSlice" import { appReducer } from "@/features/app/app.slice" +import { authReducer } from "@/features/auth/auth.slice" export const store = configureStore({ reducer: { counter: counterReducer, app: appReducer, + auth: authReducer, }, }) diff --git a/src/features/auth/auth.api.ts b/src/features/auth/auth.api.ts index 9f10798..65ab855 100644 --- a/src/features/auth/auth.api.ts +++ b/src/features/auth/auth.api.ts @@ -1,7 +1,38 @@ import { AuthInstance } from "@/features/auth/auth.instance" -export const AuthApi = () => ({ - register: (params: any) => { - return AuthInstance.post(params) +export const AuthApi = { + register: (params: RegisterArgs) => { + return AuthInstance.post("register", params) }, -}) + login: (params: LoginArgs) => { + return AuthInstance.post("login", params) + }, +} + +export type RegisterResponse = { + addedUser: AddedUser +} + +type PasswordToPick = { + password: string +} + +export type AddedUser = Omit +export type RegisterArgs = Pick & PasswordToPick +export type LoginArgs = Pick & PasswordToPick +export type PartialUser = Partial + +export type User = { + _id: string + email: string + rememberMe: boolean + isAdmin: boolean + name: string + verified: boolean + publicCardPacksCount: number + created: string + updated: string + token: string + tokenDeathTime: number + __v: number +} diff --git a/src/features/auth/auth.instance.ts b/src/features/auth/auth.instance.ts index 4b60f91..bae19b9 100644 --- a/src/features/auth/auth.instance.ts +++ b/src/features/auth/auth.instance.ts @@ -1,6 +1,6 @@ import axios from "axios" export const AuthInstance = axios.create({ - baseURL: import.meta.env.BASE_URL + "auth/", + baseURL: import.meta.env.VITE_BASE_API_URL + "auth/", withCredentials: true, }) diff --git a/src/features/auth/auth.slice.ts b/src/features/auth/auth.slice.ts new file mode 100644 index 0000000..18e52a0 --- /dev/null +++ b/src/features/auth/auth.slice.ts @@ -0,0 +1,46 @@ +import { createAsyncThunk, createSlice, PayloadAction } from "@reduxjs/toolkit" +import { + AuthApi, + LoginArgs, + RegisterArgs, + User, +} from "@/features/auth/auth.api" + +const register = createAsyncThunk("auth/register", (arg: RegisterArgs) => { + AuthApi.register(arg) + .then((res) => { + console.log(res) + }) + .catch((res) => { + console.error(res) + }) +}) + +const login = createAsyncThunk("auth/login", async (arg: LoginArgs) => { + try { + const res = await AuthApi.login(arg) + return { user: res.data } + } catch (e) { + console.error(e) + } +}) + +const slice = createSlice({ + name: "auth", + initialState: { user: null as User | null }, + reducers: { + setUser: (state, action: PayloadAction<{ user: User }>) => { + state.user = action.payload.user + }, + }, + extraReducers: (builder) => { + builder.addCase(login.fulfilled, (state, action) => { + if (action.payload?.user) { + state.user = action.payload.user + } + }) + }, +}) + +export const authReducer = slice.reducer +export const authThunks = { register, login }