0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Supabase初心者メモ】認証メソッド

Last updated at Posted at 2025-06-13

認証メソッドとは、アプリケーションにおいてユーザーの「本人確認(Authentication)」を行うために用意された一連の関数や API のことを指します。Supabaseにもいくつかの認証メソッドが用意されおり、簡単にユーザー登録やログイン、ログアウト、セッションの取得・検証をワンストップで実装できます。

supabase.auth.getSession()

現在のセッション情報(アクセストークン/リフレッシュトークン)を取得するためのメソッド

const { data, error } = await supabase.auth.getSession()

if (error) {
  console.error('GetSession Error:', error.message)
} else if (data.session) {
  console.log('Current access token:', data.session.access_token)
} else {
  console.log('No active session')
}

supabase.auth.getUser()

現在ログイン中のユーザー情報を取得するためのメソッド

const { data, error } = await supabase.auth.getUser()

if (error) {
  console.error('GetUser Error:', error.message)
} else if (data.user) {
  console.log('Logged in user:', data.user.id, data.user.email)
} else {
  console.log('No user logged in')
}

supabase.auth.signUp()

メール/パスワードで新規登録するためのメソッド

const { data, error } = await supabase.auth.signUp({
  email,
  password,
  options: {
    emailRedirectTo: 'https://your-app.com/welcome'
  }
})

if (error) {
  console.error('SignUp Error:', error.message)
} else {
  console.log('Confirmation email sent to:', data.user?.email)
}

supabase.auth.signInWithPassword()

メール/パスワードでサインインするためのメソッド

const { data, error } = await supabase.auth.signInWithPassword({
  email,
  password
})

if (error) {
  console.error('SignIn Error:', error.message)
} else {
  console.log('Signed in! Session:', data.session)
}

supabase.auth.signInWithOAuth()

Google/GitHub 等の OAuth プロバイダでサインインするためのメソッド

const { data, error } = await supabase.auth.signInWithOAuth({
  provider: 'google',
  options: {
    redirectTo: 'https://your-app.com/dashboard'
  }
})

if (error) {
  console.error('OAuth SignIn Error:', error.message)
} else {
  // data.url に OAuth 認証ページの URL が入るのでリダイレクト
  window.location.assign(data.url!)
}

supabase.auth.signOut()

サインアウト(セッション破棄)するためのメソッド

const { error } = await supabase.auth.signOut()
if (error) {
  console.error('SignOut Error:', error.message)
} else {
  console.log('Successfully signed out')
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?