Skip to content

Commit 37f9bd3

Browse files
authored
feat: add signal control to authn flow (#265)
1 parent a2d46a4 commit 37f9bd3

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/pages/login/index.tsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ const Login = () => {
7979
session: string,
8080
credentials: AuthenticationPublicKeyCredential,
8181
username: string,
82+
signal: AbortSignal | undefined,
8283
): Promise<Resp<{ token: string }>> =>
8384
r.post(
8485
"/authn/webauthn_finish_login?username=" + username,
@@ -87,6 +88,7 @@ const Login = () => {
8788
headers: {
8889
session: session,
8990
},
91+
signal,
9092
},
9193
),
9294
)
@@ -95,8 +97,10 @@ const Login = () => {
9597
options: CredentialRequestOptionsJSON
9698
}
9799
const [, getauthntemp] = useFetch(
98-
(username): PResp<Webauthntemp> =>
99-
r.get("/authn/webauthn_begin_login?username=" + username),
100+
(username, signal: AbortSignal | undefined): PResp<Webauthntemp> =>
101+
r.get("/authn/webauthn_begin_login?username=" + username, {
102+
signal,
103+
}),
100104
)
101105
const { searchParams, to } = useRouter()
102106
const isAuthnConditionalAvailable = async (): Promise<boolean> => {
@@ -114,6 +118,7 @@ const Login = () => {
114118
const AuthnSwitch = async () => {
115119
setuseauthn(!useauthn())
116120
}
121+
let AuthnSignal: AbortController | null = null
117122
const AuthnLogin = async (conditional?: boolean) => {
118123
if (!supported()) {
119124
if (!conditional) {
@@ -124,17 +129,21 @@ const Login = () => {
124129
if (conditional && !(await isAuthnConditionalAvailable())) {
125130
return
126131
}
132+
AuthnSignal?.abort()
133+
const controller = new AbortController()
134+
AuthnSignal = controller
127135
changeToken()
128136
const username_login: string = conditional ? "" : username()
129137
if (!conditional && remember() === "true") {
130138
localStorage.setItem("username", username())
131139
} else {
132140
localStorage.removeItem("username")
133141
}
134-
const resp = await getauthntemp(username_login)
142+
const resp = await getauthntemp(username_login, controller.signal)
135143
handleResp(resp, async (data) => {
136144
try {
137145
const options = parseRequestOptionsFromJSON(data.options)
146+
options.signal = controller.signal
138147
if (conditional) {
139148
// @ts-expect-error
140149
options.mediation = "conditional"
@@ -144,6 +153,7 @@ const Login = () => {
144153
data.session,
145154
credentials,
146155
username_login,
156+
controller.signal,
147157
)
148158
handleRespWithoutNotify(resp, (data) => {
149159
notify.success(t("login.success"))
@@ -154,7 +164,8 @@ const Login = () => {
154164
)
155165
})
156166
} catch (error: unknown) {
157-
if (error instanceof Error) notify.error(error.message)
167+
if (error instanceof Error && error.name != "AbortError")
168+
notify.error(error.message)
158169
}
159170
})
160171
}

0 commit comments

Comments
 (0)