Skip to content

Commit 8513353

Browse files
fix reauthentication + modal closing with clerk
1 parent 2abf02e commit 8513353

File tree

4 files changed

+66
-36
lines changed

4 files changed

+66
-36
lines changed

templates/vue/vue3-ai-chatbot-widget/src/App.vue

+4-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
isMaximizedByDefault: Boolean,
1717
previewText: String,
1818
footerDisclaimer: String,
19-
authMode: String
19+
authMode: String,
20+
clerkPublicKey: String
2021
})
2122
2223
const chatWidget = reactive({
@@ -29,7 +30,8 @@
2930
isMaximizedChat: props.isMaximizedByDefault,
3031
previewText: props.previewText,
3132
footerDisclaimer: props.footerDisclaimer,
32-
authMode: props.authMode
33+
authMode: props.authMode,
34+
clerkPublicKey: props.clerkPublicKey
3335
})
3436
3537
provide('chatWidget', chatWidget)

templates/vue/vue3-ai-chatbot-widget/src/components/layout-chat/index.vue

+22-7
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@
4545
leave-from-class="opacity-100"
4646
leave-to-class="opacity-0"
4747
>
48-
<div v-if="showAuthOverlay && chatWidget.authMode === 'basic'"
48+
<div v-if="showAuthOverlay"
4949
class="fixed inset-0 z-[60] bg-black/50 flex items-center justify-center">
50-
<BasicAuthWindow :onSubmit="handleAuthSubmit" />
50+
<BasicAuthWindow :onSubmit="handleBasicAuthSubmit" />
5151
</div>
5252
</Transition>
5353
</template>
@@ -68,16 +68,18 @@
6868
defineOptions({ name: 'layout-chat' })
6969
7070
const { messages, sendMessage, cancelMessage, resetChat, isProcessingRequest, sendFeedback, copilot } =
71-
useAzionCopilot({ server: chatWidget.serverUrl })
71+
useAzionCopilot({ server: chatWidget.serverUrl, authMode: chatWidget.authMode })
7272
73-
copilot.on(CONSTANTS.EVENTS.AUTH_REQUIRED, () => {
74-
console.log(chatWidget)
73+
copilot.on(CONSTANTS.EVENTS.AUTH_REQUIRED, async () => {
7574
if (chatWidget.authMode === 'basic') {
7675
showAuthOverlay.value = true
76+
} else if (chatWidget.authMode === 'clerk') {
77+
await handleClerkAuth()
7778
}
7879
})
7980
80-
const handleAuthSubmit = async (password) => {
81+
const handleBasicAuthSubmit = async (password) => {
82+
8183
const authService = new AuthService({
8284
authMode: 'basic',
8385
copilotBackend: chatWidget.serverUrl.url
@@ -90,6 +92,19 @@
9092
}
9193
}
9294
95+
const handleClerkAuth = async () => {
96+
const authService = new AuthService({
97+
authMode: 'clerk',
98+
copilotBackend: chatWidget.serverUrl.url,
99+
clerkPublicKey: chatWidget.clerkPublicKey
100+
})
101+
102+
const user = await authService.clerkSignIn()
103+
if (user) {
104+
showAuthOverlay.value = false
105+
}
106+
}
107+
93108
const closeChat = () => {
94109
chatWidget.isClosing = true
95110
}
@@ -108,4 +123,4 @@
108123
: 'w-[500px] h-[700px] bottom-[80px] rounded-lg shadow-lg right-5',
109124
chatWidget.isClosing ? 'translate-x-full opacity-0 scale-95' : ''
110125
])
111-
</script>
126+
</script>

templates/vue/vue3-ai-chatbot-widget/src/core/azion-copilot.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export class AzionCopilot {
77
this.sessionId = crypto.randomUUID()
88
this.events = new EventEmitter()
99
this.authToken = null
10+
this.authMode = config.authMode
1011

1112
this.serverConfig = {
1213
...CONSTANTS.SERVER.DEFAULT,
@@ -168,13 +169,15 @@ export class AzionCopilot {
168169
'Content-Type': 'application/json',
169170
}
170171

171-
if (this.authToken) {
172+
const shouldIncludeCredentials = this.authMode === 'clerk' || this.authMode === 'basic'
173+
174+
if (this.authToken && this.authMode === 'basic') {
172175
headers['Authorization'] = `Bearer ${this.authToken}`
173176
}
174177

175178
const response = await fetch(`${this.serverConfig.url}${this.serverConfig.conversation}`, {
176179
method: 'POST',
177-
credentials: this.authToken ? 'include' : 'omit',
180+
credentials: shouldIncludeCredentials ? 'include' : 'omit',
178181
headers,
179182
body: JSON.stringify({
180183
messages: messageQueue,
@@ -276,13 +279,15 @@ export class AzionCopilot {
276279
'Content-Type': 'application/json',
277280
}
278281

279-
if (this.authToken) {
282+
const shouldIncludeCredentials = this.authMode === 'clerk' || this.authMode === 'basic'
283+
284+
if (this.authToken && this.authMode === 'basic') {
280285
headers['Authorization'] = `Bearer ${this.authToken}`
281286
}
282287

283288
const response = await fetch(`${this.serverConfig.url}${this.serverConfig.feedback}`, {
284289
method: 'POST',
285-
credentials: this.authToken ? 'include' : 'omit',
290+
credentials: shouldIncludeCredentials ? 'include' : 'omit',
286291
headers,
287292
body: JSON.stringify(feedbackData)
288293
})
@@ -365,4 +370,4 @@ export class AzionCopilot {
365370
setAuthToken(token) {
366371
this.authToken = token
367372
}
368-
}
373+
}

templates/vue/vue3-ai-chatbot-widget/src/services/auth.js

+30-22
Original file line numberDiff line numberDiff line change
@@ -28,34 +28,42 @@ export class AuthService {
2828
}
2929

3030
async clerkSignIn() {
31-
3231
const clerk = new Clerk(this.clerkPublicKey)
3332
await clerk.load()
3433

3534
if (!clerk.user) {
36-
clerk.openSignIn({
37-
appearance: {
38-
socialButtonsVariant: "iconButton",
39-
elements: {
40-
card: {
41-
boxShadow: 'none',
42-
},
43-
modalCloseButton: {
44-
display: 'none'
45-
},
46-
footerAction: {
47-
display: 'none'
48-
},
49-
}
50-
},
51-
})
52-
53-
await new Promise(resolve => {
54-
clerk.addListener(({ user }) => {
55-
if (user) resolve()
35+
return new Promise((resolve) => {
36+
const openSignIn = () => clerk.openSignIn()
37+
openSignIn()
38+
39+
// Create mutation observer to watch for modal removal
40+
const observer = new MutationObserver((mutations) => {
41+
for (const mutation of mutations) {
42+
if (mutation.removedNodes.length > 0) {
43+
const modalRemoved = Array.from(mutation.removedNodes)
44+
.some(node => node.classList?.contains('cl-modalBackdrop'))
45+
46+
if (modalRemoved && !clerk.user) {
47+
setTimeout(() => {openSignIn()}, 1)
48+
}
49+
}
50+
}
51+
})
52+
53+
observer.observe(document.body, {
54+
childList: true,
55+
subtree: true
56+
})
57+
58+
clerk.addListener(({ user }) => {
59+
if (user) {
60+
observer.disconnect()
61+
resolve(user)
62+
}
63+
})
5664
})
57-
})
5865
}
66+
5967
return clerk.user
6068
}
6169

0 commit comments

Comments
 (0)