fix: redesign password recovery flow
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
import { ref, onMounted, onUnmounted } from 'vue'
|
||||
import { useRouter } from 'vue-router'
|
||||
import { NButton, NCard, NResult, NSpin } from 'naive-ui'
|
||||
import { createClient } from '@supabase/supabase-js'
|
||||
import { createDetachedAuthClient, resolveEmailAuthCallback } from '@/lib/authEmail'
|
||||
|
||||
const router = useRouter()
|
||||
|
||||
@@ -22,58 +22,20 @@ function startCountdown() {
|
||||
|
||||
onMounted(async () => {
|
||||
try {
|
||||
const search = new URLSearchParams(window.location.search)
|
||||
const hash = window.location.hash.slice(1)
|
||||
const params = new URLSearchParams(hash)
|
||||
const code = search.get('code')
|
||||
const accessToken = params.get('access_token')
|
||||
const refreshToken = params.get('refresh_token')
|
||||
const type = params.get('type')
|
||||
const error = search.get('error') ?? params.get('error')
|
||||
const confirmClient = createDetachedAuthClient()
|
||||
const result = await resolveEmailAuthCallback({
|
||||
client: confirmClient,
|
||||
allowedTypes: ['signup', 'email'],
|
||||
})
|
||||
|
||||
if (error) {
|
||||
if (!result.ok) {
|
||||
state.value = 'failed'
|
||||
return
|
||||
}
|
||||
|
||||
if (code || (type === 'signup' && accessToken && refreshToken)) {
|
||||
const confirmClient = createClient(
|
||||
import.meta.env.VITE_SUPABASE_URL,
|
||||
import.meta.env.VITE_SUPABASE_PUBLISHABLE_KEY,
|
||||
{
|
||||
auth: {
|
||||
detectSessionInUrl: false,
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
if (code) {
|
||||
const { error: exchangeError } = await confirmClient.auth.exchangeCodeForSession(code)
|
||||
if (exchangeError) {
|
||||
state.value = 'failed'
|
||||
return
|
||||
}
|
||||
} else {
|
||||
const { error: setSessionError } = await confirmClient.auth.setSession({
|
||||
access_token: accessToken!,
|
||||
refresh_token: refreshToken!,
|
||||
})
|
||||
|
||||
if (setSessionError) {
|
||||
state.value = 'failed'
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
await confirmClient.auth.signOut()
|
||||
|
||||
window.history.replaceState(null, '', window.location.pathname)
|
||||
state.value = 'success'
|
||||
startCountdown()
|
||||
return
|
||||
}
|
||||
|
||||
state.value = 'failed'
|
||||
await confirmClient.auth.signOut()
|
||||
state.value = 'success'
|
||||
startCountdown()
|
||||
} catch {
|
||||
state.value = 'failed'
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user