Skip to content

Commit

Permalink
refactor sign out functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkarimoff committed Jun 7, 2024
1 parent b309354 commit 9f7eb59
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
13 changes: 10 additions & 3 deletions actions/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import { hash, verify } from "@node-rs/argon2";
import { eq } from "drizzle-orm";
import { generateIdFromEntropySize } from "lucia";
import { revalidatePath } from "next/cache";
import { cookies } from "next/headers";
import { redirect } from "next/navigation";
import { db } from "~/drizzle/db";
import { user } from "~/drizzle/schema";
import { lucia, validateRequest } from "~/utils/auth";
Expand Down Expand Up @@ -142,9 +142,10 @@ export async function signin(
error: null,
};
} catch (error) {
console.error("Error while signing in", error);
return {
success: false,
error: JSON.stringify(error),
error: "Something went wrong! Please try again.",
};
}
}
Expand All @@ -153,6 +154,7 @@ export async function signout() {
const { session } = await validateRequest();
if (!session) {
return {
success: false,
error: "Unauthorized",
};
}
Expand All @@ -165,5 +167,10 @@ export async function signout() {
sessionCookie.value,
sessionCookie.attributes
);
return redirect("/signin");

revalidatePath("/");
return {
success: true,
error: null,
};
}
11 changes: 1 addition & 10 deletions app/_components/SignOutBtn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,7 @@ import { signout } from "~/actions/auth";
import { Button } from "~/components/ui/button";

const SignOutBtn = () => {
return (
<Button
onClick={async () => {
console.log("signing out");
await signout();
}}
>
Sign Out
</Button>
);
return <Button onClick={() => void signout()}>Sign Out</Button>;
};

export default SignOutBtn;
4 changes: 1 addition & 3 deletions app/signin/_components/SignInForm.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
"use client";

import { useFormState } from "react-dom";
import { signin } from "~/actions/auth";
import { Button } from "~/components/ui/button";
import { Input } from "~/components/ui/input";
import { Label } from "~/components/ui/label";
import { useFormState } from "react-dom";
import { useEffect } from "react";
import { redirect } from "next/navigation";

const SignInForm = () => {
const initialState = { error: null, success: false };
Expand Down

0 comments on commit 9f7eb59

Please sign in to comment.