/ parenscript / auth-ui.lisp
auth-ui.lisp
 1  ;;;; auth-ui.lisp - ParenScript version of auth-ui.js
 2  ;;;; Handle authentication UI state across all pages
 3  
 4  (in-package #:asteroid)
 5  
 6  (defparameter *auth-ui-js*
 7    (ps:ps*
 8     '(progn
 9      
10      ;; Check if user is logged in by calling the API
11      (defun check-auth-status ()
12        (ps:chain
13         (fetch "/api/asteroid/auth-status")
14         (then (lambda (response)
15                 (ps:chain response (json))))
16         (then (lambda (result)
17                 ;; api-output wraps response in {status, message, data}
18                 (let ((data (or (ps:@ result data) result)))
19                   data)))
20         (catch (lambda (error)
21                  (ps:chain console (error "Error checking auth status:" error))
22                  (ps:create :logged-in false
23                            :is-admin false)))))
24      
25      ;; Update UI based on authentication status
26      (defun update-auth-ui (auth-status)
27        ;; Show/hide elements based on login status
28        (ps:chain document
29                  (query-selector-all "[data-show-if-logged-in]")
30                  (for-each (lambda (el)
31                             (setf (ps:@ el style display)
32                                   (if (ps:@ auth-status logged-in)
33                                       "inline-block"
34                                       "none")))))
35        
36        (ps:chain document
37                  (query-selector-all "[data-show-if-logged-out]")
38                  (for-each (lambda (el)
39                             (setf (ps:@ el style display)
40                                   (if (ps:@ auth-status logged-in)
41                                       "none"
42                                       "inline-block")))))
43        
44        (ps:chain document
45                  (query-selector-all "[data-show-if-admin]")
46                  (for-each (lambda (el)
47                             (setf (ps:@ el style display)
48                                   (if (ps:@ auth-status is-admin)
49                                       "inline-block"
50                                       "none"))))))
51      
52      ;; Initialize auth UI on page load
53      (ps:chain document
54                (add-event-listener
55                 "DOMContentLoaded"
56                 (lambda ()
57                   (ps:chain console (log "Auth UI initializing..."))
58                   (ps:chain (check-auth-status)
59                             (then (lambda (auth-status)
60                                     (ps:chain console (log "Auth status:" auth-status))
61                                     (update-auth-ui auth-status)
62                                     (ps:chain console (log "Auth UI updated")))))))))
63    "Compiled JavaScript for auth UI - generated at load time"))
64  
65  (defun generate-auth-ui-js ()
66    "Return the pre-compiled JavaScript for authentication UI"
67    *auth-ui-js*)