Is it possible to implement Ruby's authentication in a JavaScript application?

Currently, I have an application running on abc.website.com that utilizes devise for user authentication in Ruby on Rails. Now, I need to set up a react app on def.website.com which should only function if the user is logged in on abc.website.com (which also uses devise).

In order to achieve this, we will need to include a link in the navbar of abc.website.com that directs users to def.website.com.

Do you have any recommendations on how to best accomplish this task?

While I am new to Ruby, I am proficient in Javascript.

Thank you in advance for your assistance. Your help is greatly appreciated.

Answer №1

If you're looking for a solution, check out this helpful guide on implementing Token Based Authentication. Another great option is using JWT.

Answer №2

The Rails application is capable of storing a cookie, such as the Rails session cookie, in the client's cookie store. This cookie is sent to the Rails app with each request from the React app, allowing you to authenticate the user by examining the contents of this cookie.

This method requires minimal adjustments (similar to what would be needed for JWT authentication, which is also a valid approach).

One benefit of this approach is that very little data needs to be stored on the client side. Only an ID is necessary for the Rails backend to identify the user and access all required information. However, if you prefer to store all data on the client side, that is also possible.

In terms of using Rails session versus JWT, there is no clear advantage one way or the other. The key is to provide the client with the necessary information, which can then be passed back to the backend as needed.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I would like the dropdown to revert back to its original selection when the ajax result is retrieved for

When the type and category are changed, the department should also be updated. Initially, it works when changing the category and type once. However, if I try to change them again without refreshing the page, it does not work properly. I prefer not to r ...

Guide on integrating an HTML and CSS register form in Django

I have successfully created a responsive register and login using HTML and CSS. Instead of utilizing the standard register form and login provided by Django upon configuration, I want to apply my own custom template. To summarize, while I am familiar with ...

Verify the occurrence of an element within an array inside of another array

Here is the scenario: const arr1 = [{id: 1},{id: 2}] const arr2 = [{id: 1},{id: 4},{id: 3}] I need to determine if elements in arr2 are present in arr1 or vice versa. This comparison needs to be done for each element in the array. The expected output sho ...

What steps should I follow to enable my bot to generate or duplicate a new voice channel?

I needed my bot to either create a new voice server or clone an existing one. The variable "voic" contains the ID of the voice channel. voic.voiceChannel.clone(undefined, true, false, 'Needed a clone') // example from ...

Having issues with the closest method in DojoQuery on Internet Explorer

Looking for guidance on utilizing the closest method in Dojo to identify the first parent element of an element that needs to be displayed/hidden based on a selected value in a filtering select. It functions correctly in Firefox, but encounters an issue in ...

What is the process for sending a request to an external site from a content_script in a web browser extension

I've been working on developing a Chrome extension with a feature that will display information from an external server, such as today's weather, in a popup within Gmail. Methods attempted: Initially, I tried including the necessary JavaScript ...

React-Native error message: Promise rejection unhandled - React child cannot be an object

I am experiencing an issue with rendering a list from an array of objects that have the structure provided below. While I have successfully handled the promise and set the data to the state, I keep encountering an error specifically with this array. The da ...

Having trouble getting JavaScript to select a stylesheet based on time?

I am attempting to implement two different styles based on the time of day. Here is the code I am using to select the CSS file depending on the current time: <script type="text/javascript"> function setTimedStylesheet() { var currentTim ...

What is the issue with undefined params in Next.js?

I have come across an issue with the function in app/api/hello/[slug]/route.ts When I try to log the output, it keeps showing as undefined. Why is this happening? The code snippet from app/api/hello/[slug]/route.ts is shown below: export async function G ...

Efficient method for deleting a specific item from a JSON object in React.js

Within the REACT JS framework, I am managing a JSON object in the state component "myrecords". The items within this object are organized by their respective Company Names and have the following structure: { "Master Automotives": [ { ...

What is the reason behind the capybara tests passing individually but failing when executed together?

When running each of the tests individually: rspec spec/features/visit_home_page_root_spec.rb:15 rspec spec/features/visit_home_page_root_spec.rb:22 they pass without any issues. However, when running both tests together: rspec spec/features/visit_home ...

How to extract the value of a key from JSON using JavaScript

Need help with an API call to retrieve a list of subcategories? Here's an example of the JSON format: { "description": "Flower", "name": "Flower", "parent_id": "1" }, { "description": "Moon", "n ...

Utilize a function on specific attribute values

I have a function in JavaScript that is designed to convert all relative URLs into absolute URLs. function rel2Abs(_relPath, _base); //_relPath represents the relative path //_base indicates the base URL Now, my objective is to implement this function on ...

Each occurrence of a variable update will result in the storing of its value within an

I'm struggling to include a variable in an array without it changing. Every time I try to push the variable to the array, it seems to alter. i = 10; function addToArray() { let b = []; b.push(i); console.log(b); } addToArray(); The variable n ...

Emptying the trumbowyg editor within an Angular environment

I am currently experiencing issues with the trumbowyg editor in a project I'm working on. I am able to update the editor by changing its model but cannot seem to clear its content using $scope.editorModel = '';. For a more detailed explanati ...

Determining if a variable has been defined in JavaScript

In my JavaScript code, I declare global variables named with the prefix "sld_label" where "label" can be any string. These variables are initialized as objects that contain a function called setval. Now, I have a function that receives a label in a string ...

Guide to importing a specific Vuetify component

Whenever I attempt to import specific Vuetify components, the base style of the component is not imported along with it. Below is the code of my parent component where I am importing components from Vuetify: Webpack: 5 Vuetify Version: 2.6.6 VueJS Versio ...

Collapse the Bootstrap Menu upon selecting a Link

I am working on a side menu with an offcanvas display feature. The current functionality is such that when the button is clicked, the canvas pushes left and opens the menu. However, I want the menu to collapse and close automatically when a link is selecte ...

Using the getElementById method in Django allows you to access specific elements in

I've been attempting to create a code for editing posts using modals that were previously generated, but I keep encountering an error stating "Identifier or string literal or numeric literal expected" and "Statement expected." Here is the snippet of m ...

Preserving the original "this" context while binding a callback for a React child

class Parent extends React.Component { constructor(props) { super(props) this.handleChildOnClick = this.handleChildOnClick.bind(this) } handleChildOnClick() { /* Here, I want to perform an action that involves both Child and Parent. ...