Skip to content

Commit

Permalink
fix: prevent unverified emails to access ai chat (#1973)
Browse files Browse the repository at this point in the history
* fix: prevent unverified emails to access ai chat

* chore: changeset
  • Loading branch information
gabriele-ct authored Apr 17, 2024
1 parent f9b9eb8 commit 394a5da
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/hip-boats-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@commercetools-docs/gatsby-theme-docs': patch
---

Fix unverified users to access AI assistant
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { CHAT_ROLE_ASSISTANT, CHAT_ROLE_USER } from './chat.const';
import ChatMessages from './chat-messages';
import {
isWaitingChunk,
isNotValidatedUser,
isUserEmailVerified,
loadLocalChatState,
setLocalStorageChatLocked,
setLocalStorageChatMode,
Expand Down Expand Up @@ -188,7 +188,7 @@ const ChatModal = () => {
setInitLoading(false);
}
};
if (chatConfig && isAuthenticated && !isNotValidatedUser(user)) {
if (chatConfig && isAuthenticated && isUserEmailVerified(user)) {
fetchChatInit();
}
}, [user, isAuthenticated, chatInit, chatConfig]);
Expand Down Expand Up @@ -471,10 +471,10 @@ const ChatModal = () => {
if (!isAuthenticated) {
setAssistantState(ASSISTANT_STATE_LOGGED_OUT);
}
if (isAuthenticated && isNotValidatedUser(user)) {
if (isAuthenticated && !isUserEmailVerified(user)) {
setAssistantState(ASSISTANT_STATE_NOT_VERIFIED);
}
if (isAuthenticated && !isNotValidatedUser(user)) {
if (isAuthenticated && isUserEmailVerified(user)) {
setAssistantState(ASSISTANT_STATE_OPEN);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ const isAllowedEmailDomain = (email, domainList) => {
return allowedEmailDomainRegExp.test(email);
};

export const isNotValidatedUser = (user) => {
return user && !user.email_verified && isAllowedEmailDomain(user.email);
// we consider verified users only if they have an actually verified email
// or if they belong to the allowed email domains
export const isUserEmailVerified = (user) => {
return isAllowedEmailDomain(user.email) || (user && user.email_verified);
};

/**
Expand Down

0 comments on commit 394a5da

Please sign in to comment.