인증
인증 (Authentication)
AI SDK RSC에서 서버 액션(Server Actions)을 사용해 데이터를 안전하게 제공하는 방법을 다루는 문서예요. 인증을 직접 처리해야 한다는 점을 꼭 기억하세요.
출처: 문서
본문
경고: AI SDK RSC는 현재 실험 단계(experimental)예요. 프로덕션에서는 AI SDK UI 사용을 권장해요. RSC에서 UI로 마이그레이션하는 방법은 마이그레이션 가이드를 참고하세요.
RSC API는 서버에서 스트리밍 값과 UI를 제공하기 위해 Server Actions를 광범위하게 사용해요.
Server Actions는 공개적이고 보호되지 않은 엔드포인트로 노출돼요. 따라서 Server Actions는 공개 API 엔드포인트처럼 취급하고, 데이터를 반환하기 전에 사용자가 해당 액션을 수행할 권한이 있는지 반드시 확인해야 해요.
'use server';
import { cookies } from 'next/headers';
import { createStreamableUI } from '@ai-sdk/rsc';
import { validateToken } from '../utils/auth';
export const getWeather = async () => {
const token = cookies().get('token');
if (!token || !validateToken(token)) {
return {
error: 'This action requires authentication',
};
}
const streamableDisplay = createStreamableUI(null);
streamableDisplay.update(<Skeleton />);
streamableDisplay.done(<Weather />);
return {
display: streamableDisplay.value,
};
};
위 예제처럼 쿠키에서 토큰을 읽어 인증 여부를 검증하고, 인증되지 않은 경우 오류를 반환하는 방식으로 서버 액션을 보호할 수 있어요.