Skip to content

Commit

Permalink
feat: add logout handling
Browse files Browse the repository at this point in the history
  • Loading branch information
dawidsowardx committed Oct 10, 2024
1 parent 64759fe commit 1699513
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 5 deletions.
10 changes: 9 additions & 1 deletion apps/dashboard/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@
dAppDefinitionAddress: CURRENT_NETWORK.dashboardDappAddress,
networkId: CURRENT_NETWORK?.id,
logger: Logger(1),
onDisconnect: () => updateAccounts([])
onDisconnect: () => {
updateAccounts([])
authApi.logout()
}
})
rdt.walletApi.setRequestData(
Expand All @@ -110,6 +113,11 @@
rdt.walletApi.walletData$.subscribe(({ accounts }) => {
updateAccounts(accounts)
if (accounts.length > 0) {
authApi.renewAuthToken().mapErr((err) => {
rdt.disconnect()
})
}
})
resolveRDT(rdt)
Expand Down
14 changes: 14 additions & 0 deletions apps/dashboard/src/routes/api/auth/logout/+server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { authController } from '@dashboard/server/auth/controller'
import type { RequestHandler } from './$types'
import { error, json } from '@sveltejs/kit'

export const POST: RequestHandler = async ({ cookies }) => {
await authController.logout(cookies)

return json(
{},
{
status: 200
}
)
}
9 changes: 9 additions & 0 deletions apps/dashboard/src/server/auth/auth-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ export const authApi = {
fetchWrapper<{ challenge: string }>(
(() => serverFetch ?? fetch)()('/api/auth/challenge')
).map(({ data }) => data.challenge),
logout: (serverFetch?: typeof fetch) =>
fetchWrapper<{ authToken: string }>(
(() => serverFetch ?? fetch)()('/api/auth/logout', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
})
),
renewAuthToken: (serverFetch?: typeof fetch) =>
fetchWrapper<{ authToken: string }>(
(() => serverFetch ?? fetch)()('/api/auth/renew', {
Expand Down
7 changes: 6 additions & 1 deletion apps/dashboard/src/server/auth/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Rola } from '@common/rola'
import { SignedChallenge } from '@common/rdt'
import type { GatewayApiClient } from '@common/gateway-sdk'
import { CURRENT_NETWORK } from '@networks'
import { err, errAsync } from 'neverthrow'
import { err, errAsync, okAsync } from 'neverthrow'
import { OAuth2 } from './oauth2'
import { UserModel } from '../user/model'
import type { Cookies } from '@sveltejs/kit'
Expand Down Expand Up @@ -91,9 +91,14 @@ export const AuthController = ({
: err({ reason: 'invalidToken' })
}

const logout = (cookies: Cookies) => {
return oAuth2.logout(cookies)
}

return {
createChallenge,
login,
logout,
renewAuthToken,
isValid: verifyAuthToken
}
Expand Down
11 changes: 8 additions & 3 deletions apps/dashboard/src/server/auth/oauth2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export type OAuth2 = ReturnType<typeof OAuth2>
export const OAuth2 = (input?: Partial<OAuth2Input>) => {
const { secret, refreshToken, authToken }: OAuth2Input = {
secret: JWT_SECRET,
refreshToken: { expiresIn: '30d', key: 'jwt' },
authToken: { expiresIn: '10m' },
refreshToken: { expiresIn: '90d', key: 'jwt' },
authToken: { expiresIn: '90d' },
...(input || {})
}

Expand Down Expand Up @@ -84,16 +84,21 @@ export const OAuth2 = (input?: Partial<OAuth2Input>) => {
const ONE_DAY = 1000 * 60 * 60 * 24
return {
httpOnly: true,
expires: new Date(Date.now() + ONE_DAY),
expires: new Date(Date.now() + ONE_DAY * 90),
sameSite: 'lax',
path: '/'
}
}

const logout = (cookies: Cookies) => {
cookies.delete(refreshToken.key, createRefreshTokenOptions())
}

return {
createTokens,
rotateRefreshToken,
renewAuthToken,
logout,
createRefreshTokenCookie,
verifyToken
}
Expand Down

0 comments on commit 1699513

Please sign in to comment.