feat: add Sentry events to monitor random logouts

This commit is contained in:
Stojan Dimitrovski committed 2023-06-23 18:22:44 +02:00
1 parent c87a5efe07
commit 222bdec204
5 files changed
+72 -2

No files matched your search

@@ -3,10 +3,12 @@ import { useQueryClient } from '@tanstack/react-query'
import { useStore } from 'hooks'
import { usePushNext } from 'hooks/misc/useAutoAuthRedirect'
import { auth } from 'lib/gotrue'
import { incrementSignInClicks } from 'lib/local-storage'
import Link from 'next/link'
import { useRef, useState } from 'react'
import { Button, Form, Input } from 'ui'
import { object, string } from 'yup'
import * as Sentry from '@sentry/nextjs'
const signInSchema = object({
email: string().email('Must be a valid email').required('Email is required'),
@@ -33,6 +35,11 @@ const SignInForm = () => {
token = captchaResponse?.response ?? null
}
const signInClicks = incrementSignInClicks()
if (signInClicks > 1) {
Sentry.captureMessage('Sign in without previous sing out detected')
}
const { error } = await auth.signInWithPassword({
email,
password,
@@ -1,7 +1,9 @@
import { useState } from 'react'
import { BASE_PATH } from 'lib/constants'
import { auth, getReturnToPath } from 'lib/gotrue'
import { incrementSignInClicks } from 'lib/local-storage'
import { Button, IconGitHub } from 'ui'
import * as Sentry from '@sentry/nextjs'
const SignInWithGitHub = () => {
const [loading, setLoading] = useState(false)
@@ -10,6 +12,11 @@ const SignInWithGitHub = () => {
setLoading(true)
try {
const signInClicks = incrementSignInClicks()
if (signInClicks > 1) {
Sentry.captureMessage('Sign in without previous sign out detected')
}
const { error } = await auth.signInWithOAuth({
provider: 'github',
options: {
@@ -7,6 +7,7 @@ import { useStore } from 'hooks'
import { post } from 'lib/common/fetch'
import { API_URL, BASE_PATH } from 'lib/constants'
import { passwordSchema } from 'lib/schemas'
import { resetSignInClicks } from 'lib/local-storage'
import PasswordConditionsHelper from './PasswordConditionsHelper'
const signUpSchema = passwordSchema.shape({
@@ -33,6 +34,8 @@ const SignUpForm = () => {
token = captchaResponse?.response ?? null
}
resetSignInClicks()
const response = await post(`${API_URL}/signup`, {
email,
password,
+3 -1
View File
@@ -12,7 +12,7 @@ import { useProfileQuery } from 'data/profile/profile-query'
import { useStore } from 'hooks'
import Telemetry from 'lib/telemetry'
import { GOTRUE_ERRORS, IS_PLATFORM } from './constants'
import { clearLocalStorage } from './local-storage'
import { clearLocalStorage, resetSignInClicks } from './local-storage'
import { useProfileCreateMutation } from 'data/profile/profile-create-mutation'
export const AuthContext = AuthContextInternal
@@ -99,6 +99,8 @@ export function useSignOut() {
const queryClient = useQueryClient()
return useCallback(async () => {
resetSignInClicks()
const result = await gotrueClient.signOut()
clearLocalStorage()
await queryClient.resetQueries()
+52 -1
View File
@@ -1,4 +1,11 @@
export const LOCAL_STORAGE_KEYS_ALLOWLIST = ['graphiql:theme', 'theme', 'supabaseDarkMode']
import { IS_PLATFORM } from './constants'
export const LOCAL_STORAGE_KEYS_ALLOWLIST = [
'graphiql:theme',
'theme',
'supabaseDarkMode',
'supabase.dashboard.sign_in_clicks',
]
export function clearLocalStorage() {
for (const key in localStorage) {
@@ -7,3 +14,47 @@ export function clearLocalStorage() {
}
}
}
function inferSignInClicks() {
if (localStorage.getItem('supabase.dashboard.sign_in_clicks')) {
return
}
localStorage.setItem(
'supabase.dashboard.sign_in_clicks',
localStorage.getItem('supabase.dashboard.auth.token') ? '1' : '0'
)
}
export function getSignInClicks(): number {
let count: number | null = null
try {
count = JSON.parse(localStorage.getItem('supabase.dashboard.sign_in_clicks') || '0')
} catch (e: any) {
// do nothing
}
return count || 0
}
export function incrementSignInClicks(): number {
const clicks = getSignInClicks()
localStorage.setItem('supabase.dashboard.sign_in_clicks', JSON.stringify(clicks + 1))
return clicks + 1
}
export function resetSignInClicks(): number {
const clicks = getSignInClicks()
localStorage.setItem('supabase.dashboard.sign_in_clicks', '0')
return clicks
}
if (globalThis && globalThis.localStorage && IS_PLATFORM) {
// populate the value based on the current local storage state
inferSignInClicks()
}