-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from Fingertips18/development
Refactor auth store to selectively persist properties and manage global loading state to actions
- Loading branch information
Showing
19 changed files
with
172 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"name": "client", | ||
"name": "go-react-auth-client", | ||
"private": true, | ||
"version": "1.0.0", | ||
"type": "module", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,34 @@ | ||
import { createJSONStorage, persist } from "zustand/middleware"; | ||
import { createJSONStorage, devtools, persist } from "zustand/middleware"; | ||
import { create } from "zustand"; | ||
|
||
interface AuthStoreState { | ||
email: string; | ||
setEmail: (email: string) => void; | ||
authorized: boolean; | ||
setAuthorized: (authorized: boolean) => void; | ||
loading: boolean; | ||
setLoading: (loading: boolean) => void; | ||
} | ||
|
||
export const useAuthStore = create( | ||
persist<AuthStoreState>( | ||
(set) => ({ | ||
email: "", | ||
setEmail: (email: string) => set({ email }), | ||
authorized: false, | ||
setAuthorized: (authorized: boolean) => set({ authorized }), | ||
}), | ||
{ | ||
name: "email", | ||
storage: createJSONStorage(() => sessionStorage), | ||
} | ||
export const useAuthStore = create<AuthStoreState>()( | ||
devtools( | ||
persist( | ||
(set) => ({ | ||
email: "", | ||
setEmail: (email: string) => set({ email }), | ||
authorized: false, | ||
setAuthorized: (authorized: boolean) => set({ authorized }), | ||
loading: false, | ||
setLoading: (loading: boolean) => set({ loading }), | ||
}), | ||
{ | ||
name: "go-react-auth", | ||
partialize: (state) => ({ | ||
email: state.email, | ||
authorized: state.authorized, | ||
}), | ||
storage: createJSONStorage(() => sessionStorage), | ||
} | ||
) | ||
) | ||
); |
11 changes: 11 additions & 0 deletions
11
client/src/pages/forgot-password/_components/forgot-password-back.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { useAuthStore } from "@/lib/stores/auth-store"; | ||
import { SwitchAuth } from "@/components/switch-auth"; | ||
import { AppRoutes } from "@/constants/routes"; | ||
|
||
const ForgotPasswordBack = () => { | ||
const { loading } = useAuthStore(); | ||
|
||
return <SwitchAuth href={AppRoutes.SignIn} tag="Back" disabled={loading} />; | ||
}; | ||
|
||
export { ForgotPasswordBack }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
client/src/pages/reset-password/_components/reset-password-back.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { useAuthStore } from "@/lib/stores/auth-store"; | ||
import { SwitchAuth } from "@/components/switch-auth"; | ||
import { AppRoutes } from "@/constants/routes"; | ||
|
||
const ResetPasswordBack = () => { | ||
const { loading } = useAuthStore(); | ||
|
||
return <SwitchAuth href={AppRoutes.SignIn} tag="Back" disabled={loading} />; | ||
}; | ||
|
||
export { ResetPasswordBack }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { useAuthStore } from "@/lib/stores/auth-store"; | ||
import { SwitchAuth } from "@/components/switch-auth"; | ||
import { AppRoutes } from "@/constants/routes"; | ||
|
||
const NoAccountYet = () => { | ||
const { loading } = useAuthStore(); | ||
|
||
return ( | ||
<SwitchAuth | ||
label="Don't have an account yet?" | ||
tag="Sign Up" | ||
href={AppRoutes.SignUp} | ||
disabled={loading} | ||
/> | ||
); | ||
}; | ||
|
||
export { NoAccountYet }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
client/src/pages/sign-up/_components/already-have-account.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { useAuthStore } from "@/lib/stores/auth-store"; | ||
import { SwitchAuth } from "@/components/switch-auth"; | ||
import { AppRoutes } from "@/constants/routes"; | ||
|
||
const AlreadyHaveAccount = () => { | ||
const { loading } = useAuthStore(); | ||
|
||
return ( | ||
<SwitchAuth | ||
label="Already have an account?" | ||
tag="Sign In" | ||
href={AppRoutes.SignIn} | ||
disabled={loading} | ||
/> | ||
); | ||
}; | ||
|
||
export { AlreadyHaveAccount }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.