JavaScript: 通过OAuth登录用户

通过第三方提供商登录现有用户。 此方法支持PKCE流程。

Parameters

Examples

通过第三方提供商登录

const { data, error } = await supabase.auth.signInWithOAuth({
  provider: 'github'
})

通过第三方提供商进行重定向登录

const { data, error } = await supabase.auth.signInWithOAuth({
  provider: 'github',
  options: {
    redirectTo: 'https://example.com/welcome'
  }
})

使用作用域和访问提供商令牌登录

// 调用createClient后立即注册此回调!
// 由于signInWithOAuth会触发重定向,需要通过回调获取提供商令牌
supabase.auth.onAuthStateChange((event, session) => {
  if (session && session.provider_token) {
    window.localStorage.setItem('oauth_provider_token', session.provider_token)
  }

  if (session && session.provider_refresh_token) {
    window.localStorage.setItem('oauth_provider_refresh_token', session.provider_refresh_token)
  }

  if (event === 'SIGNED_OUT') {
    window.localStorage.removeItem('oauth_provider_token')
    window.localStorage.removeItem('oauth_provider_refresh_token')
  }
})

// 在GitHub登录按钮调用此方法,以请求提升的权限范围启动OAuth流程
await supabase.auth.signInWithOAuth({
  provider: 'github',
  options: {
    scopes: 'repo gist notifications'
  }
})