Skip to content

Commit

Permalink
logout when chatting with invalid token
Browse files Browse the repository at this point in the history
  • Loading branch information
yeguangsuixing committed Jun 23, 2023
1 parent a9fe88e commit 0e123cc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
8 changes: 6 additions & 2 deletions app/components/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,9 @@ export function Chat() {
if (userInput.trim() === "") return;
setIsLoading(true);
chatStore
.onUserInput(userInput, websiteConfigStore, authStore)
.onUserInput(userInput, websiteConfigStore, authStore, () =>
navigate(Path.Login),
)
.then(() => setIsLoading(false));
localStorage.setItem(LAST_INPUT_KEY, userInput);
setUserInput("");
Expand Down Expand Up @@ -609,7 +611,9 @@ export function Chat() {
const content = session.messages[userIndex].content;
deleteMessage(userIndex);
chatStore
.onUserInput(content, websiteConfigStore, authStore)
.onUserInput(content, websiteConfigStore, authStore, () =>
navigate(Path.Login),
)
.then(() => setIsLoading(false));
inputRef.current?.focus();
};
Expand Down
13 changes: 12 additions & 1 deletion app/store/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ interface ChatStore {
content: string,
websiteConfigStore: WebsiteConfigStore,
authStore: AuthStore,
navigateToLogin: () => void,
) => Promise<void>;
summarizeSession: () => void;
updateStat: (message: ChatMessage) => void;
Expand Down Expand Up @@ -238,7 +239,12 @@ export const useChatStore = create<ChatStore>()(
get().summarizeSession();
},

async onUserInput(content, websiteConfigStore, authStore) {
async onUserInput(
content,
websiteConfigStore,
authStore,
navigateToLogin,
) {
const session = get().currentSession();
const modelConfig = session.mask.modelConfig;
const sensitiveWordsTip = websiteConfigStore.sensitiveWordsTip;
Expand Down Expand Up @@ -298,6 +304,7 @@ export const useChatStore = create<ChatStore>()(
},
onFinish(message) {
botMessage.streaming = false;
let logout = false;
if (message) {
try {
let jsonContent = JSON.parse(message);
Expand All @@ -317,6 +324,7 @@ export const useChatStore = create<ChatStore>()(
jsonContent &&
(jsonContent.code === 10001 || jsonContent.code === 10002)
) {
logout = true;
authStore.removeToken();
message = Locale.Error.Unauthorized;
} else {
Expand All @@ -333,6 +341,9 @@ export const useChatStore = create<ChatStore>()(
botMessage.id ?? messageIndex,
);
set(() => ({}));
if (logout) {
navigateToLogin();
}
},
onError(error) {
const isAborted = error.message.includes("aborted");
Expand Down

0 comments on commit 0e123cc

Please sign in to comment.