Skip to content

Commit

Permalink
feat: add signInAnonymously method
Browse files Browse the repository at this point in the history
  • Loading branch information
kangmingtay committed Mar 4, 2024
1 parent 15c7c82 commit 525c008
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/GoTrueClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ import type {
AuthFlowType,
LockFunc,
UserIdentity,
WeakPassword,
SignInAnonymouslyCredentials,
} from './lib/types'

polyfillGlobalThis() // Make "globalThis" available
Expand Down Expand Up @@ -361,6 +361,41 @@ export default class GoTrueClient {
}
}

async signInAnonymously(credentials?: SignInAnonymouslyCredentials): Promise<AuthResponse> {
try {
await this._removeSession()

const res = await _request(this.fetch, 'POST', `${this.url}/signup`, {
headers: this.headers,
body: {
data: credentials?.options?.data ?? {},
gotrue_meta_security: { captcha_token: credentials?.options?.captchaToken },
},
xform: _sessionResponse,
})
const { data, error } = res

if (error || !data) {
return { data: { user: null, session: null }, error: error }
}
const session: Session | null = data.session
const user: User | null = data.user

if (data.session) {
await this._saveSession(data.session)
await this._notifyAllSubscribers('SIGNED_IN', session)
}

return { data: { user, session }, error: null }
} catch (error) {
if (isAuthError(error)) {
return { data: { user: null, session: null }, error }
}

throw error
}
}

/**
* Creates a new user.
*
Expand Down
7 changes: 7 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,13 @@ export interface UpdatableFactorAttributes {
friendlyName: string
}

export type SignInAnonymouslyCredentials = {
options?: {
data?: object
captchaToken?: string
}
}

export type SignUpWithPasswordCredentials =
| {
/** The user's email address. */
Expand Down

0 comments on commit 525c008

Please sign in to comment.