@@ -228,112 +228,48 @@ handler via the `frontend-sdk` (see next section).
228
228
### Using the Frontend-SDK
229
229
230
230
The following examples will cover some common use-cases for the ` hanko-frontend-sdk ` instance returned by
231
- the ` register() ` function, but please take a look into the
232
- [ frontend-sdk docs] ( https://docs.hanko.io/jsdoc/hanko-frontend-sdk/ ) for details.
233
-
234
- Note that you can create a ` hanko-frontend-sdk ` instance without having to register the web components as follows:
231
+ the ` register() ` function:
235
232
236
233
``` js
237
- import { Hanko } from " @teamhanko/hanko-elements" ;
238
-
239
- const hanko = new Hanko (" https://hanko.yourdomain.com" );
240
- ```
234
+ // Validate the current session
235
+ const session = await hanko .validateSession ();
236
+ console .log (" Session valid:" , session .is_valid , " Claims:" , session .claims );
241
237
242
- #### Events
238
+ // Retrieve the session token
239
+ const token = hanko .getSessionToken ();
240
+ console .log (" Session token:" , token);
243
241
244
- It is possible to bind callbacks to different custom events in use of the SDKs event listener functions.
245
- The callback function will be called when the event happens and an object will be passed in, containing event details.
242
+ // Fetch the user profile
243
+ const user = await hanko .getUser ();
244
+ console .log (" User profile:" , user .user_id , user .emails );
246
245
247
- ##### Session Created
246
+ // Log out the user
247
+ await hanko .logout ();
248
+ console .log (" User logged out" );
248
249
249
- Will be triggered after a session has been created and the user has completed possible additional steps (e.g. passkey
250
- registration, password recovery, etc.). It will also be triggered when the user logs in via another browser window. The
251
- event can be used to obtain the JWT claims.
252
-
253
- ``` js
254
- hanko .onSessionCreated ((sessionDetail ) => {
255
- // A new JWT has been issued.
256
- console .info (" Session created" , sessionDetail .claims );
250
+ // Handle session creation event
251
+ hanko .onSessionCreated (({ claims }) => {
252
+ console .log (" Session created with JWT claims:" , claims);
257
253
});
258
- ```
259
-
260
- ##### Session Expired
261
-
262
- Will be triggered when the session has expired, or when the session has been removed in another browser window, because
263
- the user has logged out, or deleted the account.
264
254
265
- ``` js
255
+ // Handle session expiration event
266
256
hanko .onSessionExpired (() => {
267
- // You can redirect the user to a login page or show the `<hanko-auth>` element, or to prompt the user to log in again.
268
- console .info (" Session expired" );
257
+ console .log (" Session expired, redirecting to login" );
269
258
});
270
- ```
271
259
272
- ##### User Logged Out
273
-
274
- Will be triggered, when the user actively logs out. In other browser windows, a "hanko-session-expired" event will be
275
- triggered at the same time.
276
-
277
- ``` js
260
+ // Handle user logout event
278
261
hanko .onUserLoggedOut (() => {
279
- // You can redirect the user to a login page or show the `<hanko-auth>` element.
280
- console .info (" User logged out" );
262
+ console .log (" User logged out successfully" );
281
263
});
282
- ```
283
-
284
- ##### User Deleted
285
264
286
- Will be triggered when the user has deleted the account. In other browser windows, a "hanko-session-expired" event will
287
- be triggered at the same time.
288
-
289
- ``` js
265
+ // Handle user account deletion event
290
266
hanko .onUserDeleted (() => {
291
- // You can redirect the user to a login page or show the `<hanko-auth>` element.
292
- console .info (" User has been deleted" );
267
+ console .log (" User account deleted" );
293
268
});
294
269
```
295
270
296
- To learn what else you can do, check out the
297
- [ custom-events] ( https://github.com/teamhanko/hanko/tree/main/frontend/frontend-sdk#custom-events ) README.
298
-
299
- #### Sessions
300
-
301
- Determine whether the user is logged in:
302
-
303
- ``` js
304
- hanko .session .isValid ();
305
- ```
306
-
307
- Getting the session details:
308
-
309
- ``` js
310
- const session = hanko .session .get ();
311
-
312
- if (session) {
313
- console .info (` userID: ${ session .userID } , jwt: ${ session .jwt } ` );
314
- }
315
- ```
316
-
317
- #### User Client
318
-
319
- The SDK contains several client classes to make the communication with the Hanko-API easier. Here some examples of
320
- things you might want to do:
321
-
322
- Getting the current user:
323
-
324
- ``` js
325
- const user = await hanko .user .getCurrent ();
326
- console .info (` id: ${ user .id } , email: ${ user .email } ` );
327
- ```
328
-
329
- Log out a user:
330
-
331
- ``` js
332
- await hanko .user .logout ();
333
- ```
334
-
335
- To learn how error handling works and what else you can do with SDK, take a look into
336
- the [ frontend-sdk docs] ( https://teamhanko.github.io/hanko/jsdoc/hanko-frontend-sdk/ ) .
271
+ Please take a look into the [ frontend-sdk docs] ( https://teamhanko.github.io/hanko/jsdoc/hanko-frontend-sdk/ ) , for
272
+ further details.
337
273
338
274
## UI Customization
339
275
0 commit comments