Vivek V Dwivedi
Vivek V Dwivedi

Reputation: 2615

How to get url parameters using ui-router in angularjs

I am using ui.router for my routes in angular. Here is what I am trying to do:

I have my router file where I have defined states like this:

state('login',{
url:'/login',
views:{
    'slot1':{
    templateUrl:'app/modules/authentication/authentication.html',
    controller:'AuthenticationController',
    controllerAs:'authCntrl'
}}})

What I want to do is to log the user in is they come with a URL in this format

http://someapp.io/login?user=name&pass=423%%$dsad32

Now how do I define the route URL and how to access the user and pass in my controller. Don't need full code, just the concepts or link to documentation would do.

This is how my controller looks like:

(function(){
angular.module("app.auth",[]).
controller("AuthenticationController", function( $state, $stateParams, AuthenticationService, LoadingService){
var self = this;
console.log($stateParams);
}

And this logs the following to console:

Object {}

EDIT:

Strangely, if I change url in my router to this:

url:'/instore_login?user=&pass='

I am able to get these values. Is this even right?

Upvotes: 0

Views: 2944

Answers (2)

Martin
Martin

Reputation: 16292

Your state definition needs to explicitly state that it expects parameters. There are more than one way of doing this with uirouter. The simplest is to add it to your URL definition :

url: '/login?user&pass', 

You should always be vigilant handling user credentials. This approach seems like it might be a bad code smell. Can you talk a bit in your original question what your plans are with this controller and state and we can make sure you are not opening yourself to a security issue.

Upvotes: 1

hasnat
hasnat

Reputation: 3027

in your AuthenticationController, inject $stateParams it should be an object like

{user: "name", pass: "423%%$dsad32"}

Upvotes: 0

Related Questions