Skip to content

Commit

Permalink
add a logout button (probably not ideal)
Browse files Browse the repository at this point in the history
  • Loading branch information
chantastic committed Jul 15, 2024
1 parent ed545ff commit 2defed1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
10 changes: 9 additions & 1 deletion chan.dev/src/lib/authkit.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {sealData, unsealData} from 'iron-session'
import {WorkOS} from '@workos-inc/node'
import type {User} from '@workos-inc/node'
import {createRemoteJWKSet, jwtVerify} from 'jose'
import {createRemoteJWKSet, jwtVerify, decodeJwt} from 'jose'

export const COOKIE_NAME = 'wos-session'

Expand Down Expand Up @@ -51,6 +51,14 @@ export function getSignInURL() {
})
}

export function getSignOutURL(sessionId: string) {
return workos.userManagement.getLogoutUrl({sessionId})
}

export function getSessionId(session: Session) {
return decodeJwt(session.accessToken).sid
}

export async function authenticateWithCode(code: string) {
return await workos.userManagement.authenticateWithCode({
code,
Expand Down
17 changes: 17 additions & 0 deletions chan.dev/src/pages/auth/callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,21 @@ export const GET: APIRoute = async ({
return redirect('/dashboard')
}

export const POST: APIRoute = async ({cookies, redirect}) => {
const sessionId = String(
AUTHKIT.getSessionId(
await AUTHKIT.decryptSession(
cookies.get(AUTHKIT.COOKIE_NAME)!
)
)
)

cookies.delete(
AUTHKIT.COOKIE_NAME,
AUTHKIT.COOKIE_OPTIONS as AstroCookieSetOptions // critical that options be passed
)

return redirect(AUTHKIT.getSignOutURL(sessionId))
}

export const prerender = false
4 changes: 4 additions & 0 deletions chan.dev/src/pages/dashboard.astro
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ export const prerender = false

<h1>
Hello {session.user.lastName}!

<form method="POST" action="/auth/callback">
<button type="submit">Sign out</button>
</form>
</h1>

0 comments on commit 2defed1

Please sign in to comment.