github redirect_uri with angularjs - angularjs

I'm trying to add login with github in my app. So while testing on my local machine, I set my callback url like this http://localhost:8000/login/callback/.
And after that, I add a login link to my page like this, Login, and also
$routeProvider.when('/login/callback', {
controller: 'loginCtrl'
});
It success returned me a code with a link like this: http://localhost:8000/login/callback?code=cd07ff3b70f5d1d1b8a2. But angularjs can not response correctly.
Error response
Error code 404.
Message: File not found.
Error code explanation: 404 = Nothing matches the given URI.
I've also tried to set my redirect_url to http://localhost:8000/#/login/callback, but the return link is like this http://localhost:8000/?code=61e9c8b73c073a0bccc0/#/login/callback
I need the code to be appear at the end of the url but not in the middle so that I can use $location.search().code to get my code. How can I set it properly?
Like change my redirect_uri or change my router?

#lowerkey I still don't know how to handle this but I think that's because the page was reloaded.
When you are not setting the html5Mode to true, then will be # at the url, and if you reload the page, the # will track the information to the $scope to work with the htmlHistory api so that the app can work properly even with the page refresh.
But once you set the html5Mode to true, then there's no # to store any $scope information, and when the page reload, angular can not found the accordingly scope so it return 404.
You can check $location section on angularjs guide, it has a section explaining why this happen.
Page reload navigation
The $location service allows you to change only the URL; it does not allow you to reload the page. When you need to change the URL and reload the page or navigate to a different page, please use a lower level API, $window.location.href.

we should somehow push github to send params in html5-friendly way, i.e.
/login/callback/:code
for now I've created my hack-solution for it:
var code = parseUrlParam($location.absUrl(), 'code');
function parseUrlParam(url, param){
var temp = url.match(param + '=(\\w*)');
if(!temp[1]){
return false;
}
return temp[1];
}

Related

angular routeparams empty on api callback

Im trying to get some paramters from the urlcallback coming from an external authentication, in my angular projct using angular-route/$routeProvider
the api redirects to:
http://localhost:8080/dist/?somevar=someval&val2=someotherval#/
Note the params come before #/
I try to read the get values with $routesparams like:"
$scope.$on('$routeChangeSuccess', function() {
// $routeParams should be populated here
console.log($routeParams.somevar);
console.log($routeParams);
});
This returns an emptyresult. however it i change the url to:
http://localhost:8080/dist/#/?somevar=someval&val2=someotherval
it works, with the api I can give a callback url and I set it like:
?callbackUrl=http://localhost:8080/dist/#/
How should I get these parameters from the url whithout changing the callback url?
In this case, the problem is with the Hashbang mode, which has a hashPrefix #. In the configuration phase, you have to enable html5Mode $locationProvider.html5Mode(true);
But this requires URL rewriting on server side and the HTML <base> tag.
Read more here: https://docs.angularjs.org/guide/$location
So the explanation why it does not work for you this way, is when you give the http://localhost:8080/dist/?somevar=someval&val2=someotherval#/ url to angular, it looks for the parameters after the #, and when you populate the $routeParams, it will create the parameters after the #.

Get the url that a user is coming from AngularJS

If an user is coming from an specific page i need to do get some values out of a cookie and change what the users sees.
Now, the issue is that i cannot find a way to view what page the user is coming from.
EDIT: This is intended to capture when the users clicks back in a page and save the state of the previous page.
Any ideas?
Thanks in advance
Solved. Every time i load a page i'm saving the url, so when i get to this page i just have to read it to tell. Thanks!
You can use browser history in our javascript or you can write your last page in cookies and get the last link then update it
Using cookies will indeed fix this for you. So when a user goes to a new page - set a cookie like:
app.controller('myController',['$scope', '$location', $cookies], function($scope, $location, $cookies){
if($cookies.get('page') == '/index'){
//do stuff if user came from index
}
$scope.pageChanged = function(value){
$cookies.put('page', value);
$location.path('/index');
}
}
just make sure you use the pageChanged function to set your page every time user changes pages.
Using the $routeProvider you can use the resolve function to detect when a new route has been loaded.
Another way would be to listen for the event $routeChangeSuccessor $routeChangeError and get the information needed from the service $location or $route.
If you want a sample just ask me, I'll try to post one as soon as I have free time.

UI-Router state doesn't work when I'm redirected to my app from Adyen (payment system)

I'm trying to get my app to work with the Adyen payment system. At a certain point I redirect the user to Adyen to make a payment. When the user is done, Adyen redirects to the provided return url. Adyen also adds some query parameters to the url, that are useful to use on the return url page.
This is an example of a url that Adyen redirects to after payment:
http://my.domain/#/payment/result?authResult=AUTHORISED&merchantReference=000220-16402-1459625180&merchantSig=BjeKwBOp70hcsNRD8GUat3fHn4qXxX4Bb2Nxg9RRa04%3D&paymentMethod=mc&pspReference=8614596253051402&shopperLocale=en_GB&skinCode=wMrD9Eei
Now, I want to use UI-Router to get me to the proper page, and load the right controller. Depending on the value in the query parameter authResult, I want to go to a different state. I have created a state for /payment/result?authResult:
$stateProvider.state('payment.result', {
url: '/payment/result?authResult',
templateUrl: IM_APP_DIR + '/views/payment.result.html',
controller: function($state, $stateParams) {
switch ($stateParams.authResult) {
case 'AUTHORISED':
$state.go('payment.authorised');
break;
case 'CANCELLED':
$state.go('payment.cancelled');
break;
etcetera...
}
}
});
Example for the authorised state:
.state('payment.authorised', {
url: '/payment/authorised',
templateUrl: IM_APP_DIR + '/views/payment.authorised.html,
controller: 'PaymentCtrl'
})
Now when I test this and the Adyen test environment redirects met to the url mentioned above, the controller for the /payment/result?authResult state is never entered. (I placed a breakpoint at the line where the switch is defined, but Chrome never gets there.) Instead I get a lot of errors from angular in my console. They mainly say I am getting into an infinite digest loop (https://docs.angularjs.org/error/$rootScope/infdig?p0=10&p1=%5B%5D). I guess that has nothing to do with this problem, since this is also what happened when Adyen redirected to the return url and I had not yet defined any state for the redirect path.
Any ideas what may be wrong with the states I defined, taking the redirect url into account?
Thanks a lot in advance for any help!
Ok, I got it to work eventually. The problem was that I had the parent > child setup in the routing config all wrong. I had also omitted a ui-view directive element in the parent route that the child view could be rendered to.

How to remove flash of template in angularjs

I am using filters (of web.xml) to check if the session (http session on server side) is valid or not. If the session is not valid, then the response return error 401, and I handle it in angularjs and redirect the route to login page.
So, the situation is like this:
There is a url localhost:8585/xyz. To access this url, login is required.
When the request is made to access this url (without logging in), the filter check whether the session is valid or not. Meanwhile (as the session is being checked), the template of url localhost:8585/xyz is displayed. As the user is not logged in, session is not valid, so filter send 401 error. I handle the error in angularjs and redirect the route to login page.
I don`t want the template to be displayed before getting the actual response from filters and redirecting the route to login page.
When you're defining your routes you can use the resolve property on your route (https://docs.angularjs.org/api/ngRoute/provider/$routeProvider).
This will allow you to specify a function which returns a promise and the route will only be changed once the promise resolves. As an added bonus the value coming back from the resolve promise can be injected into the handling controller.
Here is a plnkr showing how you can use a global function for any of the routes you need.
You can use ng-cloak to hide flash of unstyled content for Angular if need be.
Your main problem seems to happen after Angular is already loaded, though. In the controller that displays the template you can have a setting:
this.loaded = false;
Then in the template itself something like:
<throbber ng-if=!ctrl.loaded/>
<content ng-if=ctrl.loaded>
<!-- template content -->
</content>
Then you can set this.loaded = true when you get the response from the filter. If you get a 401 the redirect happens anyway and this.loaded doesn't get set to true anyway.
Without any example code, it's difficult to know this is possible for you, but the ng-show directive can be used to hide an element if it's provided value is falsy:
<div ng-show="authenticated">
<!-- content you wish to hide here -->
</div>
In your controller, simply set the value to true when the (successful) response is returned
myApp.controller('MyController', function ($scope) {
$scope.init = function() {
// check auth here
$scope.authenticated = true;
}
$scope.init();
});

AngularJS - How can I do a redirect with a full page load?

I want to do a redirect that does a full page reload so that the cookies from my web server are refreshed when the page loads. window.location = "/#/Next" and window.location.href = "/#/Next" don't work, they do an Angular route which does not hit the server.
What is the correct way to make a full server request within an Angular controller?
For <a> tags:
You need to stick target="_self" on your <a> tag
There are three cases where AngularJS will perform a full page reload:
Links that contain target element
Example: link
Absolute links that go to a different domain
Example: link
Links starting with '/' that lead to a different base path when base is defined
Example: link
Using javascript:
The $location service allows you to change only the URL; it does not allow you to reload the page. When you need to change the URL and reload the page or navigate to a different page, please use a lower level API: $window.location.href.
See:
https://docs.angularjs.org/guide/$location
https://docs.angularjs.org/api/ng/service/$location
We had the same issue, working from JS code (i.e. not from HTML anchor). This is how we solved that:
If needed, virtually alter current URL through $location service. This might be useful if your destination is just a variation on the current URL, so that you can take advantage of $location helper methods. E.g. we ran $location.search(..., ...) to just change value of a querystring paramater.
Build up the new destination URL, using current $location.url() if needed. In order to work, this new one had to include everything after schema, domain and port. So e.g. if you want to move to:
http://yourdomain.example/YourAppFolder/YourAngularApp/#/YourArea/YourAction?culture=en
then you should set URL as in:
var destinationUrl = '/YourAppFolder/YourAngularApp/#/YourArea/YourAction?culture=en';
(with the leading '/' as well).
Assign new destination URL at low-level: $window.location.href = destinationUrl;
Force reload, still at low-level: $window.location.reload();
After searching and giving hit and trial session I am able to solove it by first specifying url like
$window.location.href = '/#/home/stats';
then reload
$window.location.reload();
I had the same issue. When I use window.location, $window.location or even <a href="..." target="_self"> the route does not refresh the page. So the cached services are used which is not what I want in my app. I resolved it by adding window.location.reload() after window.location to force the page to reload after routing. This method seems to load the page twice though. Might be a dirty trick, but it does the work. This is how I have it now:
$scope.openPage = function (pageName) {
window.location = '#/html/pages/' + pageName;
window.location.reload();
};
Try this
$window.location.href="#page-name";
$window.location.reload();

Resources