@cemiar/auth-sdk 1.0.1 → 1.0.3

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.
@@ -1,9 +1,5 @@
1
1
  import { AxiosInstance } from "axios";
2
- import type { AuthTokens, CemiarAuthClientConfig, CemiarAuthClientInstance, CreateApiClientOptions, EmailCodeResponse, LogoutOptions } from "../shared/Types.js";
3
- export interface UserInfo {
4
- email: string;
5
- displayName?: string;
6
- }
2
+ import { AuthTokens, CemiarAuthClientInstance, CemiarAuthClientConfig, EmailCodeResponse, LogoutOptions, CreateApiClientOptions, UserInfo } from "../shared/Types.js";
7
3
  export declare class CemiarAuthClient implements CemiarAuthClientInstance {
8
4
  private readonly baseUrl;
9
5
  private readonly tenantId;
@@ -1,6 +1,6 @@
1
1
  import axios from "axios";
2
2
  import { createDefaultTokenStorage } from "./Storage.js";
3
- import { normalizeBaseUrl, isBrowser, tokenToClaim } from "../shared/Utils.js";
3
+ import { normalizeBaseUrl, isBrowser, tokenToClaimBrowser } from "../shared/Utils.js";
4
4
  const LOGIN_METHOD_STORAGE_KEY = "cemiar-auth-login-method";
5
5
  function parseAuds(auds) {
6
6
  return auds.map(a => a.trim()).filter(Boolean);
@@ -62,7 +62,7 @@ export class CemiarAuthClient {
62
62
  const accessToken = extractAccessToken(data);
63
63
  this.setAccessToken(accessToken);
64
64
  this.persistLoginMethod("emailCode");
65
- const claim = tokenToClaim(accessToken);
65
+ const claim = tokenToClaimBrowser(accessToken);
66
66
  return {
67
67
  email: claim.sub,
68
68
  displayName: claim.displayName
@@ -73,7 +73,7 @@ export class CemiarAuthClient {
73
73
  if (token) {
74
74
  this.setAccessToken(token);
75
75
  this.persistLoginMethod("microsoft");
76
- const claim = tokenToClaim(token);
76
+ const claim = tokenToClaimBrowser(token);
77
77
  return {
78
78
  email: claim.sub,
79
79
  displayName: claim.displayName
@@ -100,9 +100,12 @@ export class CemiarAuthClient {
100
100
  return { accessToken };
101
101
  }
102
102
  async logout(options = {}) {
103
- const loginMethod = this.getCurrentLoginMethod();
104
- if (options.clearToken !== false) {
105
- this.setAccessToken(null);
103
+ let loginMethod = options.method;
104
+ if (!loginMethod) {
105
+ loginMethod = this.getCurrentLoginMethod();
106
+ if (options.clearToken !== false) {
107
+ this.setAccessToken(null);
108
+ }
106
109
  }
107
110
  const redirectUrl = options.redirectUrl || this.logoutRedirectUrl;
108
111
  const logoutPath = loginMethod === "microsoft" ? "/microsoft/logout" : "/logout";
@@ -1,6 +1,10 @@
1
1
  import type { AxiosInstance, AxiosRequestConfig } from "axios";
2
2
  import type { Request, ResponseToolkit } from "@hapi/hapi";
3
- import { UserInfo } from "../browser/CemiarAuthClient.js";
3
+ export type LoginMethod = "emailCode" | "microsoft";
4
+ export interface UserInfo {
5
+ email: string;
6
+ displayName?: string;
7
+ }
4
8
  export interface TokenStorage {
5
9
  getToken(): string | null;
6
10
  setToken(token: string | null): void;
@@ -43,6 +47,7 @@ export interface CreateApiClientOptions {
43
47
  axiosConfig?: AxiosRequestConfig;
44
48
  }
45
49
  export interface LogoutOptions {
50
+ method?: LoginMethod;
46
51
  redirectUrl?: string;
47
52
  performRedirect?: boolean;
48
53
  clearToken?: boolean;
@@ -1,4 +1,5 @@
1
1
  import { Claim } from "./models/Claim.js";
2
2
  export declare function normalizeBaseUrl(url: string): string;
3
3
  export declare function tokenToClaim(token: string): Claim;
4
+ export declare function tokenToClaimBrowser(token: string): Claim;
4
5
  export declare const isBrowser: boolean;
@@ -10,4 +10,10 @@ export function tokenToClaim(token) {
10
10
  const parsed = JSON.parse(decoded);
11
11
  return parsed;
12
12
  }
13
+ export function tokenToClaimBrowser(token) {
14
+ const payload = token.split(".")[1];
15
+ const decoded = atob(payload);
16
+ const parsed = JSON.parse(decoded);
17
+ return parsed;
18
+ }
13
19
  export const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cemiar/auth-sdk",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "Cemiar Auth integration helpers for web apps and APIs.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",