Sufyan Salim
Sufyan Salim

Reputation: 343

Set custom expires in time expo-auth-session, for Fitbit API in react native, expo

I want to add custom expires time for fitbit implicit auth flow the default expires time is a day you can customize it I want to make it for a year. If you are using the web version you can change it by directly changing the expires_in params in the url.
As shown in this below url.

https://www.fitbit.com/oauth2/authorize?response_type=token&client_id=randomid&redirect_uri=https%3A%2F%2Fauth.expo.io%2F%40albert%2Fyourapp&scope=activity%20heartrate%20location%20nutrition%20profile%20settings%20sleep%20social%20weight%20oxygen_saturation%20respiratory_rate%20temperature&expires_in=31536000

WebBrowser.maybeCompleteAuthSession();

const useProxy = Platform.select({ web: false, default: true });
// Endpoint
const discovery = {
  authorizationEndpoint: 'https://www.fitbit.com/oauth2/authorize',
  tokenEndpoint: 'https://api.fitbit.com/oauth2/token',
  revocationEndpoint: 'https://api.fitbit.com/oauth2/revoke',
};

  const [request, response, promptAsync] = useAuthRequest(
    {
      responseType: ResponseType.Token,
      clientId: 'randomid',
      scopes: ['activity', 'profile'],
      redirectUri: makeRedirectUri({
        useProxy,
        scheme: 'nudge://',
      }),
    },
    discovery
  );

  const loginFitbit = async (token) => {
    if (token) {
      try {
        await signInFitbit(token, dispatch);
        await storeFitbitToken(token);
        setLoggedIn(true);
      } catch (e) {
        setLoggedIn(false);
        addError('Could not login Fitbit. Please try agian later.');
      }
    }
  };

  React.useEffect(() => {
    if (response?.type === 'success') {
      const { access_token } = response.params;
      console.log("res",response)
      loginFitbit(access_token);
    } else {
      console.log('error', response);
    }
  }, [response]);


  React.useEffect(() => {
    const fetchData = async () => {
      let token;
      try {
        token = await getFitbitToken();
        setLoggedIn(true);
      } catch (e) {
        setLoggedIn(false);
        console.error(e);
      }
      dispatch({ type: 'RESTORE_FITBIT_TOKEN', token: token });
    };

    fetchData();
  }, [dispatch])

Upvotes: 1

Views: 494

Answers (2)

Vasyl Nahuliak
Vasyl Nahuliak

Reputation: 2468

If you want to add extra query params for your auth request you need to add extraParams object with your custom fields.

https://docs.expo.dev/versions/latest/sdk/auth-session/#authrequestconfig

const [request, response, promptAsync] = useAuthRequest(
  {
    responseType: ResponseType.Token,
    clientId: "randomid",
    scopes: ["activity", "profile"],
    redirectUri: makeRedirectUri({
      useProxy,
      scheme: "nudge://",
    }),
    extraParams: {
      expires_in: 3600, // <--- new value
    },
  },
  discovery
);

Upvotes: 1

Vasyl Nahuliak
Vasyl Nahuliak

Reputation: 2468

If your application type is currently set to using the Authorization Code Grant Flow, access tokens have a default expiration of 8 hours (28800 seconds). This cannot be changed.

However, if you'd like your users to be able to select how long your application can access their data, you will need to change your application settings to the Implicit Grant Flow. This authorization flow allows users to select how long they give consent to your application (1 day, 1 week, 30 days, or 1 year).

https://community.fitbit.com/t5/Web-API-Development/Query-parameter-expires-in-not-working/td-p/3522818

Upvotes: 1

Related Questions