Skip to content

Adds fix to validate if session is still valid on chat. #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def require_user
if current_user
cookies.encrypted[:user_id] = current_user&.id
else
redirect_to root_path
redirect_to root_path, status: :unauthorized
end
end
end
27 changes: 24 additions & 3 deletions app/javascript/controllers/chat_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default class extends ApplicationController {
console.log('Chat controller connected', this.element)
this.element.focus()
var submitButton = document.getElementById('prompt_submit')
if(submitButton) {
if (submitButton) {
submitButton.addEventListener('click', this.prompt.bind(this))
}
}
Expand All @@ -17,7 +17,7 @@ export default class extends ApplicationController {
}

keydown(event) {
if(event.keyCode === 13) {
if (event.keyCode === 13) {
if (event.metaKey || (!this.element.dataset.grow && !event.shiftKey)) {
event.preventDefault();
this.prompt(event)
Expand All @@ -29,7 +29,28 @@ export default class extends ApplicationController {
if (this.element.value.length === 0) {
return;
}
this.stimulate('ChatReflex#prompt')

const xhr = new XMLHttpRequest();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When would this fire off?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a better solution is probably to put the check in the ApplicationReflex, similar to how it works in ApplicationController

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be executed after session verification its done.

Yes let me check how to do that

xhr.open("GET", "/chats");
xhr.setRequestHeader("Content-Type", "application/json");

const token = document.head.querySelector(
'meta[name="csrf-token"]'
).content;

xhr.setRequestHeader("X-CSRF-Token", token);

xhr.onreadystatechange = () => {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
this.stimulate('ChatReflex#prompt');
} else if (xhr.status === 401) {
window.location.href = "/";
}
}
};

xhr.send();
}

beforePrompt(element, reflex, noop, reflexId) {
Expand Down