-
Notifications
You must be signed in to change notification settings - Fork 14
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 #7 from sahil143/auth
feat: add authentication and webpack local proxy
- Loading branch information
Showing
15 changed files
with
477 additions
and
8 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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
AUTH_URL=https://127.0.0.1:9443/ | ||
REGISTRATION_URL=https://127.0.0.1:9443/ | ||
PROXY_URL=https://127.0.0.1:9443/ | ||
PROXY_WEBSOCKET_URL=wss://127.0.0.1:9443/ |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file was deleted.
Oops, something went wrong.
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,63 @@ | ||
import * as React from 'react'; | ||
import { Bullseye, Spinner } from '@patternfly/react-core'; | ||
|
||
export type AuthContextType = { | ||
user: { email: string | null }; | ||
isAuthenticated: boolean; | ||
signOut?: () => void; | ||
}; | ||
|
||
const redirectToLogin = () => { | ||
window.location.replace(`/oauth2/sign_in?rd=${window.location.pathname}`); | ||
}; | ||
|
||
export const AuthContext = React.createContext<AuthContextType>({ | ||
user: { email: null }, | ||
isAuthenticated: false, | ||
}); | ||
|
||
export const AuthProvider: React.FC<React.PropsWithChildren> = ({ children }) => { | ||
const [user, setUser] = React.useState<AuthContextType['user']>({ email: null }); | ||
const [isAuthenticated, setIsAuthenticated] = React.useState<boolean>(false); | ||
|
||
React.useEffect(() => { | ||
const checkAuthStatus = async () => { | ||
try { | ||
const userData = await fetch('/oauth2/userinfo'); | ||
if (userData?.status === 401) { | ||
redirectToLogin(); | ||
} else { | ||
setUser((await userData.json()) as AuthContextType['user']); | ||
setIsAuthenticated(true); | ||
} | ||
} catch (err) { | ||
// eslint-disable-next-line no-console | ||
console.log('Error while Authenticating the user', err); | ||
} | ||
}; | ||
void checkAuthStatus(); | ||
}, []); | ||
|
||
const signOut = async () => { | ||
await fetch('/oauth2/sign_out'); | ||
redirectToLogin(); | ||
}; | ||
|
||
return ( | ||
<AuthContext.Provider | ||
value={{ | ||
user, | ||
isAuthenticated, | ||
signOut, | ||
}} | ||
> | ||
{isAuthenticated ? ( | ||
children | ||
) : ( | ||
<Bullseye> | ||
<Spinner /> | ||
</Bullseye> | ||
)} | ||
</AuthContext.Provider> | ||
); | ||
}; |
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,4 @@ | ||
import * as React from 'react'; | ||
import { AuthContext, AuthContextType } from './AuthContext'; | ||
|
||
export const useAuth = (): AuthContextType => React.useContext(AuthContext); |
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,15 @@ | ||
import * as React from 'react'; | ||
import { Toolbar, ToolbarContent, ToolbarItem } from '@patternfly/react-core'; | ||
import { UserDropdown } from './UserDropdown'; | ||
|
||
export const Header: React.FC = () => { | ||
return ( | ||
<Toolbar isFullHeight> | ||
<ToolbarContent> | ||
<ToolbarItem align={{ default: 'alignRight' }}> | ||
<UserDropdown /> | ||
</ToolbarItem> | ||
</ToolbarContent> | ||
</Toolbar> | ||
); | ||
}; |
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,42 @@ | ||
import * as React from 'react'; | ||
import { | ||
Dropdown, | ||
DropdownGroup, | ||
DropdownItem, | ||
DropdownList, | ||
MenuToggle, | ||
} from '@patternfly/react-core'; | ||
import { useAuth } from '../../auth/useAuth'; | ||
|
||
export const UserDropdown: React.FC = () => { | ||
const [isOpen, setIsOpen] = React.useState(false); | ||
const { | ||
user: { email }, | ||
signOut, | ||
} = useAuth(); | ||
return ( | ||
<Dropdown | ||
aria-label="User action" | ||
isOpen={isOpen} | ||
onSelect={() => setIsOpen(!isOpen)} | ||
onOpenChange={setIsOpen} | ||
toggle={(toggleRef) => ( | ||
<MenuToggle | ||
ref={toggleRef} | ||
isFullHeight | ||
onClick={() => { | ||
setIsOpen(!isOpen); | ||
}} | ||
> | ||
{email} | ||
</MenuToggle> | ||
)} | ||
> | ||
<DropdownGroup> | ||
<DropdownList> | ||
<DropdownItem onClick={() => signOut?.()}>Log out</DropdownItem> | ||
</DropdownList> | ||
</DropdownGroup> | ||
</Dropdown> | ||
); | ||
}; |
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,3 @@ | ||
#root { | ||
height: 100%; | ||
} |
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
Oops, something went wrong.