-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a logout workflow with server action
- Loading branch information
1 parent
873a76c
commit 1d8c825
Showing
3 changed files
with
47 additions
and
0 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,6 @@ | ||
.logoutButton { | ||
padding: 0.25rem 0.5rem; | ||
background: transparent; | ||
border: none; | ||
font-size: 1.125rem; | ||
} |
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,22 @@ | ||
'use client'; | ||
|
||
import { useRouter } from 'next/navigation'; | ||
import { logout } from './action'; | ||
import styles from './LogoutButton.module.scss'; | ||
|
||
export function LogoutButton() { | ||
const router = useRouter(); | ||
return ( | ||
<form> | ||
<button | ||
className={styles.logoutButton} | ||
formAction={async () => { | ||
await logout(); | ||
router.refresh(); | ||
}} | ||
> | ||
logout | ||
</button> | ||
</form> | ||
); | ||
} |
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,19 @@ | ||
'use server'; | ||
|
||
import { cookies } from 'next/headers'; | ||
import { deleteSession } from '../../../database/sessions'; | ||
|
||
export async function logout() { | ||
// Task: Implement the user logout workflow | ||
|
||
// 1. Get the session token from the cookie | ||
const cookieStore = await cookies(); | ||
|
||
const session = cookieStore.get('sessionToken'); | ||
|
||
// 2. Delete the session from the database based on the token | ||
if (session) await deleteSession(session.value); | ||
|
||
// 3. Delete the session cookie from the browser | ||
cookieStore.delete('sessionToken'); | ||
} |