1+ import {
2+ ACCOUNT_SERVICE_READY_MESSAGE ,
3+ accountActionFailureMessage ,
4+ accountActionUnavailableMessage ,
5+ requestAccountAuth ,
6+ } from "./account-auth-service.js" ;
7+
18const form = document . querySelector ( "[data-account-auth-form]" ) ;
29const emailField = document . querySelector ( "[data-account-auth-email]" ) ;
310const passwordField = document . querySelector ( "[data-account-auth-password]" ) ;
411const statusField = document . querySelector ( "[data-account-auth-status]" ) ;
512const submitButton = document . querySelector ( "[data-account-auth-submit]" ) ;
613const action = form ?. getAttribute ( "data-account-auth-form" ) || "" ;
7- const ACCOUNT_IDENTITY_SETUP_MESSAGE = "Account identity setup is incomplete. Please contact support." ;
8- const AUTH_UNAVAILABLE_MESSAGE = "The site is currently unavailable. Please try again later." ;
9- const PASSWORD_RESET_RATE_LIMIT_MESSAGE = "Too many reset requests. Please wait and try again later." ;
10- const CREATE_ACCOUNT_PLACEHOLDER_MESSAGE = "Create Account is not available in this preview. Please try again later." ;
11- const PASSWORD_RESET_PLACEHOLDER_MESSAGE = "Password Reset is not available in this preview. Please try again later." ;
1214let authStatus = null ;
1315
1416function setStatus ( message ) {
@@ -17,69 +19,21 @@ function setStatus(message) {
1719 }
1820}
1921
20- function isStaticOnlyLocalEntrypoint ( ) {
21- return [ "127.0.0.1" , "localhost" ] . includes ( window . location . hostname ) &&
22- window . location . port === "5500" ;
23- }
24-
2522function setFormEnabled ( enabled ) {
2623 if ( submitButton ) {
2724 submitButton . disabled = ! enabled ;
2825 }
2926}
3027
31- async function readJson ( response , fallbackMessage ) {
32- let payload = null ;
33- try {
34- payload = await response . json ( ) ;
35- } catch {
36- payload = null ;
37- }
38- if ( ! response . ok || payload ?. ok === false ) {
39- if ( payload ?. error === ACCOUNT_IDENTITY_SETUP_MESSAGE ) {
40- throw new Error ( ACCOUNT_IDENTITY_SETUP_MESSAGE ) ;
41- }
42- if ( payload ?. error === PASSWORD_RESET_RATE_LIMIT_MESSAGE ) {
43- throw new Error ( PASSWORD_RESET_RATE_LIMIT_MESSAGE ) ;
44- }
45- throw new Error ( fallbackMessage ) ;
46- }
47- return payload ?. data || { } ;
48- }
49-
50- async function requestAccountAuth ( path , options = { } ) {
51- const response = await fetch ( `/api/auth/${ path } ` , {
52- body : options . body ? JSON . stringify ( options . body ) : undefined ,
53- headers : options . body ? { "content-type" : "application/json" } : undefined ,
54- method : options . method || "GET" ,
55- } ) ;
56- return readJson ( response , AUTH_UNAVAILABLE_MESSAGE ) ;
57- }
58-
5928function unavailableStatusMessage ( ) {
60- if ( action === "create-account" ) {
61- return CREATE_ACCOUNT_PLACEHOLDER_MESSAGE ;
62- }
63- if ( action === "password-reset" ) {
64- return PASSWORD_RESET_PLACEHOLDER_MESSAGE ;
65- }
66- return "Account action is not available in this preview. Please try again later." ;
29+ return accountActionUnavailableMessage ( action ) ;
6730}
6831
6932async function refreshAccountAuthStatus ( ) {
70- if ( isStaticOnlyLocalEntrypoint ( ) ) {
71- authStatus = {
72- ready : false ,
73- message : unavailableStatusMessage ( ) ,
74- } ;
75- setFormEnabled ( false ) ;
76- setStatus ( authStatus . message ) ;
77- return authStatus ;
78- }
7933 try {
80- authStatus = await requestAccountAuth ( "status" ) ;
34+ authStatus = await requestAccountAuth ( "status" , { } , unavailableStatusMessage ( ) ) ;
8135 setFormEnabled ( Boolean ( authStatus . ready ) ) ;
82- setStatus ( authStatus . ready ? "Account service is available." : unavailableStatusMessage ( ) ) ;
36+ setStatus ( authStatus . ready ? ACCOUNT_SERVICE_READY_MESSAGE : unavailableStatusMessage ( ) ) ;
8337 } catch {
8438 authStatus = {
8539 ready : false ,
@@ -111,7 +65,7 @@ form?.addEventListener("submit", (event) => {
11165 }
11266 const endpoint = actionEndpoint ( ) ;
11367 if ( ! endpoint ) {
114- throw new Error ( "Unknown account action." ) ;
68+ throw new Error ( accountActionFailureMessage ( action ) ) ;
11569 }
11670 const body = {
11771 email : emailField ?. value || "" ,
@@ -122,15 +76,15 @@ form?.addEventListener("submit", (event) => {
12276 return requestAccountAuth ( endpoint , {
12377 body,
12478 method : "POST" ,
125- } ) ;
79+ } , accountActionFailureMessage ( action ) ) ;
12680 } )
12781 . then ( ( result ) => {
12882 if ( result ) {
12983 setStatus ( result . message || "Account action completed." ) ;
13084 }
13185 } )
13286 . catch ( ( error ) => {
133- setStatus ( error instanceof Error ? error . message : AUTH_UNAVAILABLE_MESSAGE ) ;
87+ setStatus ( error instanceof Error ? error . message : accountActionFailureMessage ( action ) ) ;
13488 } ) ;
13589} ) ;
13690
0 commit comments