Skip to content

Commit

Permalink
refact: web: remove refresh token interval (#309)
Browse files Browse the repository at this point in the history
  • Loading branch information
crlssn authored Dec 22, 2024
1 parent 017d896 commit 0f85744
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 19 deletions.
6 changes: 0 additions & 6 deletions web/src/jwt/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,3 @@ export async function refreshAccessTokenOrLogout(): Promise<void> {
const authStore = useAuthStore()
authStore.setAccessToken(res.accessToken)
}

export function scheduleTokenRefresh(): number {
console.debug('scheduling access token refresh every 10 minutes')
const interval = 10 * 60 * 1000 // 10 minutes
return window.setInterval(refreshAccessTokenOrLogout, interval)
}
8 changes: 4 additions & 4 deletions web/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { createPinia } from 'pinia'
import { useAuthStore } from '@/stores/auth'
import { useNotificationStore } from '@/stores/notifications.ts'
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'
import { refreshAccessTokenOrLogout, scheduleTokenRefresh } from '@/jwt/jwt'
import { refreshAccessTokenOrLogout } from '@/jwt/jwt'

import App from './App.vue'
import router from './router/router'
Expand All @@ -19,10 +19,10 @@ app.use(pinia)

const init = async () => {
const authStore = useAuthStore()
const notificationStore = useNotificationStore()
if (authStore.accessToken) {
if (authStore.authorised) {
await refreshAccessTokenOrLogout()
authStore.setAccessTokenRefreshInterval(scheduleTokenRefresh())

const notificationStore = useNotificationStore()
notificationStore.streamUnreadNotifications()
}

Expand Down
7 changes: 0 additions & 7 deletions web/src/stores/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export const useAuthStore = defineStore(
() => {
const userId = ref('')
const accessToken = ref('')
const accessTokenRefreshInterval = ref(0)

const setAccessToken = (token: string) => {
if (userId.value === '') {
Expand All @@ -22,11 +21,6 @@ export const useAuthStore = defineStore(
const logout = () => {
userId.value = ''
accessToken.value = ''
clearInterval(accessTokenRefreshInterval.value)
}

const setAccessTokenRefreshInterval = (interval: number) => {
accessTokenRefreshInterval.value = interval
}

const authorised = computed(() => {
Expand All @@ -37,7 +31,6 @@ export const useAuthStore = defineStore(
accessToken,
logout,
setAccessToken,
setAccessTokenRefreshInterval,
userId,
authorised,
}
Expand Down
8 changes: 8 additions & 0 deletions web/src/stores/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { defineStore } from 'pinia'
import { create } from '@bufbuild/protobuf'
import { notificationClient } from '@/http/clients.ts'
import { UnreadNotificationsRequestSchema } from '@/proto/api/v1/notification_service_pb.ts'
import { Code, ConnectError } from '@connectrpc/connect'
import { refreshAccessTokenOrLogout } from '@/jwt/jwt.ts'

export const useNotificationStore = defineStore('notifications', () => {
const unreadCount = ref(0)
Expand All @@ -18,6 +20,12 @@ export const useNotificationStore = defineStore('notifications', () => {

break
} catch (error) {
if (error instanceof ConnectError) {
if (error.code === Code.Unauthenticated) {
await refreshAccessTokenOrLogout()
}
}

console.error('Stream disconnected, retrying...', error)
}

Expand Down
2 changes: 0 additions & 2 deletions web/src/ui/auth/UserLogin.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { ref } from 'vue'
import router from '@/router/router'
import { login } from '@/http/requests'
import { useAuthStore } from '@/stores/auth'
import { scheduleTokenRefresh } from '@/jwt/jwt'
import { RouterLink } from 'vue-router'
import AppButton from '@/ui/components/AppButton.vue'
import { useNotificationStore } from '@/stores/notifications.ts'
Expand All @@ -18,7 +17,6 @@ const onLogin = async () => {
const res = await login(email.value, password.value)
if (!res) return
authStore.setAccessToken(res.accessToken)
authStore.setAccessTokenRefreshInterval(scheduleTokenRefresh())
notificationStore.streamUnreadNotifications()
await router.push('/home')
}
Expand Down

0 comments on commit 0f85744

Please sign in to comment.