8000 refactor: Refactor auth provider by BrunoQuaresma · Pull Request #5782 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

refactor: Refactor auth provider #5782

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

Merged
merged 18 commits into from
Jan 20, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Improve name
  • Loading branch information
BrunoQuaresma committed Jan 19, 2023
commit 3e589f1434304e7005bb594dc73a8caa8cd1ba93
14 changes: 6 additions & 8 deletions site/src/components/AuthProvider/AuthProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,28 @@ import { createContext, FC, PropsWithChildren, useContext } from "react"
import { authMachine } from "xServices/auth/authXService"
import { ActorRefFrom } from "xstate"

interface AuthProviderContextValue {
interface AuthContextValue {
authService: ActorRefFrom<typeof authMachine>
}

const AuthProviderContext = createContext<AuthProviderContextValue | undefined>(
undefined,
)
const AuthContext = createContext<AuthContextValue | undefined>(undefined)

export const AuthProvider: FC<PropsWithChildren> = ({ children }) => {
const authService = useInterpret(authMachine)

return (
<AuthProviderContext.Provider value={{ authService }}>
<AuthContext.Provider value={{ authService }}>
{children}
</AuthProviderContext.Provider>
</AuthContext.Provider>
)
}

type UseAuthReturnType = ReturnType<
typeof useActor<AuthProviderContextValue["authService"]>
typeof useActor<AuthContextValue["authService"]>
>

export const useAuth = (): UseAuthReturnType => {
const context = useContext(AuthProviderContext)
const context = useContext(AuthContext)

if (!context) {
throw new Error("useAuth should be used inside of <AuthProvider />")
Expand Down
0