@@ -5,20 +5,137 @@ const publishableKey = import.meta.env.VITE_CLERK_PUBLISHABLE_KEY;
5
5
const clerk = new Clerk ( publishableKey ) ;
6
6
await clerk . load ( ) ;
7
7
8
- if ( clerk . isSignedIn ( ) ) {
9
- document . getElementById ( "app" ) . innerHTML = `
8
+ if ( clerk . isSignedIn ) {
9
+ // Mount user button component - or any sort of protected UI relying on the user
10
+ document . getElementById ( "signed-in" ) . innerHTML = `
10
11
<div id="user-button"></div>
11
12
` ;
12
13
13
- const userButtonDiv = document . getElementById ( "user-button" ) ;
14
+ const userbuttonDiv = document . getElementById ( "user-button" ) ;
14
15
15
- clerk . mountUserButton ( userButtonDiv ) ;
16
+ clerk . mountUserButton ( userbuttonDiv ) ;
16
17
} else {
17
- document . getElementById ( "app" ) . innerHTML = `
18
- <div id="sign-in"></div>
19
- ` ;
18
+ // Handle the sign-in form
19
+ document
20
+ . getElementById ( "sign-in-form" )
21
+ . addEventListener ( "submit" , async ( e ) => {
22
+ e . preventDefault ( ) ;
23
+
24
+ const formData = new FormData ( e . target ) ;
25
+ const emailAddress = formData . get ( "email" ) ;
26
+ const password = formData . get ( "password" ) ;
27
+
28
+ try {
29
+ // Start the sign-in process
30
+ const signInAttempt = await clerk . client . signIn . create ( {
31
+ identifier : emailAddress ,
32
+ password,
33
+ } ) ;
34
+
35
+ // If the sign-in is complete, set the user as active
36
+ if ( signInAttempt . status === "complete" ) {
37
+ await clerk . setActive ( { session : signInAttempt . createdSessionId } ) ;
38
+ await displayNextTaskIfAny ( ) ;
39
+ } else {
40
+ // If the status is not complete, check why. User may need to
41
+ // complete further steps.
42
+ console . error ( JSON . stringify ( signInAttempt , null , 2 ) ) ;
43
+ }
44
+ } catch ( error ) {
45
+ // See https://clerk.com/docs/custom-flows/error-handling
46
+ // for more info on error handling
47
+ console . error ( error ) ;
48
+ }
49
+ } ) ;
50
+
51
+ // Handle the sign-up form
52
+ document
53
+ . getElementById ( "sign-up-form" )
54
+ . addEventListener ( "submit" , async ( e ) => {
55
+ e . preventDefault ( ) ;
56
+
57
+ const formData = new FormData ( e . target ) ;
58
+ const emailAddress = formData . get ( "email" ) ;
59
+ const password = formData . get ( "password" ) ;
60
+
61
+ try {
62
+ // Start the sign-up process using the email and password provided
63
+ await clerk . client . signUp . create ( { emailAddress, password } ) ;
64
+ await clerk . client . signUp . prepareEmailAddressVerification ( ) ;
65
+ // Hide first-factor form
66
+ document . getElementById ( "sign-up" ) . setAttribute ( "hidden" , "" ) ;
67
+ document . getElementById ( "sign-in" ) . setAttribute ( "hidden" , "" ) ;
68
+
69
+ // Show verification form
70
+ document . getElementById ( "verifying" ) . removeAttribute ( "hidden" ) ;
71
+ } catch ( error ) {
72
+ // See https://clerk.com/docs/custom-flows/error-handling
73
+ // for more info on error handling
74
+ console . error ( error ) ;
75
+ }
76
+ } ) ;
77
+
78
+ // Handle the verification form
79
+ document . getElementById ( "verifying" ) . addEventListener ( "submit" , async ( e ) => {
80
+ const formData = new FormData ( e . target ) ;
81
+ const code = formData . get ( "code" ) ;
82
+
83
+ try {
84
+ // Use the code the user provided to attempt verification
85
+ const signUpAttempt =
86
+ await clerk . client . signUp . attemptEmailAddressVerification ( {
87
+ code,
88
+ } ) ;
89
+
90
+ // Now that the user is created, set the session to active.
91
+ // TODO: Update this with clerk.setCurrentSession(signUpAttempt.createdSessionId)
92
+ await clerk . setActive ( { session : signUpAttempt . createdSessionId } ) ;
93
+ // Display organization selection UI if session doesn't have a organization selected
94
+ await displayNextTaskIfAny ( ) ;
95
+ } catch ( error ) {
96
+ // See https://clerk.com/docs/custom-flows/error-handling
97
+ // for more info on error handling
98
+ console . error ( error ) ;
99
+ }
100
+ } ) ;
101
+
102
+ // Handle organization selection
103
+ document
104
+ . getElementById ( "create-organization" )
105
+ . addEventListener ( "submit" , async ( e ) => {
106
+ e . preventDefault ( ) ;
107
+
108
+ const inputEl = document . getElementById ( "name" ) ;
109
+
110
+ if ( ! inputEl ) {
111
+ return ;
112
+ }
113
+
114
+ clerk
115
+ . createOrganization ( { name : inputEl . value } )
116
+ . then ( ( res ) => console . log ( res ) )
117
+ . catch ( ( error ) => console . log ( "An error occurred:" , error ) ) ;
118
+
119
+ // Hide the organization creation form after successful creation
120
+ document
121
+ . getElementById ( "create-organization" )
122
+ . setAttribute ( "hidden" , "true" ) ;
123
+ // Handle any remaining tasks in the flow
124
+ await displayNextTaskIfAny ( ) ;
125
+ } ) ;
126
+ }
127
+
128
+ // Check for any pending tasks (like organization creation) and display the corresponding UI element
129
+ async function displayNextTaskIfAny ( ) {
130
+ const task = await clerk . __experimental_nextTask ( ) ;
20
131
21
- const signInDiv = document . getElementById ( "sign-in" ) ;
132
+ const mapTaskKeyToElementId = {
133
+ org : "create-organization" ,
134
+ } ;
22
135
23
- clerk . mountSignIn ( signInDiv ) ;
136
+ // Here we're toggling UI visibility, but you could also navigate to different pages
137
+ // if you're application supports client-side routing
138
+ document
139
+ . getElementById ( mapTaskKeyToElementId [ task . key ] )
140
+ . removeAttribute ( "hidden" ) ;
24
141
}
0 commit comments