Files
supabase/apps/studio/lib/gotrue.ts
T
4278bd4ec1 fix: bump auth-js version to v2.67.3 (#31217)
* fix: bump auth-js version to v2.67.3

* chore: add supabase/auth and supabase/dashboard as codeowners

* Revert "chore: add supabase/auth and supabase/dashboard as codeowners"

This reverts commit 57fc5776ba.

* bump supabase-js version

* import user type from single gotrue

* fix auth-js version in studio

* remove auth-js dep in studio

* fix ts

* use catalog versions

* fix ts

* Bump realtime-js version to avoid having 2 versions.

* Add @supabase/realtime-js to the pnpm catalog.

* upgrade realtime-js

---------

Co-authored-by: Alaister Young <a@alaisteryoung.com>
Co-authored-by: Ivan Vasilov <vasilov.ivan@gmail.com>
2025-01-17 14:54:21 +08:00

75 lines
2.2 KiB
TypeScript

import { getAccessToken, gotrueClient, type User } from 'common'
export const auth = gotrueClient
export { getAccessToken }
export const getAuthUser = async (token: String): Promise<any> => {
try {
const {
data: { user },
error,
} = await auth.getUser(token.replace('Bearer ', ''))
if (error) throw error
return { user, error: null }
} catch (err) {
console.error(err)
return { user: null, error: err }
}
}
export const getAuth0Id = (provider: String, providerId: String): String => {
return `${provider}|${providerId}`
}
export const getIdentity = (gotrueUser: User) => {
try {
if (gotrueUser !== undefined && gotrueUser.identities !== undefined) {
return { identity: gotrueUser.identities[0], error: null }
}
throw 'Missing identity'
} catch (err) {
return { identity: null, error: err }
}
}
/**
* Transfers the search params from the current location path to a newly built path
*/
export const buildPathWithParams = (pathname: string) => {
const searchParams = new URLSearchParams(location.search)
return `${pathname}?${searchParams.toString()}`
}
export const getReturnToPath = (fallback = '/projects') => {
const searchParams = new URLSearchParams(location.search)
let returnTo = searchParams.get('returnTo') ?? fallback
if (process.env.NEXT_PUBLIC_BASE_PATH) {
returnTo = returnTo.replace(process.env.NEXT_PUBLIC_BASE_PATH, '')
}
searchParams.delete('returnTo')
const remainingSearchParams = searchParams.toString()
let validReturnTo
// only allow returning to internal pages. e.g. /projects
try {
// if returnTo is a relative path, this will throw an error
new URL(returnTo)
// if no error, returnTo is a valid URL and NOT an internal page
validReturnTo = fallback
} catch (_) {
// check returnTo doesn't try trick the browser to redirect
// don't try sanitize, it is a losing battle. Go to fallback
// disallow anything that starts with /non-word-char+/ or non-char+/
const pattern = /^\/?[\W]+\//
validReturnTo = pattern.test(returnTo) ? fallback : returnTo
}
return validReturnTo + (remainingSearchParams ? `?${remainingSearchParams}` : '')
}