-
Notifications
You must be signed in to change notification settings - Fork 5
feat(wallet-sdk): auth & user slice (step 5) #1166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,19 @@ | ||
| import { jwtDecode } from 'jwt-decode'; | ||
| import { safeJwtDecode } from '@agicash/utils'; | ||
|
|
||
| /** Check if the user is logged in by verifying localStorage tokens. */ | ||
| /** | ||
| * Check if the user is logged in by verifying localStorage tokens. A corrupt | ||
| * stored token counts as logged out — this feeds the DB client's token | ||
| * getter, so it must never throw into every query. | ||
| * | ||
| * Temporary leak: reads Open Secret's storage keys directly until the late | ||
| * SDK-migration steps retire the web's own DB client. | ||
| */ | ||
| export const isLoggedIn = (): boolean => { | ||
| const accessToken = window.localStorage.getItem('access_token'); | ||
| const refreshToken = window.localStorage.getItem('refresh_token'); | ||
| if (!accessToken || !refreshToken) { | ||
| return false; | ||
| } | ||
| const decoded = jwtDecode(refreshToken); | ||
| return !!decoded.exp && decoded.exp * 1000 > Date.now(); | ||
| const decoded = safeJwtDecode(refreshToken); | ||
| return !!decoded?.exp && decoded.exp * 1000 > Date.now(); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import { browserStorage } from '@agicash/opensecret'; | ||
| import { AgicashSdk } from '@agicash/wallet-sdk'; | ||
| import { breezApiKey } from '~/lib/breez'; | ||
|
|
||
| const getSupabaseUrl = () => { | ||
| const supabaseUrl = import.meta.env.VITE_SUPABASE_URL ?? ''; | ||
| if (!supabaseUrl) { | ||
| throw new Error('VITE_SUPABASE_URL is not set'); | ||
| } | ||
|
|
||
| if ( | ||
| supabaseUrl.includes('127.0.0.1') && | ||
| typeof window !== 'undefined' && | ||
| window.location.protocol === 'https:' && | ||
| (window.location.hostname.endsWith('.local') || | ||
| window.location.hostname.startsWith('192.168.') || | ||
| window.location.hostname.startsWith('10.') || | ||
| /^172\.(1[6-9]|2[0-9]|3[0-1])\./.test(window.location.hostname)) | ||
| ) { | ||
| return supabaseUrl.replace('127.0.0.1', window.location.hostname); | ||
| } | ||
|
|
||
| return supabaseUrl; | ||
| }; | ||
|
|
||
| export const supabaseUrl = getSupabaseUrl(); | ||
|
|
||
| export const supabaseAnonKey = import.meta.env.VITE_SUPABASE_ANON_KEY ?? ''; | ||
| if (!supabaseAnonKey) { | ||
| throw new Error('VITE_SUPABASE_ANON_KEY is not set'); | ||
| } | ||
|
|
||
| const openSecretApiUrl = import.meta.env.VITE_OPEN_SECRET_API_URL ?? ''; | ||
| if (!openSecretApiUrl) { | ||
| throw new Error('VITE_OPEN_SECRET_API_URL is not set'); | ||
| } | ||
|
|
||
| const openSecretClientId = import.meta.env.VITE_OPEN_SECRET_CLIENT_ID ?? ''; | ||
| if (!openSecretClientId) { | ||
| throw new Error('VITE_OPEN_SECRET_CLIENT_ID is not set'); | ||
| } | ||
|
|
||
| // console.debug(message, undefined) prints a trailing "undefined". Most calls | ||
| // pass no meta, so the second argument is omitted unless meta is provided. | ||
| const consoleLogger = { | ||
| debug: (message: string, meta?: unknown) => | ||
| meta === undefined ? console.debug(message) : console.debug(message, meta), | ||
|
jbojcic1 marked this conversation as resolved.
|
||
| info: (message: string, meta?: unknown) => | ||
| meta === undefined ? console.info(message) : console.info(message, meta), | ||
| warn: (message: string, meta?: unknown) => | ||
| meta === undefined ? console.warn(message) : console.warn(message, meta), | ||
| error: (message: string, meta?: unknown) => | ||
| meta === undefined ? console.error(message) : console.error(message, meta), | ||
| }; | ||
|
|
||
| export const sdk = AgicashSdk.create({ | ||
| db: { | ||
| url: supabaseUrl, | ||
| anonKey: supabaseAnonKey, | ||
| }, | ||
| auth: { | ||
| apiUrl: openSecretApiUrl, | ||
| clientId: openSecretClientId, | ||
| storage: browserStorage, | ||
| // e2e bridge: the Playwright fixture arms window.getMockPassword; in | ||
| // production it's absent, so this resolves null and the SDK generates. | ||
| generateGuestPassword: async () => | ||
| (await window.getMockPassword?.()) ?? null, | ||
| }, | ||
| spark: { | ||
| breezApiKey, | ||
| network: 'MAINNET', | ||
| }, | ||
| lightningAddressDomain: window.location.host, | ||
| logger: consoleLogger, | ||
| }); | ||
|
|
||
| if (import.meta.hot) { | ||
| // A hot reload of this module constructs a second SDK; dispose the old one | ||
| // so its expiry timer doesn't leak. Returning the promise makes Vite await | ||
|
jbojcic1 marked this conversation as resolved.
|
||
| // the teardown before evaluating the replacement module. | ||
| import.meta.hot.dispose(() => sdk.dispose()); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.