|
57 | 57 | *
|
58 | 58 | * Additional helper functions are available to help you manage a session lifecycle and the user experience.
|
59 | 59 | *
|
60 |
| - * *```ts |
| 60 | + *```ts |
61 | 61 | * // Check if authorized or created from existing session string
|
62 | 62 | * didsession.hasSession
|
63 | 63 | *
|
|
69 | 69 | *
|
70 | 70 | * // Check number of seconds till expiration, may want to re auth user at a time before expiration
|
71 | 71 | * didsession.expiresInSecs
|
| 72 | + * ``` |
| 73 | + * |
| 74 | + * ### Typical usage pattern |
| 75 | + * |
| 76 | + * A typical pattern is to store a serialized session in local storage and load on use if available. Then |
| 77 | + * check that a session is still valid before making writes. |
| 78 | + * |
| 79 | + * ```ts |
| 80 | + * import { DIDSession } from '@glazed/did-session' |
| 81 | + * import { EthereumAuthProvider } from '@ceramicnetwork/blockchain-utils-linking' |
72 | 82 | *
|
| 83 | + * const ethProvider = // import/get your web3 eth provider |
| 84 | + * const addresses = await ethProvider.enable() |
| 85 | + * const authProvider = new EthereumAuthProvider(ethProvider, addresses[0]) |
| 86 | + * |
| 87 | + * const loadSession = async(authProvider: EthereumAuthProvider):Promise<DIDSession> => { |
| 88 | + * const sessionStr = localStorage.getItem('didsession') |
| 89 | + * let session |
| 90 | + * |
| 91 | + * if (sessionStr) { |
| 92 | + * session = await DIDSession.fromSession(sessionStr, authProvider) |
| 93 | + * } |
| 94 | + * |
| 95 | + * if (!session || (session.hasSession && session.isExpired)) { |
| 96 | + * session = new DIDSession({ authProvider }) |
| 97 | + * session.authorize() |
| 98 | + * localStorage.setItem('didsession', session.serialize()) |
| 99 | + * } |
| 100 | + * |
| 101 | + * return session |
| 102 | + * } |
| 103 | + * |
| 104 | + * const session = await loadSession(authProvider) |
| 105 | + * const ceramic = new CeramicClient() |
| 106 | + * ceramic.did = session.getDID() |
| 107 | + * |
| 108 | + * // pass ceramic instance where needed, ie glaze |
| 109 | + * // ... |
| 110 | + * |
| 111 | + * // before ceramic writes, check if session is still valid, if expired, create new |
| 112 | + * if (session.isExpired) { |
| 113 | + * const session = loadSession(authProvider) |
| 114 | + * ceramic.did = session.getDID() |
| 115 | + * } |
| 116 | + * |
| 117 | + * // continue to write |
| 118 | + * ``` |
| 119 | + * |
73 | 120 | * @module did-session
|
74 | 121 | */
|
75 | 122 |
|
@@ -106,19 +153,19 @@ export async function createDIDKey(seed?: Uint8Array): Promise<DID> {
|
106 | 153 | return didKey
|
107 | 154 | }
|
108 | 155 |
|
109 |
| -export function JSONToBase64url(object: Record<string, any>): string { |
| 156 | +function JSONToBase64url(object: Record<string, any>): string { |
110 | 157 | return u8a.toString(u8a.fromString(JSON.stringify(object)), 'base64url')
|
111 | 158 | }
|
112 | 159 |
|
113 |
| -export function base64urlToJSON(s: string): Record<string, any> { |
| 160 | +function base64urlToJSON(s: string): Record<string, any> { |
114 | 161 | return JSON.parse(u8a.toString(u8a.fromString(s, 'base64url'))) as Record<string, any>
|
115 | 162 | }
|
116 | 163 |
|
117 |
| -export function bytesToBase64(b: Uint8Array): string { |
| 164 | +function bytesToBase64(b: Uint8Array): string { |
118 | 165 | return u8a.toString(b, 'base64pad')
|
119 | 166 | }
|
120 | 167 |
|
121 |
| -export function base64ToBytes(s: string): Uint8Array { |
| 168 | +function base64ToBytes(s: string): Uint8Array { |
122 | 169 | return u8a.fromString(s, 'base64pad')
|
123 | 170 | }
|
124 | 171 |
|
|
0 commit comments