0.6.0 • Published 2 years ago

@machinat/auth v0.6.0

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

Auth Module

This package is the underlying auth module of webview platform. You might want to use @machinat/webview unless you want to serve your own web service.

Install

npm install @machinat/core @machinat/http @machinat/auth
# or with yarn
yarn add @machinat/core @machinat/http @machinat/auth

Docs

Check the package reference.

Setup

Here is a simple example to protect your API with auth module:

Back-end

import Machinat, { makeFactoryProvider } from '@machinat/core';
import Http from '@machinat/http';
import Auth from '@machinat/auth';
// add the platforms and the authenticator you need
import { LineServerAuthenticator } from '@machinat/line/auth';

const app = Machinat.createApp({
  modules: [
    Http.initModule({ /* ... */ }),
    Auth.initModule({
      apiPath: '/auth',
      secret: 'xxx-xxx-xxx-xxx',
    }),
  ],
  services: [
    // provide authenticators of chat platforms
    { porvide: Auth.AuthenticatorList, withProvider: LineServerAuthenticator },
    { // a simple API route
      provide: Http.RequestRouteList,
      withProvider: makeFactoryProvider({ deps: [Auth.Controller] })(
        authController => ({
          path: '/myAPI',
          handler: async (req, res) => {
            // verify request authorization with AuthController
            const verifyResult = await authController.verifyAuth(req);

            if (verifyResult.ok) {
              res.end(JSON.string({ hello: verifyResult.auth.user.uid }));
            } else {
              res.writeHead(verifyResult.code);
              res.end(JSON.string({ error: verifyResult.reason }));
            }
          }
        })
      }),
    }
  ]
});

app.start();

Front-end

import AuthClient from '@machinat/auth/client';
import LineClientAuthenticator from '@machinat/line/auth/client';

(async function main () {
  const authClient = new AuthClient({
    platform: 'line',
    serverUrl: '/auth',
    authenticators: [new LineClientAuthenticator({ /* ... */ })],
  });

  const { token, context } = await authClient.auth();

  // use the bearer token to call your API
  const response = await fetch({
    url: '/myAPI',
    headers: { Authorization: `Bearer ${token}` },
  });
  // use response...
})();
0.6.0

2 years ago

0.6.0-canary.2

2 years ago

0.6.0-canary.0

2 years ago

0.6.0-beta.0

2 years ago

0.6.0-canary.3

2 years ago

0.6.0-canary.4

2 years ago

0.5.0-beta.10

2 years ago

0.5.0-beta.37

2 years ago

0.5.0-beta.15

2 years ago

0.5.0-beta.36

2 years ago

0.5.0-beta.14

2 years ago

0.5.0-beta.41

2 years ago

0.5.0-beta.20

2 years ago

0.5.0-beta.28

2 years ago

0.5.0

2 years ago

0.5.0-beta.7

2 years ago

0.5.0-beta.1

3 years ago

0.5.0-beta.0

3 years ago

0.4.1-alpha.46

3 years ago

0.4.1-alpha.44

3 years ago

0.4.1-alpha.41

3 years ago

0.4.1-alpha.34

3 years ago

0.4.1-alpha.33

3 years ago

0.4.1-alpha.29

3 years ago

0.4.1-alpha.28

3 years ago

0.4.1-alpha.27

3 years ago

0.4.1-alpha.13

3 years ago

0.4.1-alpha.12

3 years ago

0.4.1-alpha.11

3 years ago

0.4.1-alpha.8

3 years ago

0.4.1-alpha.3

3 years ago

0.4.1-alpha.1

3 years ago

0.4.1-alpha.0

3 years ago

0.4.0-beta.1

3 years ago

0.4.0-beta.0

3 years ago

0.3.0-beta.2

4 years ago

0.3.0-beta.1

4 years ago

0.3.0-beta.0

4 years ago

0.2.0-beta.0

4 years ago