-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
164 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,17 +3,56 @@ | |
<page-header image="/image/certify-pending.svg" title="验证邮件已发送" notice="验证邮件已发送至[email protected]" /> | ||
<form @submit="submit"> | ||
<button>已在邮箱中完成验证</button> | ||
<button class="resend" @click="resend">重新发送邮件</button> | ||
<button class="resend" :disabled="messageTimeout > 0" @click="resend">重新发送邮件{{ messageTimeout > 0 ? ` (${messageTimeout})` : '' }}</button> | ||
</form> | ||
<p class="action"><nuxt-link to="/register">无法访问武大邮箱?</nuxt-link></p> | ||
</template> | ||
<script lang="ts" setup> | ||
const route = useRoute(); | ||
const id = route.params.id?.toString() ?? ''; | ||
const messageTimeout = ref(0); | ||
function resetTimeout() { | ||
messageTimeout.value = 60; | ||
const intervalId = setInterval(() => { | ||
messageTimeout.value -= 1; | ||
if (messageTimeout.value === 0) { | ||
clearInterval(intervalId); | ||
} | ||
}, 1000); | ||
} | ||
onMounted(() => { | ||
resetTimeout(); | ||
}); | ||
interface CertifyRes { | ||
is_certified: boolean; | ||
certify_time: string; | ||
} | ||
function submit(e: Event) { | ||
e.preventDefault(); | ||
request<CertifyRes>(`/api/users/${id}/certify/`, { | ||
method: 'GET', | ||
}).then((res) => { | ||
if (res.data.is_certified) { | ||
location.href = '/'; | ||
} | ||
}) | ||
} | ||
interface SendRes { | ||
to: string; | ||
timeout: number; | ||
} | ||
function resend(e: Event) { | ||
e.preventDefault(); | ||
request<SendRes>(`/api/users/${id}/certify/`, { | ||
method: 'POST', | ||
}).then(() => { | ||
resetTimeout(); | ||
}); | ||
} | ||
</script> | ||
<style scoped lang="less"> | ||
|
@@ -27,6 +66,12 @@ form { | |
.resend { | ||
background-color: var(--color-button-secondary-bg); | ||
color: var(--color-primary); | ||
transition: color, background-color .3s; | ||
&[disabled] { | ||
color: var(--color-disabled); | ||
background-color: var(--color-disabled-bg); | ||
} | ||
} | ||
.action { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
interface RefreshRes { | ||
access: string; | ||
expire_time: string; | ||
} | ||
|
||
interface RequestOptions { | ||
method: 'POST' | 'GET' | 'PUT' | 'PATCH' | 'DELETE'; | ||
body?: Record<string, string>; | ||
headers?: Record<string, string>; | ||
} | ||
|
||
export async function request<T>(url: string, options: RequestOptions) { | ||
let access = localStorage.getItem('access'); | ||
const refresh = localStorage.getItem('refresh'); | ||
let exp = localStorage.getItem('exp'); | ||
if (!access || !exp) return; | ||
if (Date.now() > new Date(exp).getTime()) { | ||
try { | ||
const res = await $fetch<ResBody<RefreshRes>>('/auth/refresh/', { | ||
method: 'POST', | ||
body: { | ||
refresh, | ||
}, | ||
}); | ||
localStorage.setItem('access', res.data.access); | ||
localStorage.setItem('exp', res.data.expire_time); | ||
access = res.data.access; | ||
} catch (err) { | ||
throw new Error('Refresh expired'); | ||
} | ||
} | ||
return $fetch<ResBody<T>>(url, { | ||
...options, | ||
headers: { | ||
Authorization: `Bearer ${access}`, | ||
...options.headers, | ||
}, | ||
}).then((res) => { | ||
return res.data; | ||
}); | ||
} |