Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sending the token to a separate frontend application from Phoenix api-only app #37

Closed
ghoshnirmalya opened this issue Jun 26, 2017 · 9 comments

Comments

@ghoshnirmalya
Copy link

How are we supposed to send the token to the frontend application since the frontend application will have to open a new window for Google authentication to work properly. So, when we send the token to the frontend from the new window, only the token will be rendered in the window as json.

For example, in my backend application, I ping localhost:4000/api/v1/auth/google to get the token. But when I ping it from my frontend application, which is running on localhost:3000, I get something like the following:

screen shot 2017-06-27 at 02 25 22

I'm guessing this happens because I'm not requesting it from a new window! But, if I request it from a new window, how will the token be sent to the parent window?

@hassox
Copy link
Member

hassox commented Jun 26, 2017

Are you using the code or the token flow from google?

Also can you please show your auth controller (where you have the Ueberauth plug) and your ueberauth config / routes (no secrets please!)

@ghoshnirmalya
Copy link
Author

This is the repo (https://github.com/ghoshnirmalya/social_app_api) in which I'm using this library. Link to my auth_controller: https://github.com/ghoshnirmalya/social_app_api/blob/master/web/controllers/auth_controller.ex.

@hassox
Copy link
Member

hassox commented Jun 26, 2017

This case statement isn't doing anything: https://github.com/ghoshnirmalya/social_app_api/blob/master/web/controllers/auth_controller.ex#L34 (remember... immutable data!)

I don't think that you're actually getting a signed in connnection (you generate the token but the conn is lost)

Can you explain how you're using the 'fetch' api here?

The usual flow for the code flow would be:

  1. request login for google from your server
  2. your server redirects your browser to a new window (google)
  3. google redirects back to your server (the callback action)
  4. You confirm (with the auth struct) and sign into guardian
  5. You redirect back to your application

If you're wanting to oauth to an SPA you need to use the token auth (client side oauth). Unfortunately Ueberauth google doesn't yet support this but this PR #35 will bring it in. I haven't had a chance to properly review it yet but will do soon.

@ghoshnirmalya
Copy link
Author

ghoshnirmalya commented Jun 27, 2017

I can confirm that I'm getting a signed in connection since if I pass the token sent after signing in, all the other routes work. Whereas, if I don't send the token, the routes doesn't work.

Also, my major problem while handling authentication on the frontend is I don't know which api to ping for the authentication to work. When I ping it from my frontend application, which is running on localhost:3000, I get something like the following:

screen shot 2017-06-27 at 02 25 22

N.B.
It doesn't open in a new popup. Maybe that's why the errors are coming. But again, I'm unsure how to do that yet! Also, I'm using a SPA in the frontend.

@ghoshnirmalya
Copy link
Author

If I use a client-side oauth like react-google-login, it will send me a token after authenticating with Google but I'm unsure how I can validate that token with ueberauth_google.

@ghoshnirmalya
Copy link
Author

Currently, this is the way I'm handling logging a user in:

  1. I'm pinging Google api using react-google-login
  2. It returns me a token if the authentication is successful
  3. Then, I'm pinging the ueberauth route in my api app (something like /api/v1/auth/google)
  4. I'm encoding a new token and then sending a Guardian token to the frontend application

I'm unsure if this is the correct way to do this.

@rhysldavis
Copy link

Hey I'm having this very issue. Has a better solution been found then two separate auth steps?

@gary-menzel
Copy link

Exactly the same problem - but with an Ember app (will also need it to work for a React app).

I have a PoC showing Ueberauth can authenticate against Google, Microsoft, Okta, etc with an API based Phoenix backend but the callback stage has to ultimately result in a redirect back to the separately hosted front-end.

[in fact - I based it off the work from @ghoshnirmalya to get the Phoenix part working]

@benonymus
Copy link

Hey so there a better solution for this? or could someone explain the second step?
how can i send the token from the client to my auth/google and have ueberauth use it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants