Skip to content

Commit

Permalink
⚡fix App Auto logout during sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
greatsamist committed Jul 25, 2024
1 parent e9534ba commit 59c8dd3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
7 changes: 1 addition & 6 deletions packages/app/components/authorization/CheckAuthorization.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { apiUrl } from '@/lib/utils/utils'
import { cookies } from 'next/headers'

const CheckAuthorization = async () => {
const privyToken = cookies().get('privy-token')
const userSession = cookies().get('user-session')
const userAddress = cookies().get('user-address')
const res = await fetch(`${apiUrl()}/auth/verify-token`, {
Expand All @@ -14,12 +13,8 @@ const CheckAuthorization = async () => {
headers: { 'Content-Type': 'application/json' },
})
const resData = await res.json()

const isAuthorized =
!!userAddress?.value &&
!!privyToken?.value &&
!!userSession?.value &&
resData.data
!!userAddress?.value && !!userSession?.value && resData.data

return isAuthorized
}
Expand Down
14 changes: 13 additions & 1 deletion packages/app/components/misc/SignInUserButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useLogin, useLogout, usePrivy } from '@privy-io/react-auth'
import { deleteSession, storeSession } from '@/lib/actions/auth'
import { apiUrl } from '@/lib/utils/utils'
import { Loader2 } from 'lucide-react'
import { useState } from 'react'
import React, { useEffect, useState } from 'react'

interface SignInUserButtonProps {
className?: string
Expand All @@ -18,6 +18,13 @@ export const SignInUserButton = ({
const { ready, authenticated } = usePrivy()
const [isLoading, setIsLoading] = useState(false)

const privyRefreshToken = localStorage.getItem(
'privy:refresh_token'
)
const parsePrivyRefreshToken = privyRefreshToken
? JSON.parse(privyRefreshToken)
: null

const getSession = async () => {
const privyToken = localStorage.getItem('privy:token')
const token = privyToken ? JSON.parse(privyToken) : null
Expand All @@ -36,6 +43,11 @@ export const SignInUserButton = ({
})
}

useEffect(() => {
if (!parsePrivyRefreshToken) logout()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [parsePrivyRefreshToken])

const { login } = useLogin({
onComplete: () => {
getSession()
Expand Down

0 comments on commit 59c8dd3

Please sign in to comment.