Displaying a sign out option in Angular after a user has successfully signed

Experiencing an issue with Angular authentication using ng-token-auth. The Sign Out button is not displaying after successful Sign Up, although it works fine for Sign In.

I have checked my code and the isLoggedIn() and signOut() functions are working as expected without any errors.

Could someone please point out where I might be going wrong? Thank you!

Navbar Controller

function($rootScope, $scope, $location, $auth, currentUser) {

  $scope.isLoggedIn = function() {
    return ($scope.user.id) ? true : false;
  }

  $scope.signOut = function() {
    currentUser.signOut();
  };

Auth Controller

  $scope.submitRegistration = function() {
    $auth.submitRegistration($scope.registrationForm)
      .then(function(res) {
        currentUser.set(res.data.data);
        $scope.close();
      })
      .catch(function(res) {
        $scope.errors = res.data.errors.full_messages.join(', ');
      })
  };

  $scope.submitLogin = function() {
    $auth.submitLogin($scope.loginForm)
      .then(function(resp) {
        currentUser.set(resp);
        $scope.close();
      })
      .catch(function(resp) {
         $scope.errors = "Email or Password invalid...";
      });
  };

Navbar HTML

    <li ng-controller="AuthModalCtrl" ng-if='!isLoggedIn()'>
      <div ng-click="openModal(signin)">Sign In</div>
    </li>
    <li ng-controller="AuthModalCtrl" ng-if='!isLoggedIn()'>
        <div class="bubble-btn sign-up" ng-click="openModal('register')">Sign Up</div>
    </li>
    <li ng-controller="NavCtrl" ng-show='isLoggedIn()'>
       <div ng-click="signOut()">Sign Out</div>
    </li>

Answer №1

It seems like users are able to successfully log in, but you wish to automatically log them in after they complete the registration process.

As per the guidance provided in the ng-token-auth documentation, the $auth.submitRegistration function does not automatically log in the user; it simply registers them. To achieve automatic login upon registration, you will need to explicitly handle the login process within the success callback of the submitRegistration function.

The success callback returns a response containing the registration parameters sent by the user. To implement automatic login after registration, consider modifying your code as follows:

$scope.submitRegistration = function() {
    $auth.submitRegistration($scope.registrationForm)
      .then(function(res) {

        // Use the response (res) to create your login object

        // You have the option to either call $auth.submitLogin with appropriate parameters here and specify another callback,
        // or utilize your existing $scope.submitLogin functionality
        $auth.submitLogin( ...insert your login params here... )

        $scope.close();
      })
      .catch(function(res) {
        $scope.errors = res.data.errors.full_messages.join(', ');
      })
  };

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

"Troubleshoot: Inadequate performance of table search in node.js due

Embarking on a journey of learning here excites me. I have an insatiable appetite for new knowledge! Grateful in advance for any assistance! Presenting a concise Node.js JavaScript code snippet of 30 lines for a standard table search process. The "table" ...

Guidelines for triggering the update of radio buttons within a Webix datatable

Is it possible to automatically update the Webix datatable when radio buttons are selected? Here is an example of a datatable: var list = [{ id:1, user:"", mail:"", rad:'' }, { id:2, user:"", mail:"", rad:'' }, { id:3, user:" ...

Encountering a problem when making a HTTPS GET request to a REST API using

I am currently running an Angular application that utilizes an external API to fetch country ISOs. However, I am encountering an error with the API since it uses HTTPS. Interestingly, when I implement a proxy in my Angular local environment by mapping /is ...

Tips for building a diverse array of data types and effectively utilizing them based on their specific type in Typescript

Trying to store both custom types, Graphic and Asset, in the same array is proving to be a challenge. The goal is to access them and retain their individual type information. const trail: Array<Graphic | Asset> = []; for (let index = 0; index < t ...

Finding it difficult to grasp the concept of importing node packages

I am completely new to the world of web development and hoping for some guidance. I'm currently attempting to utilize a JavaScript library called euclid.ts. The instructions on its page are straightforward: Instructions to import euclid.ts Here is w ...

Javascript background image rotation causes a sudden jump to the top of the webpage

I am struggling with a JavaScript issue that I can't seem to figure out. I'm very new to this so any help would be greatly appreciated. I found some code that loads random images into a div element and made some modifications to add a bit of rand ...

Concentrate on the text input once an option is chosen from the select box within AngularJS

I am attempting to focus on an input type text after selecting an option from a select box. However, after selecting the option, the focus switches back to the select box instead of staying on the input. To see the issue in action, here is a link to a JSF ...

Attempting to generate a jwt results in an undefined value

Currently in the process of mastering Node.js and I am in the midst of transitioning my existing API from Python to Node. My main challenge lies in the creation of a jwt token for authentication purposes with a third-party API. However, it seems I'm e ...

The MomentJS difference calculation is incorporating an additional hour

I am currently working on a project that involves time calculations. const currentCETTime = moment.tz('2020-03-18 15:58:38', 'Europe/Madrid'); const limitCETTime = moment.tz('2020-03-18 18:00:00', 'Europe/Madrid') ...

What is the best method for globally configuring the Angular moment timezone?

I integrated angular moment js into my angular application. I am looking to display the date and time based on the time zone of the user accessing the app. However, I am facing difficulty in applying the time zone globally throughout my application. https ...

What is the most effective method for incorporating access token authentication in a React application?

As a newcomer to React, I am working on implementing authentication using Express.js in my react web application. I have successfully set the token in response cookies on the backend with the HttpOnly flag, but unfortunately, I am unable to read it on the ...

Ways to iterate over all div elements containing a certain class using JavaScript

I am currently working on creating a search functionality for an FAQ page that will filter out results that do not contain the keywords entered by the user. Right now, the search function only highlights the keywords, and I have assigned divs with the clas ...

Utilizing AngularJS to Secure Access with Bearer Token and Web API 2.0

Despite setting the $http.defaults.headers.common.Authorization to null, I am still able to access the [Authorize] section of my Web API 2.0 application. This issue only occurs when trying to fetch data via an initial GET request after starting the applic ...

Tips for creating pill progress bars that overlap each other

I am attempting to design a progress bar in bootstrap that displays projected and actual results, with a unique rounded "pill" shape on the right side. Below is the code snippet showcasing my progress: const data = { actual: 1155, projected: 1 ...

When Utilizing an async Task Method, DbContext is Properly Disposed

Currently, I have set up an async Task method to handle the IAuthenticationFilter interface. After logging in successfully, I can access an API with no issues when it has this attribute. However, if I revisit the API later on, an exception is thrown. publ ...

Revamp State before redirecting using React Router Dom

In my current project, I am facing an issue with redirecting after updating the state successfully. The technologies I'm using include React 16.12.0 and React Router DOM 5.1.2. Here's the scenario: I have a button that, when clicked, should updat ...

The ReferenceArrayInput request is lacking an api-prefix

I have been attempting to utilize the ReferenceArrayInput component from react-admin in order to modify a OneToMany relationship. Although the options for the multi-select input load correctly, the actual selection does not work as expected. Interesting ...

Activate the button once a checkbox within the loop is selected, located outside of the loop

Make the Button clickable only if a checkbox is checked within a loop, with the button placed outside the loop HTML <body ng-controller="MainCtrl"> <div ng-repeat="$item in items"> <label> Amount: <input type="number" ng ...

AJAX, JQuery, JQuery UI, CSS, and custom JavaScript are experiencing loading issues

Can anyone explain why my AJAX requests only work when I remove //code.jquery.com/jquery-1.9.1.js from my site, but then I can't access the JQuery UI datePicker? <head> <link rel="stylesheet" type="text/css" href="CSS/custom.css"> < ...

Mastering the navigation of CSS elements in Protractor-based end-to-end tests by utilizing Xpath

When using Chrome dev tools, I am presented with the following XPath: //*[@id="OvForm"]/basic-Item-fields/div[1]/div/div[3]/detail-label[1]/p/span/strong ...