@cemiar/auth-sdk 1.0.3 → 1.0.4

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 CHANGED
@@ -52,6 +52,19 @@ const jobs = await api.get("/api/jobs");
52
52
  authClient.attachInterceptors(existingAxiosInstance);
53
53
  ```
54
54
 
55
+ #### Handling the Microsoft callback
56
+
57
+ ```ts
58
+ const params = Object.fromEntries(new URLSearchParams(window.location.search));
59
+ const user = authClient.handleRedirectMicrosoftCallback(params);
60
+
61
+ if (user) {
62
+ history.replaceState(null, "", "/dashboard");
63
+ }
64
+ ```
65
+
66
+ `handleRedirectMicrosoftCallback` stores the returned access token, infers the login method for future logouts, and gives you the signed-in user details. Call it once on your redirect page, then clean up the query string if needed.
67
+
55
68
  #### Token storage hooks
56
69
 
57
70
  By default the client stores tokens in `localStorage`. Override storage to customize behaviour:
@@ -74,6 +87,20 @@ const authClient = createCemiarAuthClient({
74
87
  });
75
88
  ```
76
89
 
90
+ #### Logging out
91
+
92
+ ```ts
93
+ await authClient.logout();
94
+ ```
95
+
96
+ Logout automatically detects whether the user signed in with Microsoft or email and routes to the matching Cemiar Auth endpoint. Override behaviour with the optional flags:
97
+
98
+ - `redirectUrl`: override the post-logout redirect (defaults to `logoutRedirectUrl` from the constructor)
99
+ - `performRedirect`: set to `false` to stay on the page and only clear the server session
100
+ - `clearToken`: set to `false` to retain the locally cached access token
101
+
102
+ You can still specify `method: "microsoft" | "emailCode"` if you need to force a flow.
103
+
77
104
  ### Backend (Hapi)
78
105
 
79
106
  ```ts
@@ -20,7 +20,7 @@ export declare class CemiarAuthClient implements CemiarAuthClientInstance {
20
20
  handleRedirectMicrosoftCallback(routeQueryData: Record<string, string>): UserInfo | null;
21
21
  getMicrosoftLoginUrl(extraParams?: Record<string, string>): string;
22
22
  refreshToken(): Promise<AuthTokens>;
23
- logout(options?: LogoutOptions): Promise<void>;
23
+ logout(_options?: LogoutOptions): Promise<void>;
24
24
  createApiClient(options: CreateApiClientOptions): AxiosInstance;
25
25
  attachInterceptors(instance: AxiosInstance): AxiosInstance;
26
26
  private addAuthHeader;
@@ -18,7 +18,7 @@ export class CemiarAuthClient {
18
18
  var _a, _b, _c;
19
19
  this.storage = createDefaultTokenStorage();
20
20
  this.refreshPromise = null;
21
- this.baseUrl = normalizeBaseUrl(config.baseUrl || "http://localhost:3000/auth");
21
+ this.baseUrl = normalizeBaseUrl(config.baseUrl || "http://localhost:3000") + "/auth";
22
22
  this.tenantId = config.tenantId;
23
23
  this.auds = parseAuds(config.auds);
24
24
  this.redirectUrl =
@@ -99,29 +99,9 @@ export class CemiarAuthClient {
99
99
  this.setAccessToken(accessToken);
100
100
  return { accessToken };
101
101
  }
102
- async logout(options = {}) {
103
- let loginMethod = options.method;
104
- if (!loginMethod) {
105
- loginMethod = this.getCurrentLoginMethod();
106
- if (options.clearToken !== false) {
107
- this.setAccessToken(null);
108
- }
109
- }
110
- const redirectUrl = options.redirectUrl || this.logoutRedirectUrl;
111
- const logoutPath = loginMethod === "microsoft" ? "/microsoft/logout" : "/logout";
112
- const logoutUrl = redirectUrl
113
- ? `${this.baseUrl}${logoutPath}?redirectUrl=${encodeURIComponent(redirectUrl)}`
114
- : `${this.baseUrl}${logoutPath}`;
115
- try {
116
- if (options.performRedirect === false || !isBrowser) {
117
- await axios.post(`${this.baseUrl}/logout`, {}, { withCredentials: true });
118
- return;
119
- }
120
- window.location.href = logoutUrl;
121
- }
122
- catch {
123
- // ignore network errors during logout
124
- }
102
+ async logout(_options = {}) {
103
+ await axios.post(`${this.baseUrl}/logout`, {}, { withCredentials: true });
104
+ this.setAccessToken(null);
125
105
  }
126
106
  createApiClient(options) {
127
107
  var _a;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cemiar/auth-sdk",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "description": "Cemiar Auth integration helpers for web apps and APIs.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",