teko-oauth2-react-native
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

Teko Identity OAuth 2 Javascript Library for expo/react native app

A Javascript Library for integrate Teko Identity OAuth 2 to expo/react native application.

Getting started

import TekoID from 'teko-oauth2-react-native';

const App = props => {
  return (
    <View style={styles.container}>
      {!isLoggedIn ? (
        <View>
          <TouchableOpacity style={{ padding: 8, backgroundColor: 'lightblue' }} onPress={onLoginWithGoogle}>
            <Text>{'Login with google'}</Text>
          </TouchableOpacity>
        </View>
      ) : (
        <TouchableOpacity style={{ marginTop: 16, padding: 8, backgroundColor: 'lightblue' }} onPress={onLogout}>
          <Text>{'Logout'}</Text>
        </TouchableOpacity>
      )}
    </View>
  );
};

Installation

Add package to project by using git repo

# yarn
yarn add ssh://git@github.com:teko-vn/tekoid-react-native.git

# npm
npm install ssh://git@github.com:teko-vn/tekoid-react-native.git

Initiate the client

import TekoID from 'teko-oauth2-react-native';

TekoID.init({
  clientId: 'your-iam-client-id',
  googleClientId: 'your-google-client-id',
  scopes: ['your', 'scopes'],
  oauthDomain: 'https://oauth.develop.tekoapis.net',
  identityDomain: 'https://identity.develop.tekoapis.net',
}).then(() => {
  // The initial phase is finish.
  // Now you can do your logic.
  setLoggedInState(TekoId.isLoggedIn());
});

which returns a promise and resolve after finish initiating.

For more integrate details, please refers to this sample project.

Usage

async TekoID.init(configs)

configs key Type Default value Required
clientId string Y
googleClientId string Y
scopes [string] []
oauthDomain string https://oauth.tekoapis.com
identityDomain string https://identity.tekoapis.com
useNativeLogIn boolean false
  • clientId: the ID of your client which you registered in Identity Teko.
  • googleClientId: the Google client id of your app for logging in with Google.
  • scopes: the scopes of your client.
  • oauthDomain: the oauth domain to use SDK.
  • identityDomain: the indentity domain to use SDK.
  • useNativeLogIn: whether app use native logg in method or not (by browser).

NOTE: For development environment, please specific oauthDomain to https://oauth.develop.tekoapis.net and identityDomain to https://identity.develop.tekoapis.net

NOTE: TekoID.init() function is an async function.


TekoID.user

.isLoggedIn()

Return true if user is logged in, false if not.

.logout()

Clear current user's auth state in order to log user out.

Call this function when user click to Logout button in your application.

.loginWithGoogle()

Log in with google by expo-google-sign-in if useNativeLogIn is true, otherwise, use expo-google-app-auth

.loginWithPhone(phoneNumber)

Request a OTP number, which is sent to your phone for verify user.

.verifyOtp(otp)

Authenticate user by verify given otp (Requested with loginWithPhone method)

.getAccessToken()

Get logged user's current access token.

Return a string which is current user's access token if user is logged. null if user is not logged.

.getUserInfo()

Get current user's general information.

Return object which includes current user's information. Error will be thrown if user is not logged in.

  • With openid scope, id_token will return with fields: sub, name, picture, updated_at.

  • With addition profile scope, id_token will return with addition fields: email, phone_number, birthday, address.

  • With addition read:permissions scope, id_token will return with addition fields: roles, permissions, meta_data.

Return value example (with openid, profile, read:permissions included in scopes):

{
  at_hash: 'MIAbeb9-aBBfL1B3XVbF-A',
  aud: ['public-sample-client'],
  auth_time: 1557287614,
  exp: 1557291215,
  iat: 1557287615,
  iss: 'https://dev-id.teko.vn/',

  sub: '1',
  name: 'User Name',
  picture: 'https://domain.com/avatar_url.jpg',
  updated_at: '2019-12-31 00:00:00',

  email: 'email@teko.vn',
  phone_number: '0123456789',
  birthday: '2000-12-31',
  address: '',

  roles: ['string'],
  permissions: ['string'],
  meta_data: {
    key: 'value'
  }
}

.getUserScopes()

Get logged user's scopes.

Return array of string which is current user's scopes if user is logged. [] if user is not logged.

Return value example:

['openid', 'profile'];

NOTE: This scopes may different with the scopes you defined when init. Because the scopes may be able to access by your app, but not for the current user (the user denied the app for access his/her data). If you want to use scopes in order to display correct UI, please uses this scopes value.

.refreshAccessToken(listener: OnRefreshAccessTokenListener)

Refresh current access token:

  • If current refresh token is expire or invalid, TekoID will call logout method.
  • If refresh successfule, TekoID will call listener for client can continue process.

.setOnAuthStateChangedListener(listener)

Register a listener, which will be called when auth state changed (eg: login success, call logout method)

Todo

  • [x] Check authentication state
  • [x] Login with google
  • [x] Logout
  • [x] Get access token
  • [x] Get use info
  • [x] Login by OTP
  • [x] Expiration date
  • [x] Refresh access token
  • [ ] Login with native method

Package Sidebar

Install

npm i teko-oauth2-react-native

Weekly Downloads

0

Version

0.1.0

License

ISC

Unpacked Size

24.8 kB

Total Files

12

Last publish

Collaborators

  • tu.nb