I have an AngularJs UI login page and I'm posting the username and password details to a Servlet to validate, and when I'm returning back I'm using sendRedirect from Servlet but it's not working. Here is my sample code
In Servlet:
req.sendRedirect("NewFile.jsp");
It is displaying content inside NewFile.jsp within the login page itself instead of redirecting to NewFile.jsp
I have tried using "$window.location.href" inside AngularJs code but that itself didnt forward to home page.
Any help is much appreciated.
Thank you!
Acording of what you said in the comment of the bounty, I have to say that having everything in only one controller sounds like a bad idea, for more information about this and naming conventions go to Naming conventions and best practices
According to your question, maybe you need to give more details of the problem, it's not clear.
Have you tried to use $location module?
https://docs.angularjs.org/api/ng/service/$location
You can use de .path() module to redirect.
$location.path('#!/');
That will redirect you to the home page.
I need to set up an authentication system with Node.js Express framework and Vue.js. I saw up to now solutions only with JWT. It would be nice for me to found a solution as this in AngularJS, with promises: https://vickev.com/#!/article/authentication-in-single-page-applications-node-js-passportjs-angularjs .
Is it possible with Vue.js router or somehow otherwise to set up a resolve parameter with promise function as in this guide with AngularJS?
Thank you!
I am currently looking into this myself, and having worked with vue-router a little bit I think that router.beforeEach might do the trick: https://router.vuejs.org/en/advanced/navigation-guards.html
The way I see it is that a promise inside beforeEach route guard would be calling the backend api route and in case of success would be resolved and navigation to our route can continue, in case it's rejected - user's redirected to login route.
Will respond later once I've looked into this propertly myself as I'm still learning. Let me know if you have learnt anything about it since you've created this question.
I am working on building web apps with Express and AngularJS. The problem is I though I pretty well understood how to figure out html5 mode (avoiding hashbang on url for angular apps), but I didn't.
Right now for ordinary urls such as www.url.com/register, it works perfectly fine. Even if I refresh it it stays stable just okay.
However, for some url that has parameters such as 'www.url.com/user/1231231412414', when I refresh it, I see bare json data set.
I put router.get('*', {=}) stuff at the very end of the route script, and tried anything I can do, but I am totally stuck.
Is there anybody who know the solution and had gone through this problem? I would really appreciate for any help or advice for this problem. Thanks, guys.
I apologize this question turned out a bit long, but I have worked on this for some time and really needed to explain all the story.
Background: App based on MEAN stack, trying to authorize Facebook logins using Passport.js.
Following Passport.js guide I implemented something similar to:
// HTML
Add a Facebook login
// send to facebook to do the authentication
app.get('/connect/facebook',isLoggedIn, passport.authorize('facebook',
{ scope : 'email' })
);
// handle the callback after facebook has authorized the user
app.get('/connect/facebook/callback',
passport.authorize('facebook', {
successRedirect : '/profile',
failureRedirect : '/profile'
}));
Notice the target=_self in the html in order to skip Angular routing.
Clearly, authorization works fine. However, redirection does not work, as the routing is handled by Angular. After authorization I never land on /profile (but on the default Angular route).
Therefore, I tried with a custom callback as suggested by Passport.js here, with the hope of passing json data to Angular, and let Angular do the routing. I ended up doing something like:
// In the controller
$http.get("/connect/facebook").success(function(data){
// here I wait for json data from the server and do the routing
});
// I call this route from Angular
app.get('/connect/facebook',isLoggedIn,passport.authorize('facebook',
{ scope : 'email' })
);
// But Facebook lands here!
app.get('/connect/facebook/callback',function(req, res, next) {
passport.authorize('facebook', function(err, user, info) {
res.json({something:smtg});
...
Clearly custom callbacks work for local-login, as Passport.js explains. But here do you see the problem? I call /connect/facebook from Angular, but I should receive some json from /connect/facebook/callback.
I am about to give up Passport, but before this, do you see any solution which would allow landing on /profile after FB authorization, perhaps with a custom message? Many thanks for reading through.
EDIT:
The same question had been reported as an issue on the Passport-Facebook GitHub account. Some additional attempts have been posted there, but not quite the fix yet.
This is a bit more in depth than can be described in one answer, but I'll try to start pointing you in the right direction.
Essentially, Angular.js routes are not really HTML routes at all, but an internal route structure that happens to use the URL for use of the end user. Remember that Angular.js is a client script, and that a full page reload is not desired, as this will reload the entire script. Therefore, /# is used to trick the browser into jumping to a specific bit of code within the already loaded script. (as opposed to the traditional anchor location in the HTML document). Unfortunately (or fortunately), HTML 5 mode allows you to hide the /# part of the url, so instead of seeing http://somesite.com/#/someroute you just see http://somesite.com/someroute. Rest assured, however, that the /# is still there. Angular.js uses the HTML5 pushState (AKA HistoryAPI) to perform the magic replacement.
Given this, if you have called a server route, you are outside the Angular.js script, and any call to load the angular script again will start from the very beginning. You can't actually call your Angular.js route from the server without a full reload. Therefore, you are really doing a double route redirect here. Your server should be calling it's default route for angular, appending /#/someroute to the call. The angular.js page will load, parse off the /#, and redirect to the correct angular route. Keep in mind, however, that if there was any dependency on already loaded objects, those are no longer in memory. Therefore, any route accessed this way should operate as if it is an entry point to your application.
Effectively, you should try using successRedirect : '#/profile', keeping in mind that the profile route in angular should be treated as an app entry point.
Hopefully this gets you started.
If #Claies's way is not working, is it possible you have not get rid of the #= fragment from the facebook callback.
Have a read of this post
I try lots of methods to find current controller and action in cakephp routing, but I didn't got answer. Is there any method to find controller and action in routing. If yes, please help me. Or if there is any other way to find these, Please tell me.
It's the routes that decide to what controller and action a url maps to. When routes.php is being loaded the routing process has still not been done (since you are still setting up routes), so you cannot know controller or action is in routes.php.
If the website formatted something like this :
www.johndoe.test/test/test_action/
then probably it can be found on test controller but of course that depends on the settings of the website on how the developer configured it. It may also be found on routes.php like #ADmad said.