Skip to content
This repository was archived by the owner on Mar 22, 2022. It is now read-only.

OAuth 2 login for cordova #530

Closed
dortamiguel opened this issue Jun 20, 2017 · 2 comments
Closed

OAuth 2 login for cordova #530

dortamiguel opened this issue Jun 20, 2017 · 2 comments

Comments

@dortamiguel
Copy link

I have a web app that logins through oauth2 using feathers with passport.

This web app has a link that redirects to the oauth login, when the login is successfull the browser goes back to the original page and returns a token for the login.

This works on a normal browser but on a cordova / phonegap app don't because the callback successRedirect of the oauth can't redirect back to the app.

How I can make this work on latest android and ios with wkwebview?

@marshallswain
Copy link
Member

Did you get this working @ellipticaldoor?

@dortamiguel
Copy link
Author

dortamiguel commented Jun 21, 2017

Yes, on the cordova config.xml just add this line and rebuild the project,

<plugin name="cordova-plugin-inappbrowser" spec="^1.7.1" />

After that you can log in through oauth with this method, (after oauth has been configured on the backend with the instructions of the official docs feathers)

cordovaOauthLogin() {
	// Open browser for linkedin oauth login
	const browser = window.cordova.InAppBrowser.open(
		'http://localhost:3060/auth/linkedin',
		'_blank',
		'location=no,clearsessioncache=yes,clearcache=yes'
	)

	// Starts event to detect when the login has finished
	// and the feathers cookie is ready
	browser.addEventListener('loadstop', event => {
		// Detect the successRedirect from feathers
		if (event.url === 'http://localhost:8066/')
			browser.executeScript(
				{ code: 'document.cookie.valueOf("feathers-jwt")' },
				async cookie => {
					// Extract and set feathers-jwt token from cookie
					const token = `${cookie}`.slice(13)
					localStorage.setItem('feathers-jwt', token)

					// Close the InAppBrowser
					browser.close()

					// Login after token is ready on the localStorage
					await this.$store.dispatch('auth/authenticate')
					this.$router.replace({ name: 'SelectProfile' })
				}
			)
	})
},

This was tested for ios and android.

Someone knows how to get rid of the callbacks when using an addEventListener?

# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants