AngularJS User-Centric Authentication Page

I am in the process of developing an Angular application that will dynamically display data based on user input from the login page. In essence, I require assistance with achieving the following:

  1. Upon entering a username and password, the URL should be constructed with this information and called so that a unique ID (a numeric digit) is generated for each user. This unique ID will be in JSON format as {"Record":[{"Id":12}]}

  2. If a unique ID is returned from the URL, tab.html should be displayed; if null is returned, an error message stating wrong credentials must be shown.

  3. For a successfully logged-in user, a table in tab.html should be displayed based on the unique ID generated from the username and password.

Here is the code snippet I currently have:

login.html:

<form ng-submit=submit()>
            <input type="text" name="username" placeholder="Username" ng-model="person.firstName" required />
            <span class="error" ng-show="mainForm.usernamename.$error.required">required</span>
            <input type="password" name="pswd" placeholder="Password" ng-model="person.pswd" required />
            <span class="error" ng-show="mainForm.pswd.$error.required">required</span>
            <div class="submit">
                <div>
                    <label>
                        <input name="remember" type="checkbox" data-ng-model="remember" data-ng-click="rememberMe()"> Rememeber Me
                    </label>
                </div>
                <input type="submit" value="LOGIN">
            </div>
            <div class="row">
                <div class="col-sm-10"><p><a href="#">Forgot Password?</a></p></div>
            </div>             
        </form>

tab.html:

  <div ng-controller="SampleController">
        <table class="table table-striped table-bordered table-hover dataTables-example">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>ID</th>
                    <th>Qualification</th>         
                </tr>
            </thead>
            <tbody>
                <tr ng-repeat="x in tableContent" >                 
                 <td>{{x.Name}} </td>
                    <td>{{x.DT}}</td>
                    <td>{{x.Qualification}}</td>
                  </tr>
            </tbody>
        </table>      
</div>

app.js:

var wc = angular.module('wc', []);
wc.config(function ($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/login');
$stateProvider
         .state('login', {
             url: '/login',
             templateUrl: 'views/login.html',
             controller: 'LoginCtrl'
         })
.state('tab', {
    url: '/tab1',
    templateUrl: 'views/tab.html'
});
});

wc.controller('LoginCtrl', function ($scope,$http, $location) {
$scope.submit = function () {
     $http.get("URL-PartAame=" + $scope.person.firstName + "&Password=" + $scope.person.pswd)
       .success(function (data) {
//how to get the id from above url and display data based on condition//
           $scope.tableData = data;
           console.log(data)
           $location.path('/tab1');
       })
       .error(function (response, status, headers, config) { });
}
});
wc.controller('SampleController', function ($scope, $http, $modal) {
  $http.get("UrlA-UserId="returnedId)
    .success(function (response) {
        $scope.tableContent = response.Table;
    });
 };

I understand that using a service or factory could solve this issue, but how can a service be invoked alongside the submit() function? If this approach is incorrect, please suggest an alternative method. Thank you in advance!

Answer №1

Utilizing the $state.go method along with the $stateParams service.

wc.controller('LoginCtrl', function ($scope,$http, $location, $state) {
$scope.submit = function () {
     $http.get("URL-PartAame=" + $scope.person.firstName + "&Password=" + $scope.person.pswd)
       .success(function (data) {
//how to retrieve the id from the specified url and display data based on certain conditions//
           $scope.tableData = data;
           console.log(data)

           $state.go('tab', {id: the_necesarry_id});
           //$location.path('/tab1');
       })
       .error(function (response, status, headers, config) { });
}
});


wc.controller('SampleController', function ($scope, $http, $modal, $stateParams) {
  var returnedId = $stateParams.id;
  $http.get("UrlA-UserId="returnedId)
    .success(function (response) {
        $scope.tableContent = response.Table;
    });
 };

Remember to include the id attribute provided by the $stateParams service in the URL state declaration.

.state('tab', {
    url: '/tab1/:id',
    templateUrl: 'views/tab.html'
});

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

Adding padding to navigation items in Bootstrap 5 when the collapsible navbar is expanded

How can I modify the CSS rules for nav items in a Bootstrap 5 collapsible navbar to have proper padding and margins only when they are expanded and the buttons are on the right side? <!DOCTYPE html> <html lang="en"> <head> <meta ...

Creating a dynamic multi-select feature in AngularJS with ng-repeat

I'm relatively new to AngularJS and JavaScript, but I've managed to create a functional multi-select list. The ng-model linked to the dropdown is part of a "user" DTO object, specifically a property that stores an array of "groups" the user can j ...

JavaScript code for styling changes is not functioning as expected

Check out this code snippet: var moving_nav = document.getElementById("moving_nav"); var logo = document.getElementById("logo"); setInterval(function(){ var scrolled = window.pageYOffset || document.documentElement.scrollTop; if (scrolled >= ...

Displaying search results in various Angular components

On my home page (homePageComponent), I have a search feature. When the user clicks on the search button, they are redirected to a different page called the search list page (searchListComponent). Within the searchListComponent, there is another component c ...

Angular: Tailored content and design for individual customers

Currently, I am in the process of creating a web application using Angular that will cater to various clients. Each client has their own unique requirements in terms of functionality and style. However, there are certain functionalities that are common acr ...

Send the object containing a Dictionary<string, int> parameter to the WebMethod

I encountered an error that says: "Cannot convert object of type 'System.String' to type 'System.Collections.Generic.Dictionary2[System.String,System.Int32]'","StackTrace":" at System.Web.Script.Serialization.ObjectConverter.ConvertObje ...

utilize the getStaticProps function within the specified component

I recently started a project using Next.js and TypeScript. I have a main component that is called in the index.js page, where I use the getStaticProps function. However, when I log the prop object returned by getStaticProps in my main component, it shows a ...

Exploring the wonders of ExpressJS session variables

Having transitioned from a PHP background to focusing on JS, I find myself adjusting to the differences in handling session variables. In PHP, the $_SESSION global variable was very convenient as it allowed easy access to session data throughout the code. ...

Split an array of articles into subarrays according to their topics using the reduce method in Javascript

I am currently seeking a solution to divide an array of articles into subarrays based on the first key-value pair and its order. After researching several Stack Overflow posts, I have come across one that seems to align closely with my objective: break a ...

Using Javascript to automatically replace content when the page is loaded

Having trouble with my fullCalendar jquery calendar and the 'eventClick' method. When I click on an event, the URL is getting encoded incorrectly. For example, I'm trying to pass a Wordpress URL like this: http://sitedomain.com/?post_type= ...

Guide to implementing CSS3 transitions with prefixes using JavaScript

Is there a way to apply CSS styles using JavaScript when I don't have access to the CSS file? #fade div { -webkit-transition: opacity 1s; -moz-transition: opacity 1s; -o-transition: opacity 1s; -ms-transition: opacity 1s; transition: ...

Retrieving the date input value in AngularJS using ng-model

Attempting to retrieve the value of my HTML 5 input date component by using ng-model binding as shown below: <input type="date" id="dateFin" class="form-control" name="dateFin" ng-model="filtres.dateFin" placeholder="dd/MM/yyyy" min="01 ...

What sets apart `var now = new Date();` and `var now = Date();` in JavaScript

I am specifically interested in exploring the impact of adding "new" on the variable, as well as understanding when and why it is used. I would also like to understand why I am obtaining identical answers when printing both versions. ...

Action outcome yielding identical content

Is there a way in MVC C# to return an ActionResult without making any changes to the view? I have attempted: Returning null and new EmptyResult(), but these result in an empty page Returning an empty JavaScript (again, resulting in an empty page) Retur ...

Creating a persistent toolbar in Vuetify: Tips and tricks

Within my Vuetify app, I've implemented a toolbar: <v-toolbar dark color="primary"> <v-toolbar-side-icon @click.stop="drawer.drawer = !drawer.drawer"></v-toolbar-side-icon> <v-toolbar-title>{{drawer.title}}</v-toolb ...

How do I ensure a single row in my table constantly remains at the bottom?

I am currently working on developing a MUI table that shows rows, with the last row displaying the total number of colors. The challenge I am facing is ensuring that the last row always stays at the bottom when there are no results in the table. I have att ...

The ajaxStart() and ajaxStop() methods are not being triggered

I'm currently working on a Q/A platform where users can click on specific questions to be redirected to a page dedicated for answers. However, when a user tries to answer a question by clicking the "Answer" link, certain background processes such as ...

Is there a way to determine if the value of an array has not been defined?

When attempting to access the value of an array that does not exist, an error is thrown stating "variable is not defined." Consider the following example: var arr = new Array(); arr['house']['rooms'] = 2; Using the following check: ...

Simple HTML and CSS exercise for beginners to grasp the concept

I'm looking to modify the layout of a specific webpage at based on the following instructions: First, insert this link within the <head> <link rel="stylesheet" href="https://trafficbalance.io/static/css/sdk/sdk.css"> -next, add th ...

What is the best way to conceal a choice field when it has no available options?

I am currently working with two dynamic select fields. The first select field is populated from a list and based on the selection made, it dynamically populates the second select field. However, I am facing an issue where I need to hide the second select ...