Skip to content

Commit 70d9e16

Browse files
frjowes-otf
authored andcommitted
Fix login bug.
1 parent b45301d commit 70d9e16

2 files changed

Lines changed: 15 additions & 4 deletions

File tree

hypha/apply/users/passkey_views.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from django.contrib.auth.decorators import login_required
77
from django.core.exceptions import PermissionDenied
88
from django.http import JsonResponse
9-
from django.shortcuts import get_object_or_404, render
9+
from django.shortcuts import get_object_or_404, render, resolve_url
1010
from django.utils import timezone
1111
from django.utils.decorators import method_decorator
1212
from django.utils.http import url_has_allowed_host_and_scheme
@@ -250,16 +250,17 @@ def post(self, request):
250250
passkey.save(update_fields=["sign_count", "last_used_at"])
251251

252252
user = passkey.user
253-
login(request, user, backend="django.contrib.auth.backends.ModelBackend")
253+
user.backend = settings.CUSTOM_AUTH_BACKEND
254+
login(request, user)
254255
request.session["passkey_authenticated"] = True
255256

256-
next_url = data.get("next") or "/"
257+
next_url = data.get("next") or resolve_url(settings.LOGIN_REDIRECT_URL)
257258
if not url_has_allowed_host_and_scheme(
258259
next_url,
259260
allowed_hosts={request.get_host()},
260261
require_https=request.is_secure(),
261262
):
262-
next_url = settings.LOGIN_REDIRECT_URL
263+
next_url = resolve_url(settings.LOGIN_REDIRECT_URL)
263264
return JsonResponse({"status": "ok", "redirect_url": next_url})
264265

265266

hypha/static_src/javascript/passkeys.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
window.hypha = window.hypha || {};
1515

1616
window.hypha.passkeys = (function () {
17+
let _conditionalAbortController = null;
1718
function getCsrfToken() {
1819
const el = document.querySelector("[name=csrfmiddlewaretoken]");
1920
if (el) return el.value;
@@ -126,6 +127,12 @@ window.hypha.passkeys = (function () {
126127
* Authenticate with a passkey via an explicit button click on the login page.
127128
*/
128129
async function authenticate() {
130+
// Abort any in-progress conditional mediation before starting explicit auth.
131+
if (_conditionalAbortController) {
132+
_conditionalAbortController.abort();
133+
_conditionalAbortController = null;
134+
}
135+
129136
const beginUrl = document.getElementById("passkey-auth-begin-url")?.value;
130137
const completeUrl = document.getElementById(
131138
"passkey-auth-complete-url"
@@ -182,6 +189,8 @@ window.hypha.passkeys = (function () {
182189
)?.value;
183190
if (!beginUrl || !completeUrl) return;
184191

192+
_conditionalAbortController = new AbortController();
193+
185194
try {
186195
const beginResp = await jsonPost(beginUrl, {});
187196
if (!beginResp.ok) return;
@@ -192,6 +201,7 @@ window.hypha.passkeys = (function () {
192201
const credential = await navigator.credentials.get({
193202
publicKey: PublicKeyCredential.parseRequestOptionsFromJSON(authOptions),
194203
mediation: "conditional",
204+
signal: _conditionalAbortController.signal,
195205
});
196206

197207
if (!credential) return;

0 commit comments

Comments
 (0)