Side menu in Ionic - showing different links depending on whether the user is authenticated or not

I am working on an Ionic app with a side menu that is used for multiple pages. I want to customize the links in the side menu depending on whether the user is authenticated or not. This is how I have set up my routes:

module.export = angular.module('coop').config(function($stateProvider, $urlRouterProvider) {
  $stateProvider

  //side menu
  .state('main', {
    url: '/main',
    abstract: true,
    templateUrl: 'templates/main.html',
  })

 .state('main.public', {
    url: '/public',
    views: {
      'content': {
        templateUrl: 'templates/public.html',
        controller: 'PublicController'
      }
    },
    authenticate: false
  })

  .state('main.articles', {
    url: '/articles',
    views: {
      'content': {
        templateUrl: 'templates/articles.html',
        controller: 'ArticlesController'
      }
    },
    authenticate: true
  }) 

The links in the side menu should change based on the authenticate property of each state. How can I implement this logic in the side menu?

<ion-side-menus>
  <ion-side-menu side="left" class="side-menu" scroll="false">
    <ul class="menu">
      <div class="side-menu-header">
      </div>
      <div class="menu-main">
        <li>
          <a ng-if="authenticated" menu-close ui-sref="main.profile">My Profile</a>
          <a ng-if="!authenticated" menu-close ui-sref="main.login">Log In</a>
        </li>
      </div>
      <div class="menu-last">
        <li>
          <a ng-if="authenticated" menu-close ui-sref="main.logout">Log Out</a>
        </li>
      </div>
    </ul>
  </ion-side-menu>
  <ion-side-menu-content>
    <ion-nav-view name="content"></ion-nav-view>
  </ion-side-menu-content>
</ion-side-menus>

Update

If anyone else faces this issue, I managed to solve it by setting a rootscope variable on $stateChangeStart event in the app.js:

// Check for login status when changing page URL
  $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
    var currentRoute = toState.name;
    $rootScope.authenticated = false;

    if ($auth.isAuthenticated()) {
      $rootScope.authenticated = true;
    }

Answer №1

Congratulations on successfully completing the task. To implement authentication in the controller, simply add

$scope.authenticated = $state.authenticate

Alternatively, you could create a global variable globals.authenticated within a service named globals, which can be updated on login/logout events. This variable can then be utilized in the side menu using ng-if="globals.authenticated"

Another approach is to store this information in either local storage or SQLite for future reference.

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

How can I modify the border color within a div element?

My journey into web development is just beginning, and I've grasped the basics of Java, JSP, HTML, JS, CSS, and JQ. However, I've hit a roadblock while attempting to change the border color of a div upon mouse hover. Despite my efforts, I haven&a ...

Utilizing Firebase Cloud Firestore: A guide to programmatically handling indexes

Transitioning from Firebase Realtime Database to Cloud Firestore has presented some challenges in implementing "complex" queries across collections. Despite this, I am determined to make it work. My current setup involves using Express JS and Firebase Adm ...

Every time I employ window.location, the Iframe becomes nested

I am experiencing an issue with my HTML structure: <html> <body> This is the container of the iframe <iframe src="/foo.html"> </iframe> </body> </html> (/foo.html) <html> <script type="text/javascript"> $( ...

Selecting multiple categories using jQuery

HTML: <ul id="selector"> <li><a href="#" class="group1" rel="group1">group 1</a></li> <li><a href="#" class="group2" rel="group2">group 2</a></li> <li><a href="#" class="group3" re ...

The v-autocomplete feature in vuetify doesn't allow for editing the text input after an option has been selected

Link to code on CodePen: CodePen Link When you visit the provided page and enter "joe" into the search box, choose one of the two Joes that appear. Try to then select text in the search box or use backspace to delete only the last character. You will no ...

What is the best method for disseminating data to multiple stores with just a single action in the React flux architecture?

Is there a way to efficiently update multiple stores with data in one action? Imagine receiving post data from a server as a user action. Below is a simple pseudo code for this action: class UserActions { getPosts() { asyncFetch(apiEndPoint, ...

Is it possible to encase <v-img> within an anchor element?

I am currently using Vuetify 1.5 and have included a couple of <v-avatars></v-avatars> elements in which there is a nested <v-img></v-img>. I attempted to enclose the img tags within an a tag but encountered an issue wherein the ima ...

Combine React 17 with webpack 5 and babel-plugin-react-css-modules, enabling the use of CSS modules with stylename functionality

I am in the process of setting up a new React application using the latest technologies available, including React 17, Webpack 5, and other essential modules. My goal is to implement CSS modules utilizing the styleName concept with the help of babel-plugi ...

Unable to send multiple onChange AJAX requests

Why do I keep getting the error Notice: Undefined index: provinsi when trying to retrieve values from tahun and provinsi using this code? If I remove provinsi from the code and only use tahun, I am able to successfully fetch data from my database. func ...

What exactly does a 'hoisted manifest' mean when it comes to using Yarn?

Encountering an issue while attempting to install a package using yarn, I am receiving the error message: expected hoisted manifest for \"myPackage#@material-ui/core#react-dom\" However, the concept of a 'hoisted manifest' is not entir ...

When using ng-init, the select will not automatically set to a default value

I'm facing an issue with setting the default value for a drop-down using ng-options. Even though I am able to get the default value in the response, it is not getting set in the drop-down. In JavaScript: $scope.personalDetails = [ { ...

Tips for inserting an element with a background color into an email using variables

I have encountered an issue while trying to send an email that includes an element from my component. The element is passed from Angular to C# and then sent to the customer. Here is the element: https://i.sstatic.net/2ky1b.png Everything looks good in ...

A guide on accessing the value of ng-model in an AngularJS controller

I am facing an issue with a simple view that contains an input text field where the user enters their name. The controller should retrieve this value and assign it to the employeeDescription variable. However, the ng-model value from the input field is not ...

Incorporating the unshift method in JavaScript: A Step-by-

I'm looking to create a new function with the following requirements: Function add(arr,...newVal){ } array = [1,2,3]; add(array,0) console.log(array); //I want this to output [0,1,2,3] I tried creating the function similar to push like this: ...

Having trouble fetching data using $http and promises in AngularJS

I'm having trouble connecting to my RESTful API within my AngularJS application. Despite my efforts, I'm not seeing any data being displayed on the screen. It seems like I might be misunderstanding the usage of $http with promises. Any suggestio ...

Change the color of the Material UI InputLabel

Seeking assistance with my input label: <InputLabel>NAME</InputLabel> The text is currently displaying in white, which makes it difficult to read against the white background. Can someone provide guidance on how I can change the color to blac ...

Error: Namespace declaration does not have a type annotation - TypeScript/Babel

After creating my app using the command npx create-react-app --typescript, I encountered an issue with generated code and namespaces causing Babel to throw an error. Here are the steps I took to try and resolve the issue: I ejected the project Updated b ...

When toggling the menu in bootstrap, the functionality of <a href=""> links may be disrupted

My attempt to integrate a toggle function into each link in the menu to automatically close the menu on mobile after selecting a link has hit a snag. With the solution of adding data-toggle="collapse" data-target="#navbarSupportedContent" to the <a href ...

Effortlessly apply CSS styles to multiple cached jQuery variables without redundancy

Hey there, I've got this piece of code written in Javascript for a classic ASP page. Since there are no CSS classes defined, everything is handled within the Javascript itself. var $a= $('#a'), $v= $('#v'), $s= $('#s'), ...

What is the best way to keep track of the most recent 100 items?

In Angular, I want to store the last 100 items to display. Currently, I am using an array and inserting items with 'array.push'. If this method is not effective for this scenario, what alternative approach can I take? Here is a snippet of the co ...