-
Notifications
You must be signed in to change notification settings - Fork 21
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 #276 from steff456/fix-login
Add logout functionality and improve login/logout design
- Loading branch information
Showing
10 changed files
with
143 additions
and
81 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
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
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,24 @@ | ||
import { apiSlice } from "../api"; | ||
|
||
export const authApiSlice = apiSlice.injectEndpoints({ | ||
endpoints: builder => ({ | ||
getAuth: builder.query<any, void>({ | ||
query: () => "/api/v1/permission/" | ||
}), | ||
logout: builder.mutation({ | ||
query: (logoutUrl: string) => ({ | ||
url: logoutUrl, | ||
method: "POST" | ||
}) | ||
}), | ||
login: builder.mutation({ | ||
query: (loginUrl: string) => ({ | ||
url: loginUrl, | ||
method: "GET" | ||
}) | ||
}) | ||
}) | ||
}); | ||
|
||
export const { useLazyGetAuthQuery, useLogoutMutation, useLoginMutation } = | ||
authApiSlice; |
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,65 @@ | ||
import React, { useState, useEffect } from "react"; | ||
|
||
import { StyledButtonPrimary } from "../../../styles"; | ||
import { PrefContext } from "../../../preferences"; | ||
import { | ||
useLazyGetAuthQuery, | ||
useLogoutMutation, | ||
useLoginMutation | ||
} from "../authApiSlice"; | ||
|
||
export const AuthButton = () => { | ||
const prefs = React.useContext(PrefContext); | ||
const authUrl = prefs.loginUrl; | ||
const pageUrl = window.location.href; | ||
const loginPageUrl = `${authUrl}${pageUrl}`; | ||
const logoutUrl = prefs.logoutUrl; | ||
|
||
const [triggerAuthQuery] = useLazyGetAuthQuery(); | ||
const [triggerLogout] = useLogoutMutation(); | ||
const [triggerLogin] = useLoginMutation(); | ||
|
||
const [authenticated, setAuthenticated] = useState(false); | ||
|
||
const performLogout = async () => { | ||
await triggerLogout(logoutUrl); | ||
setAuthenticated(false); | ||
window.location.href = pageUrl; | ||
}; | ||
|
||
const performLogin = async () => { | ||
await triggerLogin(loginPageUrl); | ||
setAuthenticated(true); | ||
window.location.href = loginPageUrl; | ||
}; | ||
|
||
const handleOnClick = () => { | ||
if (authenticated) { | ||
performLogout(); | ||
} else { | ||
performLogin(); | ||
} | ||
}; | ||
|
||
const getPermissions = async () => { | ||
const { data: permissions } = await triggerAuthQuery(); | ||
setAuthenticated(permissions.data.authenticated); | ||
}; | ||
|
||
useEffect(() => { | ||
(async () => { | ||
getPermissions(); | ||
})(); | ||
}, []); | ||
|
||
return ( | ||
<StyledButtonPrimary | ||
onClick={() => { | ||
handleOnClick(); | ||
}} | ||
sx={{ position: "absolute", top: 14, right: 18 }} | ||
> | ||
{authenticated ? "Log out" : "Log in"} | ||
</StyledButtonPrimary> | ||
); | ||
}; |
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 @@ | ||
export * from "./authButton"; |
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,2 @@ | ||
export * from "./authApiSlice"; | ||
export * from "./components"; |
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