@@ -25,6 +25,25 @@ const captchaKey = ref(import.meta.env.VITE_CAPTCHA_KEY)
2525const isLoading = ref (false )
2626const isLoadingMain = ref (true )
2727
28+ function getRecoveryParams() {
29+ const hashParams = new URLSearchParams (route .hash .replace (' #' , ' ' ))
30+ const queryParams = new URLSearchParams (window .location .search )
31+ return {
32+ accessToken: hashParams .get (' access_token' ) ?? queryParams .get (' access_token' ) ?? ' ' ,
33+ refreshToken: hashParams .get (' refresh_token' ) ?? queryParams .get (' refresh_token' ) ?? ' ' ,
34+ code: queryParams .get (' code' ) ?? hashParams .get (' code' ) ?? ' ' ,
35+ error: queryParams .get (' error' ) ?? hashParams .get (' error' ) ?? ' ' ,
36+ errorDescription: queryParams .get (' error_description' ) ?? hashParams .get (' error_description' ) ?? ' ' ,
37+ }
38+ }
39+
40+ function finishWithError(message : string , error ? : unknown ) {
41+ setErrors (' forgot-password' , [message ], {})
42+ if (error )
43+ console .error (' forgot password error' , error )
44+ isLoading .value = false
45+ }
46+
2847async function step1(form : { email: string }) {
2948 const redirectTo = ` ${import .meta .env .VITE_APP_URL }/forgot_password?step=2 `
3049 // console.log('redirect', redirectTo)
@@ -43,36 +62,46 @@ async function step1(form: { email: string }) {
4362}
4463
4564async function step2(form : { password: string , password_confirm: string }) {
46- const queryString = route .hash .replace (' #' , ' ' )
47- const urlParams = new URLSearchParams (queryString )
48- const access_token = urlParams .get (' access_token' ) ?? ' '
49- const refresh_token = urlParams .get (' refresh_token' ) ?? ' '
50- // login with access_token
51- const { error } = await supabase .auth .setSession ({ refresh_token , access_token })
65+ const { accessToken, refreshToken, code, error, errorDescription } = getRecoveryParams ()
5266 if (error ) {
53- setErrors (' forgot-password' , [error .message ], {})
67+ finishWithError (errorDescription || error )
68+ return
69+ }
70+ if (accessToken && refreshToken ) {
71+ const { error : sessionError } = await supabase .auth .setSession ({ refresh_token: refreshToken , access_token: accessToken })
72+ if (sessionError ) {
73+ finishWithError (sessionError .message , sessionError )
74+ return
75+ }
76+ }
77+ else if (code ) {
78+ const { error : exchangeError } = await supabase .auth .exchangeCodeForSession (code )
79+ if (exchangeError ) {
80+ finishWithError (exchangeError .message , exchangeError )
81+ return
82+ }
83+ }
84+ else {
85+ finishWithError (t (' expired' ))
5486 return
5587 }
5688 const aal = await supabase .auth .mfa .getAuthenticatorAssuranceLevel ()
5789 const { currentLevel, nextLevel } = aal .data !
5890 if (nextLevel !== currentLevel ) {
5991 const { data : mfaFactors, error : mfaError } = await supabase .auth .mfa .listFactors ()
6092 if (mfaError ) {
61- setErrors (' forgot-password' , [mfaError .message ], {})
62- console .error (' Cannot get MFA factors' , mfaError )
93+ finishWithError (mfaError .message , mfaError )
6394 return
6495 }
6596 const factor = mfaFactors .all .find (factor => factor .status === ' verified' )
6697 if (! factor ) {
67- setErrors (' forgot-password' , [' Cannot find MFA factor' ], {})
68- console .error (' Cannot get MFA factors' , mfaError )
98+ finishWithError (' Cannot find MFA factor' )
6999 return
70100 }
71101
72102 const { data : challenge, error : errorChallenge } = await supabase .auth .mfa .challenge ({ factorId: factor .id })
73103 if (errorChallenge ) {
74- setErrors (' forgot-password' , [errorChallenge .message ], {})
75- console .error (' Cannot challenge MFA factor' , errorChallenge )
104+ finishWithError (errorChallenge .message , errorChallenge )
76105 return
77106 }
78107
@@ -119,7 +148,7 @@ async function submit(form: { email: string, password: string, password_confirm:
119148 if (step .value === 1 ) {
120149 await step1 (form )
121150 }
122- else if (step .value === 2 && route . hash ) {
151+ else if (step .value === 2 ) {
123152 await step2 (form )
124153 }
125154}
@@ -130,6 +159,8 @@ watchEffect(() => {
130159 // console.log('router.currentRoute.value.query', router.currentRoute.value.query)
131160 if (router .currentRoute .value .query && router .currentRoute .value .query .step )
132161 step .value = Number .parseInt (router .currentRoute .value .query .step as string )
162+ else if (getRecoveryParams ().accessToken || getRecoveryParams ().refreshToken || getRecoveryParams ().code )
163+ step .value = 2
133164 isLoadingMain .value = false
134165 }
135166})
0 commit comments