0.8.0 • Published 2 years ago
@sociably/auth v0.8.0
Auth Module
This package is the underlying auth module of webview platform. You might want
to use @sociably/webview
unless you want to serve your own web service.
Install
npm install @sociably/core @sociably/http @sociably/auth
# or with yarn
yarn add @sociably/core @sociably/http @sociably/auth
Docs
Check the package reference.
Setup
Here is a simple example to protect your API with auth module:
Back-end
import Sociably, { makeFactoryProvider } from '@sociably/core';
import Http from '@sociably/http';
import Auth from '@sociably/auth';
// add the platforms and the authenticator you need
import { LineServerAuthenticator } from '@sociably/line/webview';
const app = Sociably.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 '@sociably/auth/client';
import LineClientAuthenticator from '@sociably/line/webview/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.9.0-alpha.0
2 years ago
0.9.0-alpha.2
2 years ago
0.9.0-alpha.3
2 years ago
0.9.0-alpha.4
2 years ago
0.9.0-alpha.26
2 years ago
0.9.0-alpha.13
2 years ago
0.8.0
2 years ago
0.8.0-alpha.28
2 years ago
0.8.0-alpha.44
2 years ago
0.8.0-alpha.43
2 years ago
0.8.0-alpha.32
2 years ago
0.8.0-alpha.10
2 years ago
0.8.0-alpha.8
2 years ago
0.8.0-alpha.5
2 years ago
0.8.0-alpha.0
3 years ago
0.8.0-alpha.1
3 years ago
0.7.1
3 years ago
0.7.0
3 years ago