2.1.1 • Published 2 years ago

pod-sso-pkce v2.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

pod-sso-pkce

This package includes methods for using SSO POD (https://docs.pod.ir/v1.6.0.0/SSO/Introduction/149/SSO) by The PKCE method.

You can use this package in any frontend project.

Installation

Use npm to install the package:

npm i pod-sso-pkce
Notice: All data is stored in Local Storage.

Methods

MethodDescription
authenticateTo login to the account with the SSO POD
authenticateWithAutologinTo login to the account with the SSO POD by automatic login
validateTo validate the user after returning from the SSO POD to your app and get an access token
refreshTokenTo get a new access token after the current access token expires
getAccessTokenGet the user's current access token
isAuthenticatedChecks if the user is logged in or not
logoutUser logs out from the user account

authenticate()

ParameterTypeDescriptionExample
ssoURLstringhttps://accounts.pod.ir/
clientIdstring20200000egh98412ea0a164708f109210
redirectUrlSsostringhttp://myapp.ir/validate
ssoScopestringprofile email

After calling this method, your app will be redirected to the SSO POD.

authenticateWithAutologin()

ParameterTypeDescriptionExample
ssoURLstringhttps://accounts.pod.ir/
clientIdstring20200000egh98412ea0a164708f109210
redirectUrlSsostringhttp://myapp.ir/validate
ssoScopestringprofile email

Notice: You must enter 4 parameters in url query string: keyId, accessToken, timestamp, sign

Read this link for more information: https://docs.pod.ir/v1.6.0.0/SSO/User/8660/autologin

After calling this method, your app will be redirected to the SSO POD.

validate()

ParameterTypeDescriptionExample
ssoURLstringhttps://accounts.pod.ir/
clientIdstring20200000egh98412ea0a164708f109210
redirectUrlSsostringhttp://myapp.ir/validate
redirectUrlAfterValidatestringhttp://myapp.ir/mypanel
onErrorfunction() => alert('something wrong')

After calling this method, if the validation is successful, the user will be redirected to your target url (redirectUrlAfterValidate) but if it isn't, the onError event will occur.

refreshToken()

ParameterTypeDescriptionExample
ssoInfoObject{ clientId: '20200000egh98412ea0a164708f109210', ssoURL:'https://accounts.pod.ir/' ,onError: () => alert("something wrong")}

After calling this method, you can get a new access token with the getAccessToken() Method. the onError event will occurs if an error happens.

getAccessToken()

Return valuesTypeDescription
access tokenstringWhen the user is logged in
nullnullWhen the user is not logged in

isAuthenticated()

Return valuesTypeDescription
truebooleanWhen the user is logged in
falsebooleanWhen the user is not logged in

This method is improving...

logout()

ParameterTypeDescriptionexample
redirectUrlAfterLogoutstringThe page you want the user to see after logout from the accounthttp://example.ir/home

Usage

authenticate() Example

import { authenticate } from 'pod-sso-pkce';

function App() {
  const ssoURL = 'https://accounts.pod.ir/';
  const clientId = '20200000egh98412ea0a164708f109210';
  const redirectUrlSso = 'http://myapp.ir/validate';
  const ssoScope = 'profile email';

  // ...
  authenticate(ssoURL, clientId, redirectUrlSso, ssoScope);
  // ...
}

authenticateWithAutologin() Example

sample address: http://myapp.ir/autologin?access_token=123&key_id=456×tamp=789&signature=101112

import { authenticateWithAutologin } from 'pod-sso-pkce';

function App() {
  const ssoURL = 'https://accounts.pod.ir/';
  const clientId = '20200000egh98412ea0a164708f109210';
  const redirectUrlSso = 'http://myapp.ir/validate';
  const ssoScope = 'profile email';

  // ...
  authenticateWithAutologin(ssoURL, clientId, redirectUrlSso, ssoScope);
  // ...
}

validate() Example

import { validate } from 'pod-sso-pkce';

function App() {
  const ssoURL = 'https://accounts.pod.ir/';
  const clientId = '20200000egh98412ea0a164708f109210';
  const redirectUrlSso = 'http://myapp.ir/validate';
  const ssoScope = 'profile email';
  const redirectUrlAfterValidate = 'http://myapp.ir/mypanel';

  // ...
  validate(ssoURL, clientId, redirectUrlSso, redirectUrlAfterValidate, () => {
    console.log('error in validation');
  });
  // ...
}

refreshToken() Example

import { refreshToken, getAccessToken } from 'pod-sso-pkce';

async function fetch() {
  // ...
  if (error.response.status === 401) {
    await refreshToken({
      clientId: '20200000egh98412ea0a164708f109210',
      ssoURL: 'https://accounts.pod.ir/',
      onError: () => alert('something wrong'),
    });

    const accessToken = getAccessToken();
  }
  // ...
}

getAccessToken() Example

import { getAccessToken } from 'pod-sso-pkce';

function App() {
  // ...
  const accessToken = getAccessToken();
  // ...
}

isAuthenticated() Example

import { isAuthenticated } from 'pod-sso-pkce';

function App() {
  // ...
  if (isAuthenticated()) {
    alert('you are login.');
  } else {
    alert('you are not login');
  }
  // ...
}

logout() Example

import { logout } from 'pod-sso-pkce';

function App() {
  // ...
  logout('http://myapp.ir/home'); // :D
  // ...
}

LICENSE

MIT

2.1.1

2 years ago

2.1.0

3 years ago

2.0.0

3 years ago

1.1.4

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.1

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.0.0

3 years ago

0.1.0

3 years ago