0

I want to open a new tab in which I make a call to a function from my own server (The one is below). The reason I want to do this is to be able to redirect the user from the server and not the frontend.

I don't know how to do this, but my server function is supposed to check if the user is registered to Stripe, if he is not, then I must redirect him to Stripe's website. I want to redirect using response.redirect.

My server is programmed in NodeJS and the function looks like this:

MyFunction = function (req,res) {
    if (user has already integrated with stripe) {
     return res.send(200)
    }
    else {
     res.redirect('https://connect.stripe.com/oauth/authorize?response_type=code&client_id=CLIENT_ID&scope=read_write'))
    }
}

How do I open a new tab from my controller and in that tab call for the function in the server?

See, the main problem is not opening the window, I have already seen the suggested solutions, it is how to, after opening it, making the http request to my server from that tab in ANGULAR.

gbsojo
  • 131
  • 2
  • 8
  • http://stackoverflow.com/a/11384018/644669 – Zakaria Feb 02 '16 at 15:16
  • Possible duplicate of [Open a URL in a new tab (and not a new window) using JavaScript](http://stackoverflow.com/questions/4907843/open-a-url-in-a-new-tab-and-not-a-new-window-using-javascript) – Zakaria Feb 02 '16 at 15:16
  • 1
    How do you call that MyFunction? If it's from a link, did you try adding target="_blank"? – piyuj Feb 02 '16 at 15:19
  • Consider making each tab its own `route` (or `state` if using UI-Router), and doing the stripe check in a `resolve` promise. If the resolve fails, the route (or state) change will fail, and you can redirect by binding on `$stateChangeError`. – adamdport Feb 02 '16 at 15:21

2 Answers2

0

Firstly, it is not possible to open a new tab with javascript. You can open a new window, and if the user has new windows to open as tabs then you will get a tab rather than a window. To open a new window:

window.open('urlToOpen')

It would probably be best to open the new window with the URL to your NodeJS route endpoint and then send the redirect to stripe. If the user is already integrated on stripe then close the newly opened window and redirect the parent window if necessary.

  • It worked using simply the endpoint route. Idk why it hand't worked before, it worked now. Thanks. – gbsojo Feb 02 '16 at 15:47
0

It's not clear to me why you want to open the tab first, and then decide where to redirect it to after the fact. You could do that like so:

var foo = window.open('[some url]');
if ([some logic]) {
    foo.location.href='[a different url]'
} 

...but it would probably be more reasonable to determine the correct URL in the first place, before opening the new window/tab.

Daniel Beck
  • 20,653
  • 5
  • 38
  • 53