Anney
Anney

Reputation: 99

React Native app - how to use oauth 2.0 and Open ID connect?

I've searched and spent so much time wrapping my head around this. Given a client_id, client_secret, authorization_endpoint, auth URL, token_endpoint and a few other info--

*

  1. How do I do connect to get an auth token via ajax call?
  2. And when I receive the JWT, how do I parse it?

*

There are libraries like redux-oidc available, but doesn't look like its compatible in a react-native app (ran into a rabbit hole of errors).

react-native-jwt upon basic import on my react-native app, gives me an undefined split() error.

More info: I use redux-saga for middleware.

Hoping somebody can help me, even leads would be truly appreciated.

Upvotes: 3

Views: 1732

Answers (1)

Anney
Anney

Reputation: 99

I got it working! Here's what I did.

1.) Set an Authorization Header with the base64 encoded client id and secret.

'Authorization': 'Basic ' + Base64.encode([client_id]:[client_secret])

I used a free Base64 library from the internet instead of relying on Javascript's btoa().

2.) I did a POST request to /connect/token with the parameters ({ grant_type: 'client_credentials', ...}) and my scope. Made sure the data was sent via application/x-www-form-urlencoded

Got the token in the response!

Upvotes: 1

Related Questions