Skip to content

Commit c55b6bd

Browse files
fix: Fix the sign out
1 parent 16c4c25 commit c55b6bd

File tree

4 files changed

+45
-23
lines changed

4 files changed

+45
-23
lines changed

src/app/(auth)/sign-in/sign-in.client.tsx

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,34 @@ interface PasskeyAuthenticationButtonProps {
2626
}
2727

2828
function PasskeyAuthenticationButton({ className, disabled, children }: PasskeyAuthenticationButtonProps) {
29+
const { execute: generateOptions } = useServerAction(generateAuthenticationOptionsAction, {
30+
onError: (error) => {
31+
toast.dismiss();
32+
toast.error(error.err?.message || "Failed to get authentication options");
33+
},
34+
});
35+
36+
const { execute: verifyAuthentication } = useServerAction(verifyAuthenticationAction, {
37+
onError: (error) => {
38+
toast.dismiss();
39+
toast.error(error.err?.message || "Authentication failed");
40+
},
41+
onSuccess: () => {
42+
toast.dismiss();
43+
toast.success("Authentication successful");
44+
window.location.href = "/dashboard";
45+
},
46+
});
47+
2948
const [isAuthenticating, setIsAuthenticating] = useState(false);
3049

3150
const handleAuthenticate = async () => {
3251
try {
3352
setIsAuthenticating(true);
53+
toast.loading("Authenticating with passkey...");
3454

3555
// Get authentication options from the server
36-
const [options] = await generateAuthenticationOptionsAction({});
56+
const [options] = await generateOptions({});
3757

3858
if (!options) {
3959
throw new Error("Failed to get authentication options");
@@ -45,16 +65,14 @@ function PasskeyAuthenticationButton({ className, disabled, children }: PasskeyA
4565
});
4666

4767
// Send the response back to the server for verification
48-
await verifyAuthenticationAction({
68+
await verifyAuthentication({
4969
response: authenticationResponse,
5070
challenge: options.challenge,
5171
});
52-
53-
toast.success("Authentication successful");
54-
window.location.href = "/dashboard"; // Redirect to dashboard after successful authentication
5572
} catch (error) {
5673
console.error("Passkey authentication error:", error);
57-
toast.error((error as { err?: { message: string } })?.err?.message || "Authentication failed");
74+
toast.dismiss();
75+
toast.error("Authentication failed");
5876
} finally {
5977
setIsAuthenticating(false);
6078
}

src/app/(settings)/settings/security/passkey-settings.actions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ export const verifyAuthenticationAction = createServerAction()
160160
const { verification, credential } = await verifyPasskeyAuthentication(input.response, input.challenge);
161161

162162
if (!verification.verified) {
163-
throw new Error("Passkey authentication failed");
163+
throw new ZSAError("FORBIDDEN", "Passkey authentication failed");
164164
}
165165

166166
await createAndStoreSession(credential.userId, "passkey", input.response.id);

src/components/nav-user.tsx

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,31 +84,35 @@ export function NavUser({
8484
</DropdownMenuLabel>
8585
<DropdownMenuSeparator />
8686
<DropdownMenuGroup>
87-
<DropdownMenuItem>
87+
<DropdownMenuItem className="cursor-pointer">
8888
<Sparkles />
8989
Upgrade to Pro
9090
</DropdownMenuItem>
9191
</DropdownMenuGroup>
9292
<DropdownMenuSeparator />
9393
<DropdownMenuGroup>
94-
<DropdownMenuItem>
94+
<DropdownMenuItem className="cursor-pointer">
9595
<BadgeCheck />
9696
Account
9797
</DropdownMenuItem>
98-
<DropdownMenuItem>
98+
<DropdownMenuItem className="cursor-pointer">
9999
<CreditCard />
100100
Billing
101101
</DropdownMenuItem>
102-
<DropdownMenuItem>
102+
<DropdownMenuItem className="cursor-pointer">
103103
<Bell />
104104
Notifications
105105
</DropdownMenuItem>
106106
</DropdownMenuGroup>
107107
<DropdownMenuSeparator />
108-
<DropdownMenuItem onClick={() => {
109-
signOut()
110-
router.push('/')
111-
}}>
108+
<DropdownMenuItem
109+
onClick={() => {
110+
signOut().then(() => {
111+
router.push('/')
112+
})
113+
}}
114+
className="cursor-pointer"
115+
>
112116
<LogOut />
113117
Log out
114118
</DropdownMenuItem>

src/hooks/useSignOut.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import { useSessionStore } from "@/state/session";
22
import { signOutAction } from "@/actions/sign-out.action";
3-
4-
3+
import { toast } from "sonner";
54
const useSignOut = () => {
65
const { clearSession } = useSessionStore();
76

8-
const signOut = () => {
9-
signOutAction().then(() => {
10-
setTimeout(() => {
11-
clearSession()
12-
}, 200)
13-
})
7+
const signOut = async () => {
8+
toast.loading("Signing out...")
9+
await signOutAction();
10+
clearSession();
11+
await new Promise((resolve) => setTimeout(resolve, 200));
12+
toast.dismiss()
13+
toast.success("Signed out successfully")
1414
}
1515

1616
return { signOut }

0 commit comments

Comments
 (0)