Log out if authentication error happens during timeline reload

This commit is contained in:
silverpill 2023-03-07 20:28:09 +00:00
parent e297bd9ae0
commit e001850ea8
6 changed files with 22 additions and 16 deletions

View file

@ -19,6 +19,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Fixed text overflow in post subheader. - Fixed text overflow in post subheader.
- Prevent display name from shrinking too much in post header. - Prevent display name from shrinking too much in post header.
- Log out if authentication error happens during timeline reload.
## [1.15.0] - 2023-02-27 ## [1.15.0] - 2023-02-27

View file

@ -1,6 +1,6 @@
import { BACKEND_URL } from "@/constants" import { BACKEND_URL } from "@/constants"
import { PAGE_SIZE, http } from "./common" import { handleResponse, http, PAGE_SIZE } from "./common"
import { Post } from "./posts" import { Post } from "./posts"
import { Profile } from "./users" import { Profile } from "./users"
@ -23,9 +23,6 @@ export async function getNotifications(
queryParams, queryParams,
authToken, authToken,
}) })
const data = await response.json() const data = await handleResponse(response)
if (response.status !== 200) {
throw new Error(data.error_description)
}
return data return data
} }

View file

@ -2,7 +2,7 @@ import { RouteLocationRaw } from "vue-router"
import { BACKEND_URL } from "@/constants" import { BACKEND_URL } from "@/constants"
import { createDidFromEthereumAddress } from "@/utils/did" import { createDidFromEthereumAddress } from "@/utils/did"
import { PAGE_SIZE, http } from "./common" import { handleResponse, http, PAGE_SIZE } from "./common"
import { CustomEmoji } from "./emojis" import { CustomEmoji } from "./emojis"
export const EXTRA_FIELD_COUNT_MAX = 10 export const EXTRA_FIELD_COUNT_MAX = 10
@ -199,13 +199,10 @@ export async function revokeAccessToken(
} }
} }
export async function getCurrentUser(authToken: string): Promise<User | null> { export async function getCurrentUser(authToken: string): Promise<User> {
const url = `${BACKEND_URL}/api/v1/accounts/verify_credentials` const url = `${BACKEND_URL}/api/v1/accounts/verify_credentials`
const response = await http(url, { authToken }) const response = await http(url, { authToken })
if (response.status !== 200) { const data = await handleResponse(response)
return null
}
const data = await response.json()
return data return data
} }

View file

@ -55,7 +55,16 @@ const emit = defineEmits<{(event: "reload-home"): void}>()
function showHomeTimeline() { function showHomeTimeline() {
if (route.name === "home") { if (route.name === "home") {
try {
loadNotifications(ensureAuthToken()) loadNotifications(ensureAuthToken())
} catch (error: any) {
if (error.message === "access token is invalid") {
router.push({ name: "landing-page" })
return
} else {
throw error
}
}
emit("reload-home") emit("reload-home")
} else { } else {
router.push({ name: "home" }) router.push({ name: "home" })

View file

@ -9,10 +9,10 @@ const lastReadId = ref<string | null>(null)
export function useNotifications() { export function useNotifications() {
async function loadNotifications(authToken: string): Promise<void> { async function loadNotifications(authToken: string): Promise<void> {
const notifications_ = await getNotifications(authToken) const items = await getNotifications(authToken)
const marker = await getNotificationMarker(authToken) const marker = await getNotificationMarker(authToken)
// Don't update reactive object until marker is loaded // Don't update reactive object until marker is loaded
notifications.value = notifications_ notifications.value = items
if (marker) { if (marker) {
lastReadId.value = marker.last_read_id lastReadId.value = marker.last_read_id
} }

View file

@ -42,9 +42,11 @@ export function useCurrentUser() {
const token = localStorage.getItem(AUTH_TOKEN_STORAGE_KEY) const token = localStorage.getItem(AUTH_TOKEN_STORAGE_KEY)
if (token) { if (token) {
authToken.value = token authToken.value = token
try {
currentUser.value = await getCurrentUser(token) currentUser.value = await getCurrentUser(token)
if (currentUser.value === null) { } catch (error: any) {
// Failed to get current user, removing invalid token // Failed to get current user, removing invalid token
currentUser.value = null
clearAuthToken() clearAuthToken()
} }
} }