Skip to content

Commit a1897bb

Browse files
authored
build(did-session): docs and release (#138)
1 parent 4b6327d commit a1897bb

File tree

2 files changed

+53
-6
lines changed

2 files changed

+53
-6
lines changed

packages/did-session/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@glazed/did-session",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"author": "3Box Labs",
55
"license": "(Apache-2.0 OR MIT)",
66
"homepage": "https://github.com/ceramicstudio/js-glaze#readme",

packages/did-session/src/index.ts

+52-5
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
*
5858
* Additional helper functions are available to help you manage a session lifecycle and the user experience.
5959
*
60-
* *```ts
60+
*```ts
6161
* // Check if authorized or created from existing session string
6262
* didsession.hasSession
6363
*
@@ -69,7 +69,54 @@
6969
*
7070
* // Check number of seconds till expiration, may want to re auth user at a time before expiration
7171
* 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'
7282
*
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+
*
73120
* @module did-session
74121
*/
75122

@@ -106,19 +153,19 @@ export async function createDIDKey(seed?: Uint8Array): Promise<DID> {
106153
return didKey
107154
}
108155

109-
export function JSONToBase64url(object: Record<string, any>): string {
156+
function JSONToBase64url(object: Record<string, any>): string {
110157
return u8a.toString(u8a.fromString(JSON.stringify(object)), 'base64url')
111158
}
112159

113-
export function base64urlToJSON(s: string): Record<string, any> {
160+
function base64urlToJSON(s: string): Record<string, any> {
114161
return JSON.parse(u8a.toString(u8a.fromString(s, 'base64url'))) as Record<string, any>
115162
}
116163

117-
export function bytesToBase64(b: Uint8Array): string {
164+
function bytesToBase64(b: Uint8Array): string {
118165
return u8a.toString(b, 'base64pad')
119166
}
120167

121-
export function base64ToBytes(s: string): Uint8Array {
168+
function base64ToBytes(s: string): Uint8Array {
122169
return u8a.fromString(s, 'base64pad')
123170
}
124171

0 commit comments

Comments
 (0)