본문 바로가기
🚀 Backend

[Swift + Supabase] OAuth - Sign in with Apple

by dev.py 2025. 4. 28.

Swift에서 Supabase 를 활용해서 Apple OAuth 구현 방법(삽질 후기)

 

1. Apple Developer Console 설정

  • Service ID 생성
    • Apple Developer -> Certificates, Identifiers & Profiles
    • Sign in with Apple 활성화
  • Key 생성 
    • Certificates -> Key + 버튼
    • "Sign in with Apple" 
    • .p8 파일 다운로드
  • Redirect URI 등록
    • 2.Supabase 에서 만들어진 redirect URL 추가

 

.p8 파일에서 Secret Key 뽑기

secret Key 를 내가 뽑아야한다

# Python
import jwt
import datetime

TEAM_ID = ''
CLIENT_ID = ''
KEY_ID = ''

with open("AuthKey_xxxxx.p8") as f:
    private_key = f.read()

headers = {
    "kid": KEY_ID,
    "alg": "ES256"
}

payload = {
    "iss": TEAM_ID,
    "iat": int(datetime.datetime.now().timestamp()),
    "exp": int((datetime.datetime.now() + datetime.timedelta(days=180)).timestamp()),
    "aud": "https://appleid.apple.com",
    "sub": CLIENT_ID
}

token = jwt.encode(payload, private_key, algorithm="ES256", headers=headers)
print(token)

 

2. Supabase

 

사전 작업과 준비물

  • Apple Developer Console 설정
  • AuthKey_xxxx.p8에서 Secret Key 뽑기

 

여기서 챙겨서 다른곳에 쓸거

  • 프로젝트 URL
  • Aron Key
  • Callback URL

 

1. Apple OAuth 설정

좌측 메뉴에서 [Authentication] - [Sign In / Up] 에서 Apple OAuth를 활성화 한다.

 

 

활성화를 위해선 Client IDs와 Secret key를 입력해준다.

 

여기서 Callback URL를 챙겨주자

 

 

2. Project URL, API Key

[Project Settings] - [Data API]

 

 

 

Project URL, Proejct API Keys

2. Swift Client

공식 문서

 

Login with Apple | Supabase Docs

Use Sign in with Apple with Supabase

supabase.com

 

 

import SwiftUI
import AuthenticationServices
import Supabase // supabase 패키지 설치해서 넣어준다.
struct SignInView: View {

    // 위에서 챙긴 URL과 supabase 키를 넣어준다.
    let client = SupabaseClient(supabaseURL: URL(string: "your url")!, supabaseKey: "your anon key")

    
    var body: some View {
      SignInWithAppleButton { request in
        request.requestedScopes = [.email, .fullName]
      } onCompletion: { result in
        Task {
          do {
            guard let credential = try result.get().credential as? ASAuthorizationAppleIDCredential
            else {
              return
            }
            guard let idToken = credential.identityToken
              .flatMap({ String(data: $0, encoding: .utf8) })
            else {
              return
            }
              try await client.auth.signInWithIdToken(
              credentials: .init(
                provider: .apple,
                idToken: idToken 
              )
            )
          } catch {
            dump(error)
          }
        }
      }
      .fixedSize()
    }
}

 

 

위의 코드를 통해 Apple OAuth를 진행하면, Supabase - [Authentication] 테이블에

 

유저가 추가된다.

'🚀 Backend' 카테고리의 다른 글

[Database] CAP 이론 (CAP Theorem)  (0) 2025.05.12
[FastAPI] N+1 Problem  (0) 2025.05.07
[FastAPI] Middleware 미들웨어 (CORS, Logging)  (0) 2025.04.20
[FastAPI] SSE (Server-Sent Events)  (0) 2025.04.16
[FastAPI] SQLModel  (0) 2025.03.14