@rivium/chat 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +2 -0
- package/dist/modules/users.d.ts +31 -0
- package/dist/modules/users.js +38 -0
- package/dist/types.d.ts +19 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -79,6 +79,28 @@ const summary = await riviumChat.rooms.getUnreadSummary('user-1');
|
|
|
79
79
|
|
|
80
80
|
## API Reference
|
|
81
81
|
|
|
82
|
+
### Users — verified identity (recommended)
|
|
83
|
+
|
|
84
|
+
Your API key ships inside every app, so it cannot prove who a user is. Mint a
|
|
85
|
+
short-lived token here and hand it to the client SDK's `tokenProvider`:
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
// Express example — your own login protects the route
|
|
89
|
+
app.get('/chat-token', requireLogin, async (req, res) => {
|
|
90
|
+
const { token } = await riviumChat.users.createToken({ userId: req.user.id });
|
|
91
|
+
res.json({ token });
|
|
92
|
+
});
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
riviumChat.users.createToken({ userId, info?, ttl? }) // 1 h default, 24 h max
|
|
97
|
+
riviumChat.users.revokeTokens(userId) // on logout, password change, ban
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
`revokeTokens` invalidates every token issued to that user so far; it takes
|
|
101
|
+
effect within about 15 seconds across servers. Client SDKs then ask your
|
|
102
|
+
endpoint for a new token, so a signed-out user gets nothing.
|
|
103
|
+
|
|
82
104
|
### Rooms
|
|
83
105
|
|
|
84
106
|
```typescript
|
package/dist/index.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { Reactions } from './modules/reactions';
|
|
|
4
4
|
import { Pins } from './modules/pins';
|
|
5
5
|
import { Webhooks } from './modules/webhooks';
|
|
6
6
|
import { Tokens } from './modules/tokens';
|
|
7
|
+
import { Users } from './modules/users';
|
|
7
8
|
import { RiviumChatConfig } from './types';
|
|
8
9
|
export declare class RiviumChat {
|
|
9
10
|
private client;
|
|
@@ -19,6 +20,8 @@ export declare class RiviumChat {
|
|
|
19
20
|
webhooks: Webhooks;
|
|
20
21
|
/** Centrifugo connection tokens */
|
|
21
22
|
tokens: Tokens;
|
|
23
|
+
/** User tokens — verified identity for your client apps */
|
|
24
|
+
users: Users;
|
|
22
25
|
constructor(config: RiviumChatConfig);
|
|
23
26
|
}
|
|
24
27
|
export { RiviumChatError } from './client';
|
package/dist/index.js
CHANGED
|
@@ -22,6 +22,7 @@ const reactions_1 = require("./modules/reactions");
|
|
|
22
22
|
const pins_1 = require("./modules/pins");
|
|
23
23
|
const webhooks_1 = require("./modules/webhooks");
|
|
24
24
|
const tokens_1 = require("./modules/tokens");
|
|
25
|
+
const users_1 = require("./modules/users");
|
|
25
26
|
class RiviumChat {
|
|
26
27
|
constructor(config) {
|
|
27
28
|
this.client = new client_1.HttpClient(config);
|
|
@@ -31,6 +32,7 @@ class RiviumChat {
|
|
|
31
32
|
this.pins = new pins_1.Pins(this.client);
|
|
32
33
|
this.webhooks = new webhooks_1.Webhooks(this.client);
|
|
33
34
|
this.tokens = new tokens_1.Tokens(this.client);
|
|
35
|
+
this.users = new users_1.Users(this.client);
|
|
34
36
|
}
|
|
35
37
|
}
|
|
36
38
|
exports.RiviumChat = RiviumChat;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { HttpClient } from '../client';
|
|
2
|
+
import { CreateUserTokenOptions, UserToken, RevokeTokensResult } from '../types';
|
|
3
|
+
/**
|
|
4
|
+
* User tokens — how your app proves **who** the user is.
|
|
5
|
+
*
|
|
6
|
+
* Your API key ships inside every app, so it cannot identify a user. Mint a
|
|
7
|
+
* short-lived token here (server-side, with your server secret) and hand it to
|
|
8
|
+
* the client SDK's `tokenProvider`. The client SDKs refresh it automatically.
|
|
9
|
+
*/
|
|
10
|
+
export declare class Users {
|
|
11
|
+
private client;
|
|
12
|
+
constructor(client: HttpClient);
|
|
13
|
+
/**
|
|
14
|
+
* Creates a token for one of your users. Expires in 1 hour by default
|
|
15
|
+
* (`ttl` seconds, max 24 hours).
|
|
16
|
+
*
|
|
17
|
+
* ```ts
|
|
18
|
+
* app.get('/chat-token', requireLogin, async (req, res) => {
|
|
19
|
+
* const { token } = await chat.users.createToken({ userId: req.user.id });
|
|
20
|
+
* res.json({ token });
|
|
21
|
+
* });
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
createToken(options: CreateUserTokenOptions): Promise<UserToken>;
|
|
25
|
+
/**
|
|
26
|
+
* Invalidates every token issued to this user so far — call it on logout,
|
|
27
|
+
* password change or ban. Tokens already in the user's hands stop working
|
|
28
|
+
* within seconds, for REST and realtime alike.
|
|
29
|
+
*/
|
|
30
|
+
revokeTokens(userId: string): Promise<RevokeTokensResult>;
|
|
31
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Users = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* User tokens — how your app proves **who** the user is.
|
|
6
|
+
*
|
|
7
|
+
* Your API key ships inside every app, so it cannot identify a user. Mint a
|
|
8
|
+
* short-lived token here (server-side, with your server secret) and hand it to
|
|
9
|
+
* the client SDK's `tokenProvider`. The client SDKs refresh it automatically.
|
|
10
|
+
*/
|
|
11
|
+
class Users {
|
|
12
|
+
constructor(client) {
|
|
13
|
+
this.client = client;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Creates a token for one of your users. Expires in 1 hour by default
|
|
17
|
+
* (`ttl` seconds, max 24 hours).
|
|
18
|
+
*
|
|
19
|
+
* ```ts
|
|
20
|
+
* app.get('/chat-token', requireLogin, async (req, res) => {
|
|
21
|
+
* const { token } = await chat.users.createToken({ userId: req.user.id });
|
|
22
|
+
* res.json({ token });
|
|
23
|
+
* });
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
async createToken(options) {
|
|
27
|
+
return this.client.post('/api/v1/users/token', options);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Invalidates every token issued to this user so far — call it on logout,
|
|
31
|
+
* password change or ban. Tokens already in the user's hands stop working
|
|
32
|
+
* within seconds, for REST and realtime alike.
|
|
33
|
+
*/
|
|
34
|
+
async revokeTokens(userId) {
|
|
35
|
+
return this.client.post(`/api/v1/users/${encodeURIComponent(userId)}/revoke-tokens`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
exports.Users = Users;
|
package/dist/types.d.ts
CHANGED
|
@@ -160,3 +160,22 @@ export interface GetTokenOptions {
|
|
|
160
160
|
userId: string;
|
|
161
161
|
info?: Record<string, any>;
|
|
162
162
|
}
|
|
163
|
+
export interface CreateUserTokenOptions {
|
|
164
|
+
/** Your own id for the user the token is for. */
|
|
165
|
+
userId: string;
|
|
166
|
+
/** Public connection info (e.g. displayName) visible to the realtime layer. */
|
|
167
|
+
info?: Record<string, any>;
|
|
168
|
+
/** Lifetime in seconds. Default 3600 (1 h), max 86400 (24 h). */
|
|
169
|
+
ttl?: number;
|
|
170
|
+
}
|
|
171
|
+
export interface UserToken {
|
|
172
|
+
/** Hand this to the client SDK's tokenProvider. */
|
|
173
|
+
token: string;
|
|
174
|
+
/** ISO timestamp; the client SDKs refresh shortly before it. */
|
|
175
|
+
expiresAt: string;
|
|
176
|
+
userId: string;
|
|
177
|
+
}
|
|
178
|
+
export interface RevokeTokensResult {
|
|
179
|
+
/** Tokens issued at or before this moment are no longer accepted. */
|
|
180
|
+
revokedBefore: string;
|
|
181
|
+
}
|