@darco2903/auth-api 2.0.4-beta.0

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 ADDED
@@ -0,0 +1,68 @@
1
+ # Authentication API
2
+
3
+ ## Description
4
+
5
+ This package provides an authentication API for Darco2903's services, including JWT token signing and verification. It includes both client and server utilities to facilitate the implementation of authentication across multiple services.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install @darco2903/auth-api
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ### Creating a client
16
+
17
+ ```ts
18
+ import { createClient } from "@darco2903/auth-api/client";
19
+
20
+ const SERVER_ORIGIN = "https://auth.example.com";
21
+ const authApi = createAuthClient(SERVER_ORIGIN);
22
+ ```
23
+
24
+ ### Signing a JWT token
25
+
26
+ ```ts
27
+ import {
28
+ JWTSign,
29
+ type AccessTokenData,
30
+ UserRole,
31
+ } from "@darco2903/auth-api/server";
32
+ import { Hour } from "@darco2903/secondthought";
33
+
34
+ const JWT_PRIVATE_KEY = "";
35
+ const tokenData: AccessTokenData = {
36
+ public_id: "user_public_id",
37
+ role: UserRole.User,
38
+ totp_required: false,
39
+ totp_verified: false,
40
+ };
41
+ const expiresIn = new Hour(1); // Token expires in 1 hour. Also accepts a number of seconds (e.g., 3600).
42
+
43
+ await JWTSign(tokenData, JWT_PRIVATE_KEY, expiresIn).match(
44
+ (signedToken) => {
45
+ console.log("JWT signing successful:", signedToken);
46
+ },
47
+ (err) => {
48
+ console.error(`JWT signing failed: ${err.message}`);
49
+ }
50
+ );
51
+ ```
52
+
53
+ ### Verifying a JWT token
54
+
55
+ ```ts
56
+ import { JWTVerify } from "@darco2903/auth-api/server";
57
+
58
+ const accessToken = "..."; // Your JWT token here
59
+
60
+ await JWTVerify(accessToken, JWT_PUBLIC_KEY).match(
61
+ (decodedToken) => {
62
+ console.log("JWT verification successful:", decodedToken);
63
+ },
64
+ (err) => {
65
+ console.error(`JWT verification failed: ${err.message}`);
66
+ }
67
+ );
68
+ ```