@ace-auth/sdk-browser 中文文档教程

发布于 4年前 浏览 15 更新于 3年前

Ace authorisation SDK

Install

npm install @ace-auth/sdk-browseryarn add @ace-auth/sdk-browser

Use cases

SDK 使用 umd 模块声明。 所以你可以使用它,比如节点模块系统(commonjs),amd 或 在浏览器中全局。

Parameters

NameDefault valueDescription
aceEndpointRequiredAce-ui endpoint.
autorefreshOptionalfalseRefresh tokens automatically. (onAceEndpoint should be set)
clientIdRequiredAce client ID.
debugOptionalfalseVerbose output
defaultRedirectUriOptionalDefault url for signIn, signUp, signOut methods
flagsOptional{}Available flags: { nosso: boolean }
localeOptional"en-US"Locale. Available locales 'en-US' or 'zh-CN'
onTokensOptionalCallback on new tokens received
ownAceEndpointOptionalClient ace backend endpoint.
signInRedirectUriOptionalUrl for signIn method.
signOutRedirectUriOptionalUrl for signOut method.
signUpRedirectUriOptionalUrl for signUp method.
storageOptionalwindow.localStorageStorage implementing window.localStorage interface.
storageKeyOptional"ace-sdk-tokens"Token's storage key

Methods

NameReturn valueDescription
clearStorage()Promise<void>Clears storage data
getTokens()Promise<ITokens>Returns client tokens
getOriginUrl()stringReturns origin url before signIn or signUp
getSignInUrl()stringReturns ace sign in url
getSignUpUrl(params?: { referrer?: string })stringReturns ace sign up url
on(event: string, cb: (tokens:ITokens)) => void)voidSubscribes to event
off(event: string, cb: (tokens:ITokens)) => void)voidUnsubscribes from event
setLocale(locale: 'en-US'|'zh-CN')'en-US'|'zh-CN'Sets locale for ACE class
signIn()Promise<ITokens|void>Starts sign in flow if anonymous user, returns tokens if logged in
signOut()Promise<void>Starts sign out flow
signUp(params?: { referrer?: string })voidStarts sign in flow

Interfaces

interface ITokens {
  access_token: string;
  id_token: string;
}

Examples

es modules (typescript)

class App extends React.Component {
  signIn() {
    ace.signIn().then(data => {
      if (data && data.access_token && data.id_token) {
        alert('already logged');
      }
    });
  }

  signUp() {
    ace.signUp();
  }

  logout() {
    ace.signOut();
  }

  getTokens() {
    ace
      .getTokens()
      .then(tokens => {
        alert(JSON.stringify(tokens, null, 2));
      })
      .catch(() => {
        alert('unathorized');
      });
  }

  render(): React.ReactNode {
    return (
      <div className="App">
        <header className="App-header">
          <div className="App-buttons">
            <button onClick={this.signIn.bind(this)}>Sign In</button>
            <button onClick={this.signUp.bind(this)}>Sign Up</button>
            <button onClick={this.logout.bind(this)}>Logout</button>
            <button onClick={this.getTokens.bind(this)}>getTokens</button>
          </div>
        </header>
      </div>
    );
  }
}

export default App;

CommonJs

const React = require('react');
const AceSdk = require('@ace-auth/sdk-browser');
// Example storage below.
const Storage = require('./storage');

const ace = new AceSdk.CreateClient({
  aceEndpoint: 'http://example.com',
  ownAceEndpoint: 'http://example.com',
  clientId: '<clientId>',
  signInRedirectUri: 'http://example.com/',
  signUpRedirectUri: 'http://example.com/',
  storage: new Storage(),
  locale: 'en-US',
});

class App extends React.Component {
  signIn() {
    ace.signIn().then(data => {
      if (data && data.access_token && data.id_token) {
        alert('already logged');
      }
    });
  }

  signUp() {
    ace.signUp();
  }

  logout() {
    ace.signOut();
  }

  getTokens() {
    ace
      .getTokens()
      .then(tokens => {
        alert(JSON.stringify(tokens, null, 2));
      })
      .catch(() => {
        alert('unathorized');
      });
  }

  render() {
    return (
      <div className="App">
        <header className="App-header">
          <div className="App-buttons">
            <button onClick={this.signIn.bind(this)}>Sign In</button>
            <button onClick={this.signUp.bind(this)}>Sign Up</button>
            <button onClick={this.logout.bind(this)}>Logout</button>
            <button onClick={this.getTokens.bind(this)}>getTokens</button>
          </div>
        </header>
      </div>
    );
  }
}

export default App;

global

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <script defer src="https://unpkg.com/@ace-auth/sdk-browser"></script>
    <script defer>
      window.ace = new window.AceSdk.CreateClient({
        aceEndpoint: '<%=aceUrl %>',
        ownAceEndpoint: location.origin,
        clientId: '<%=clientId %>',
        signInRedirectUri: location.origin,
        signUpRedirectUri: location.origin,
      });

      document.getElementById('login-button').onclick = function () {
        ace.signIn();
      };
      document.getElementById('signin-button').onclick = function () {
        ace.signUp({ referrer: '<userValidEmail>' });
      };
      document.getElementById('signout-button').onclick = function () {
        ace.signOut();
      };
    </script>
    <title>Simple App</title>
  </head>
  <body>
    <button id="login-button">Login</button> <button id="signin-button">Sign Up</button>
    <button id="signout-button">Sign Out</button>
  </body>
</html>

storage

export default class Storage {
  constructor() {
    this._store = {};
  }

  getItem(key) {
    return this._store[key] || null;
  }

  setItem(key, value) {
    this._store[key] = value.toString();
  }

  removeItem(key: string) {
    this._store[key] = null;
  }

  clear() {
    this._store = {};
  }
}

Ace authorisation SDK

Install

npm install @ace-auth/sdk-browser or yarn add @ace-auth/sdk-browser

Use cases

SDK uses umd module declaration. So you can use it such as node module system (commonjs), amd or globally in browser.

Parameters

NameDefault valueDescription
aceEndpointRequiredAce-ui endpoint.
autorefreshOptionalfalseRefresh tokens automatically. (onAceEndpoint should be set)
clientIdRequiredAce client ID.
debugOptionalfalseVerbose output
defaultRedirectUriOptionalDefault url for signIn, signUp, signOut methods
flagsOptional{}Available flags: { nosso: boolean }
localeOptional"en-US"Locale. Available locales 'en-US' or 'zh-CN'
onTokensOptionalCallback on new tokens received
ownAceEndpointOptionalClient ace backend endpoint.
signInRedirectUriOptionalUrl for signIn method.
signOutRedirectUriOptionalUrl for signOut method.
signUpRedirectUriOptionalUrl for signUp method.
storageOptionalwindow.localStorageStorage implementing window.localStorage interface.
storageKeyOptional"ace-sdk-tokens"Token's storage key

Methods

NameReturn valueDescription
clearStorage()Promise<void>Clears storage data
getTokens()Promise<ITokens>Returns client tokens
getOriginUrl()stringReturns origin url before signIn or signUp
getSignInUrl()stringReturns ace sign in url
getSignUpUrl(params?: { referrer?: string })stringReturns ace sign up url
on(event: string, cb: (tokens:ITokens)) => void)voidSubscribes to event
off(event: string, cb: (tokens:ITokens)) => void)voidUnsubscribes from event
setLocale(locale: 'en-US'|'zh-CN')'en-US'|'zh-CN'Sets locale for ACE class
signIn()Promise<ITokens|void>Starts sign in flow if anonymous user, returns tokens if logged in
signOut()Promise<void>Starts sign out flow
signUp(params?: { referrer?: string })voidStarts sign in flow

Interfaces

interface ITokens {
  access_token: string;
  id_token: string;
}

Examples

es modules (typescript)

class App extends React.Component {
  signIn() {
    ace.signIn().then(data => {
      if (data && data.access_token && data.id_token) {
        alert('already logged');
      }
    });
  }

  signUp() {
    ace.signUp();
  }

  logout() {
    ace.signOut();
  }

  getTokens() {
    ace
      .getTokens()
      .then(tokens => {
        alert(JSON.stringify(tokens, null, 2));
      })
      .catch(() => {
        alert('unathorized');
      });
  }

  render(): React.ReactNode {
    return (
      <div className="App">
        <header className="App-header">
          <div className="App-buttons">
            <button onClick={this.signIn.bind(this)}>Sign In</button>
            <button onClick={this.signUp.bind(this)}>Sign Up</button>
            <button onClick={this.logout.bind(this)}>Logout</button>
            <button onClick={this.getTokens.bind(this)}>getTokens</button>
          </div>
        </header>
      </div>
    );
  }
}

export default App;

CommonJs

const React = require('react');
const AceSdk = require('@ace-auth/sdk-browser');
// Example storage below.
const Storage = require('./storage');

const ace = new AceSdk.CreateClient({
  aceEndpoint: 'http://example.com',
  ownAceEndpoint: 'http://example.com',
  clientId: '<clientId>',
  signInRedirectUri: 'http://example.com/',
  signUpRedirectUri: 'http://example.com/',
  storage: new Storage(),
  locale: 'en-US',
});

class App extends React.Component {
  signIn() {
    ace.signIn().then(data => {
      if (data && data.access_token && data.id_token) {
        alert('already logged');
      }
    });
  }

  signUp() {
    ace.signUp();
  }

  logout() {
    ace.signOut();
  }

  getTokens() {
    ace
      .getTokens()
      .then(tokens => {
        alert(JSON.stringify(tokens, null, 2));
      })
      .catch(() => {
        alert('unathorized');
      });
  }

  render() {
    return (
      <div className="App">
        <header className="App-header">
          <div className="App-buttons">
            <button onClick={this.signIn.bind(this)}>Sign In</button>
            <button onClick={this.signUp.bind(this)}>Sign Up</button>
            <button onClick={this.logout.bind(this)}>Logout</button>
            <button onClick={this.getTokens.bind(this)}>getTokens</button>
          </div>
        </header>
      </div>
    );
  }
}

export default App;

global

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <script defer src="https://unpkg.com/@ace-auth/sdk-browser"></script>
    <script defer>
      window.ace = new window.AceSdk.CreateClient({
        aceEndpoint: '<%=aceUrl %>',
        ownAceEndpoint: location.origin,
        clientId: '<%=clientId %>',
        signInRedirectUri: location.origin,
        signUpRedirectUri: location.origin,
      });

      document.getElementById('login-button').onclick = function () {
        ace.signIn();
      };
      document.getElementById('signin-button').onclick = function () {
        ace.signUp({ referrer: '<userValidEmail>' });
      };
      document.getElementById('signout-button').onclick = function () {
        ace.signOut();
      };
    </script>
    <title>Simple App</title>
  </head>
  <body>
    <button id="login-button">Login</button> <button id="signin-button">Sign Up</button>
    <button id="signout-button">Sign Out</button>
  </body>
</html>

storage

export default class Storage {
  constructor() {
    this._store = {};
  }

  getItem(key) {
    return this._store[key] || null;
  }

  setItem(key, value) {
    this._store[key] = value.toString();
  }

  removeItem(key: string) {
    this._store[key] = null;
  }

  clear() {
    this._store = {};
  }
}
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文