Angularjs authentication cookie successfully functions on the second attempt

    login: function(user, success, error) {
        var s = new session(user);
        s.$login({}, function (u, putResponseHeaders) {
            if ($cookies.user) {
                console.log('cookie set' + $cookies.user);
                user = JSON.parse($cookies.user);
            }
            else
                console.log('no cookie set after login');

            console.log(u);

            $rootScope.user = user;
            console.log($rootScope.user.role);
            success(user);
        }, function () {
            error();
        });
    },

Upon clicking the login button, the server assigns an authentication cookie. The aforementioned function is responsible for handling this cookie and calling the success(user) function based on its presence. An issue arises where upon the first click of the login button, 'no cookie set after login' is displayed and the cookie is not detected. However, on the second click, authorization takes place successfully. While Google Chrome recognizes the cookie being set during the initial login attempt, the AngularJS code fails to acknowledge its existence. This inconsistency raises the question of why the AngularJS code is unable to detect the cookie when Chrome can.

Answer №1

Due to the fact that the cookie has not been established and is not accessible to Javascript.

Your method of operation seems unconventional. Why do you place importance on the cookie? Wouldn't it be more logical for the login function to return the user object in JSON format for direct use? The cookie should simply serve as a means to transmit session data with future requests...

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

Error Message for Sequelize Validation Fails to Appear as Expected

I am trying to implement Sequelize model validation in order to validate a form field. When the book or author fields are left empty, I want this message to be displayed: Oops! An error occurred. "Title" is required. "Author" is required. The issue I&ap ...

Tips for matching variable content with button identification to display content within a variable for ThreeJS?

I'm currently working on a project using Three.js where I need to load multiple 3D models and store them in an array. Each model has a corresponding button with a unique ID that when clicked, should add that specific model to the scene and remove any ...

Tips for retrieving the newly created instance in Sequelize

I have been experimenting with sequelize for my new project, but I am facing difficulties in returning the created instance. Any help would be appreciated. Below is my create method : exports.create = (req, res) => { let user = { "user ...

"Adding content to the DOM using AJAX, the data is showing up as plain text rather than formatted

As part of my project, I am working on incorporating data retrieved through an AJAX request into the DOM to be able to manipulate it further. However, I encountered an issue where the data displayed as a string instead of HTML when appended to the DOM. Bel ...

Personalize your Datatable export options with Jquery

I am working with a datatable that contains columns with data in the format 'XXXX unit'. For my export, I need to remove the 'unit' part of the data. What specific rule should I implement for this task? exportOptions: { columns: ...

Cease the form submission process using Ajax when the input field is blank

I have implemented an ajax code that halts the form submission process if any input value is empty and displays a popup alert. However, I am facing an issue where the process continues even after closing the alert popup. How can I ensure that the process ...

Determine the quantity of specific key/value pairs in a dynamic JSON object

I have a data structure in the form of a JSON object that contains key-value pairs for clients. The list of clients varies daily based on their transactions made each day of the month. Therefore, my data only includes transaction information by clients. Be ...

Guide to dynamically setting a value in an input field using JavaScript's document.querySelector

My goal is to input a value using Javascript. Check out my project link! Click on "add to cart" and then proceed to checkout to reach the checkout page. I am trying to automatically add a Zipcode value to the checkout page. I attempted this method but it ...

What could be causing the <a> taghelper route to stop working after being used by a form post?

This ASP.Net Core MVC project includes a menu link in the layout file that directs to the Employees controller and Index action: <li><a asp-controller="Employees" asp-action="Index">Employees</a></li> The corresponding MVC control ...

Encountering an error message saying "assignment to undeclared variable"

I'm attempting to incorporate a react icon picker from material-ui-icon-picker However, I keep encountering an error stating "assignment to undeclared variable showPickedIcon" edit( { attributes, className, focus, setAttributes, setFocus, setState ...

Adjust the size of an image and move its position both vertically and horizontally using Javascript

I am currently working on transforming an image both vertically and horizontally, as well as scaling it using JavaScript. While I have managed to resize the image successfully, it doesn't quite look how I want it to. I am aiming for a similar effect a ...

Creating a dynamic form where input fields and their values update based on user input in jQuery

In my form, I have an input field where users will enter an ISBN number. Based on the input number, I need to populate two other input fields: one for book title and one for author name. I am currently calling a JavaScript function on the onblur event of ...

Inquiry regarding the setTimeout function requiring an answer

I have a question about how setTimeout works. Does it wait for the previous code to finish executing before moving on to execute something else after a set time, or does it simply wait for a specific amount of time and then continue with the rest of the co ...

Implement tooltip functionality in ssr chart using echarts

A chart is generated using echarts on the server-side: getChart.ts const chart = echarts.init(null, null, { renderer: 'svg', ssr: true, width: 400, height: 300 }); chart.setOption({ xAxis: { data: timeData }, ...

Is it possible to trigger a submit button to perform a POST request and an AJAX call simultaneously

I have a form that sends a POST request to show_aht2.php, but I also need it to trigger an AJAX call as shown in the code below. How can I achieve this without redirecting the user to another page? I want the user to remain on map.php. Thank you in advanc ...

Is there a method to leverage ng-bind for better functionality?

I have created a unique directive that adjusts the size of text in a box to ensure it stays on one line. It works perfectly when the text is received from the server, but I am facing an issue with dynamically generated text from my Angular controller. The ...

partial download between servers

I have been attempting to transfer/copy a large file from a remote server to my server in segmented parts or chunks. My initial approach involved utilizing a script I found here: . After making some modifications, I integrated a form into the script and e ...

Scroll down 1 page using Javascript and Jquery

Hey there! I'm looking to create a website where a small scroll on the mouse wheel will take the site to the next anchor. It's kind of hard to explain, but one good example is the Tesla Model 3 website on the US site here. Does anyone have any t ...

Warning message in React about missing floating prop in components

application.js (Webpack Entry Point) import React from 'react'; import ReactDOM from 'react-dom'; import App from './App.jsx'; document.addEventListener('DOMContentLoaded', () => { ReactDOM.render(<App /> ...

Is it acceptable to assign unique names to individual arrays within a multidimensional array?

I was curious about the possibility of naming arrays within a multi-array, similar to how it can be done with individual arrays. For example: var cars = new Array(); cars[0] = "Saab"; cars[1] = "Volvo"; cars[2] = "BMW"; I was wondering if there was a way ...