Skip to content

Commit

Permalink
feat: updated parse url function to handle supabase error message and…
Browse files Browse the repository at this point in the history
… updated icons.
  • Loading branch information
jerrythomas committed Jan 10, 2024
1 parent d6d5b41 commit 4a3aa14
Show file tree
Hide file tree
Showing 14 changed files with 87 additions and 43 deletions.
2 changes: 1 addition & 1 deletion adapters/supabase/src/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function parseUrlError(url) {
isError: true,
status: result.error_code,
name: result.error,
message: result.error_description.replaceAll('+', '')
message: result.error_description
}
}
return error
Expand Down
43 changes: 24 additions & 19 deletions kavach.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions packages/core/spec/helper.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ describe('Helper functions', () => {
expect(params).toEqual({})
params = urlHashToParams('https://localhost?#foo=bar&token=baz')
expect(params).toEqual({ foo: 'bar', token: 'baz' })
params = urlHashToParams('https://localhost?#foo=&token=baz')
expect(params).toEqual({ foo: '', token: 'baz' })
params = urlHashToParams('https://localhost?#foo==&token=baz')
expect(params).toEqual({ foo: '=', token: 'baz' })
params = urlHashToParams(
'https://kavach.vercel.app/auth#error=server_error&error_code=500&error_description=Unable+to+exchange+external+code%253A+0.ARIA32EImwmt30e01JoAgoq58HuyW2kf3VpEpwXcKlrNwQrWAAA.AgABAAIAAAAmoFfGtYxvRrNriQdPKIZ-AgDs_wUA9P_H4zoxyzaAya3YcrENNUg-9epulTlB3BHZMxNpyiPttjTwudfhwDVQP95G4ELGgnII_zNli49Lf7Jyi2T_pSr6DgED5FzuVoeDiFS26qn1VUu6zJuZaZv5tL89KP_nre3d16tmkGv5MVaJdmCeqzyPVW5igokRrjJ9phbO6QDGZSlbgdLHTDou2sbx9KliTPN4Zg9ZrApj42IvShpdpBIOQDc-N-YDjXei5JD4Jg5D3JVLIMwG9uoF_QvEWFM8rHxZGsIr0F_3vvPsGqwD6NK6E8OWeLHCXie5nBywfWmIW-7js3feSfa8TnkKASyNKT6hF90A2olr2vAh5KJby1TSED8qUqbizMXYJw8iq65u1JkDT43PEibMKlkgiqVWsRE02GItCrSZ3GKF8mgYAnvU8-fbFXHQlroWn66gT_cSW-glSqAYxgSptJdRm8akMF5eOEatrcOAN3mogWbpN8xTHn9tIplAR7RE3srCY36xCgOTiGZK7oomVsjg27GhpSUk7Omh73JLTEN5-jGNEeKhbY0TxX7nY88J67tko-c4Ek2Ktp33UI1XEr0mVOZJ9N2_4vl22xumxFwLKAc-RmZO9OKNHmDAR-MOOS_6KLAgYQWaykvNZqniFeow4sUs2ZPcETZ3E3OQ7u_W84lzRGq73v6WqGzWY8qW_7RDIQlqbNFIhBL-opYQrOtImI6men3LVFhMgxi7p0g'
)
expect(params).toEqual({
error: 'server_error',
error_code: '500',
error_description: 'Unable to exchange external code'
})
})
it('Should identify url with auth params', () => {
expect(hasAuthParams('/auth#access_token=abcd')).toBeTruthy()
Expand Down
29 changes: 27 additions & 2 deletions packages/core/src/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,39 @@ export function hasAuthParams(url) {
export function urlHashToParams(url) {
const [, hash] = url.split('#')
if (hash && hash.length) {
return hash
let result = hash
.split('&')
.map((kv) => kv.split('='))
.map((kv) => extractKeyValuePair(kv))
.reduce((acc, kv) => ({ ...acc, [kv[0]]: kv[1] }), {})

return result
}
return {}
}

/**
* Extracts key value pairs from the data string where key and value are separated by '='.
*
* @param {string} data
* @param {string} separator - separator between key and value (default '=')
* @returns {[string, string]} [key, value] pair
*/
export function extractKeyValuePair(data, separator = '=') {
let values = data.split(separator)
if (values.length === 1) {
values.push('')
} else if (values.length > 2) {
values = [values[0], values.slice(1).join(separator)]
} else {
values[1] = decodeURIComponent(values[1])
if (values[1].includes('%')) {
values[1] = values[1]
.substring(0, values[1].indexOf('%'))
.replace(/\+/g, ' ')
}
}
return [values[0], values[1]]
}
/**
* Generates a redirect response using the provided inputs
*
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/src/kavach.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export const authStatus = writable()
export function createKavach(adapter, options) {
const deflector = createDeflector(options)
const logger = options?.logger ?? zeroLogger
// const invalidate = options?.invalidate ?? (() => {})
const invalidateAll = options?.invalidateAll ?? (() => {})

const signIn = async (credentials) => {
Expand Down Expand Up @@ -47,6 +46,7 @@ export function createKavach(adapter, options) {
adapter.onAuthChange(async (event, session) => {
if (url) {
authStatus.set(adapter.parseUrlError(url))
console.log('onAuthChange, message in url', adapter.parseUrlError(url))
}

const result = await fetch(deflector.page.session, {
Expand Down
2 changes: 2 additions & 0 deletions sites/skeleton/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ node_modules
!.env.example
vite.config.js.timestamp-*
vite.config.ts.timestamp-*
favicon_io
favicon_io.zip
Binary file modified sites/skeleton/static/android-chrome-192x192.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified sites/skeleton/static/android-chrome-512x512.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified sites/skeleton/static/apple-touch-icon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified sites/skeleton/static/favicon-16x16.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified sites/skeleton/static/favicon-32x32.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified sites/skeleton/static/favicon.ico
Binary file not shown.
Binary file removed sites/skeleton/static/favicon.png
Binary file not shown.

0 comments on commit 4a3aa14

Please sign in to comment.