Skip to content

Commit

Permalink
fix(nuxt): return undefined instead of throwing for fetch user info (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
wangsijie authored Jun 26, 2024
1 parent cad6d89 commit 3ba2e81
Show file tree
Hide file tree
Showing 4 changed files with 609 additions and 397 deletions.
9 changes: 9 additions & 0 deletions .changeset/honest-hornets-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@logto/nuxt": minor
---

return undefined when fetchUserInfo failed

When `fetchUserInfo` is set to `true`, `useLogto()` will call `fetchUserInfo` to get user info. If `fetchUserInfo` failed, it now returns `undefined` instead of throwing an error.

You can check the value of `userLogto()` to see if the user is authenticated.
1 change: 1 addition & 0 deletions packages/nuxt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"dependencies": {
"@logto/node": "workspace:^",
"@nuxt/kit": "^3.10.2",
"@silverhand/essentials": "^2.8.7",
"defu": "^6.1.4"
}
}
3 changes: 2 additions & 1 deletion packages/nuxt/src/runtime/server/event-handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import LogtoClient, { CookieStorage } from '@logto/node';
import { trySafe } from '@silverhand/essentials';
import { defineEventHandler, getRequestURL, getCookie, setCookie, sendRedirect } from 'h3';

import { defaults } from '../utils/constants';
Expand Down Expand Up @@ -76,6 +77,6 @@ export default defineEventHandler(async (event) => {
event.context.logtoClient = logto;
// eslint-disable-next-line @silverhand/fp/no-mutation
event.context.logtoUser = (await logto.isAuthenticated())
? await (fetchUserInfo ? logto.fetchUserInfo() : logto.getIdTokenClaims())
? await (fetchUserInfo ? trySafe(async () => logto.fetchUserInfo()) : logto.getIdTokenClaims())
: undefined;
});
Loading

0 comments on commit 3ba2e81

Please sign in to comment.