-
Notifications
You must be signed in to change notification settings - Fork 5
Add Sign Out Button when Sign In #71
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
Open
dodok8
wants to merge
12
commits into
main
Choose a base branch
from
sign-out-button
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
43adb0a
Add deleteSessionCookie()
dodok8 898220a
Turn off eslint/require-await and typescript/require-await
dodok8 967d058
Apply HeaderAccountButton
dodok8 853bdf8
Remove argument from RevokeSession Mutation
dodok8 d2ccebc
Revoke Session when Sign out
dodok8 9181a45
Fix deleteSession Cookie logic
dodok8 844e077
Add Error Indicator
dodok8 96d248b
Remove unecccessary fallback
dodok8 591ec97
Add new test scenario to auth.test.ts
dodok8 5bfbbb4
Organize HeaderAccountButton
dodok8 922ab3d
Simplify deleteSession Logic
dodok8 d9b0ed7
Add comment on ErrorBoundary and Suspense
dodok8 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,154 @@ | ||
| // DrFed: A web-based platform for developing and debugging ActivityPub apps | ||
| // Copyright (C) 2026 DrFed team | ||
| // | ||
| // This program is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU Affero General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU Affero General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU Affero General Public License | ||
| // along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| import { Button } from "@kobalte/core/button"; | ||
| import { A, action, useAction, useSubmission } from "@solidjs/router"; | ||
| import { commitMutation, graphql } from "relay-runtime"; | ||
| import { ErrorBoundary, Show, Suspense } from "solid-js"; | ||
| import { createLazyLoadQuery } from "solid-relay"; | ||
|
|
||
| import { createRelayEnvironment } from "~/RelayEnvironment.ts"; | ||
| import { deleteSessionCookie } from "~/session.ts"; | ||
|
|
||
| import type { HeaderAccountButtonQuery } from "./__generated__/HeaderAccountButtonQuery.graphql.ts"; | ||
| import type { RevokeSession } from "./__generated__/RevokeSession.graphql.ts"; | ||
|
|
||
| import styles from "~/styles/app.module.css"; | ||
|
|
||
| const revokeSessionMutation = graphql` | ||
| mutation RevokeSession { | ||
| revokeSession { | ||
| revoke | ||
| } | ||
| } | ||
| `; | ||
|
|
||
| interface revokeSessionResult { | ||
| message: string; | ||
| status: "error" | "success"; | ||
| } | ||
|
|
||
| const signOutAction = action(async () => { | ||
| "use server"; | ||
|
|
||
| const environment = createRelayEnvironment(); | ||
|
|
||
| const result = await new Promise<revokeSessionResult>((resolve) => { | ||
| commitMutation<RevokeSession>(environment, { | ||
| mutation: revokeSessionMutation, | ||
| variables: {}, | ||
| onCompleted: (_response, errors) => { | ||
| const graphQLErrors = errors ?? []; | ||
|
|
||
| if (graphQLErrors.length > 0) { | ||
| resolve({ | ||
| message: graphQLErrors.map((error) => error.message).join("\n"), | ||
| status: "error", | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| deleteSessionCookie(); | ||
|
dodok8 marked this conversation as resolved.
|
||
| } catch { | ||
| resolve({ | ||
| message: "Unable to delete cookie", | ||
| status: "error", | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| resolve({ | ||
| message: "Signed Out", | ||
| status: "success", | ||
| }); | ||
| }, | ||
| onError: (error) => { | ||
| resolve({ | ||
| message: error.message, | ||
| status: "error", | ||
| }); | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| return result; | ||
| }, "sign-out"); | ||
|
|
||
| export function HeaderAccountButton() { | ||
| const signOut = useAction(signOutAction); | ||
| const submission = useSubmission(signOutAction); | ||
| const query = createLazyLoadQuery<HeaderAccountButtonQuery>( | ||
| graphql` | ||
| query HeaderAccountButtonQuery { | ||
| viewer { | ||
| name | ||
| } | ||
| } | ||
| `, | ||
| {}, | ||
| ); | ||
|
|
||
| async function handleSignOut() { | ||
| const signOutResult = await signOut(); | ||
|
|
||
| if (signOutResult.status === "success") { | ||
| globalThis.location.replace("/"); | ||
| } | ||
| } | ||
|
|
||
| return ( | ||
| /* A query failure does not mean the viewer is signed out. Instead, it means sever error or network error, making access to other function in this situation will make another error. */ | ||
| <ErrorBoundary fallback={() => <></>}> | ||
| {/* Avoid auth-state flicker while the viewer query is pending. */} | ||
| <Suspense fallback={<></>}> | ||
|
dodok8 marked this conversation as resolved.
|
||
| <Show when={query()}> | ||
| {(data) => ( | ||
| <Show | ||
| when={data().viewer} | ||
| fallback={ | ||
| <A | ||
| class={styles.headerAction} | ||
| activeClass={styles.active} | ||
| href="/sign-in" | ||
| > | ||
| Sign in | ||
| </A> | ||
| } | ||
| > | ||
| <Button | ||
| type="button" | ||
| class={styles.headerAction} | ||
| disabled={submission.pending} | ||
| title={ | ||
| submission.result?.status === "error" | ||
| ? submission.result.message | ||
| : "Sign Out" | ||
| } | ||
| aria-live="polite" | ||
| onClick={() => void handleSignOut()} | ||
| > | ||
| {submission.result?.status === "error" | ||
| ? submission.result.message | ||
| : "Sign Out"} | ||
| </Button> | ||
| </Show> | ||
| )} | ||
| </Show> | ||
| </Suspense> | ||
| </ErrorBoundary> | ||
| ); | ||
| } | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.