@lemondouble/lemon-auth 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +110 -0
- package/package.json +5 -1
package/README.md
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
# @lemondouble/lemon-auth
|
|
2
|
+
|
|
3
|
+
`*.lemondouble.com` 서비스를 위한 인증 라이브러리. Next.js 16+ 전용.
|
|
4
|
+
|
|
5
|
+
`auth.lemondouble.com` 인증 서버와 연동하여 JWT 검증, 토큰 갱신, 로그인/로그아웃을 처리합니다.
|
|
6
|
+
|
|
7
|
+
## 설치
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @lemondouble/lemon-auth
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## 엔트리포인트
|
|
14
|
+
|
|
15
|
+
| 경로 | 환경 | 용도 |
|
|
16
|
+
|------|------|------|
|
|
17
|
+
| `@lemondouble/lemon-auth/server` | Server Components, Route Handlers, Server Actions | JWT 검증, 유저 조회, 인증 체크 |
|
|
18
|
+
| `@lemondouble/lemon-auth/client` | Client Components | AuthProvider, useAuth 훅, URL 헬퍼 |
|
|
19
|
+
| `@lemondouble/lemon-auth/proxy` | proxy.ts | 토큰 자동 갱신 미들웨어 |
|
|
20
|
+
|
|
21
|
+
## 사용법
|
|
22
|
+
|
|
23
|
+
### Server
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import {
|
|
27
|
+
getUser,
|
|
28
|
+
requireAuth,
|
|
29
|
+
requireClient,
|
|
30
|
+
verifyAccessToken,
|
|
31
|
+
} from "@lemondouble/lemon-auth/server";
|
|
32
|
+
|
|
33
|
+
// JWT 검증 (쿠키에서 자동으로 읽음)
|
|
34
|
+
const claims = await verifyAccessToken();
|
|
35
|
+
|
|
36
|
+
// 유저 정보 조회 (요청당 1회만 실행, React cache)
|
|
37
|
+
const user = await getUser();
|
|
38
|
+
|
|
39
|
+
// 인증 필수 (미인증 시 redirect)
|
|
40
|
+
const user = await requireAuth(); // "/" 로 redirect
|
|
41
|
+
const user = await requireAuth("/login"); // "/login" 으로 redirect
|
|
42
|
+
|
|
43
|
+
// 클라이언트 승인 필수
|
|
44
|
+
const user = await requireClient("your-client-uuid");
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Proxy (proxy.ts)
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
import { createAuthProxy } from "@lemondouble/lemon-auth/proxy";
|
|
51
|
+
|
|
52
|
+
export default createAuthProxy({
|
|
53
|
+
publicPaths: ["/", "/about"], // 인증 불필요 경로
|
|
54
|
+
clientId: process.env.CLIENT_ID, // approved_clients 체크 (선택)
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
export const config = {
|
|
58
|
+
matcher: ["/((?!_next/static|_next/image|favicon.ico).*)"],
|
|
59
|
+
};
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
proxy가 하는 일:
|
|
63
|
+
- 보호 경로 접근 시 `access_token` 검증
|
|
64
|
+
- 만료 임박(60초 이내) 시 자동으로 토큰 갱신
|
|
65
|
+
- 미인증 시 `/` 또는 로그인 페이지로 redirect
|
|
66
|
+
|
|
67
|
+
### Client
|
|
68
|
+
|
|
69
|
+
```tsx
|
|
70
|
+
// layout.tsx (Server Component)
|
|
71
|
+
import { getUser } from "@lemondouble/lemon-auth/server";
|
|
72
|
+
import { AuthProvider } from "@lemondouble/lemon-auth/client";
|
|
73
|
+
|
|
74
|
+
export default async function Layout({ children }) {
|
|
75
|
+
const user = await getUser();
|
|
76
|
+
return <AuthProvider user={user}>{children}</AuthProvider>;
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
```tsx
|
|
81
|
+
// 아무 Client Component
|
|
82
|
+
"use client";
|
|
83
|
+
import { useAuth, loginUrl, logout, profileUrl } from "@lemondouble/lemon-auth/client";
|
|
84
|
+
|
|
85
|
+
function MyComponent() {
|
|
86
|
+
const { user, isAuthenticated } = useAuth();
|
|
87
|
+
|
|
88
|
+
return isAuthenticated ? (
|
|
89
|
+
<>
|
|
90
|
+
<span>{user.nickname}</span>
|
|
91
|
+
<a href={profileUrl("https://myapp.lemondouble.com")}>프로필 편집</a>
|
|
92
|
+
<button onClick={() => logout()}>로그아웃</button>
|
|
93
|
+
</>
|
|
94
|
+
) : (
|
|
95
|
+
<a href={loginUrl("https://myapp.lemondouble.com/callback")}>로그인</a>
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## 타입
|
|
101
|
+
|
|
102
|
+
```ts
|
|
103
|
+
interface LemonUser {
|
|
104
|
+
uid: string;
|
|
105
|
+
nickname: string;
|
|
106
|
+
profileImageUrl: string;
|
|
107
|
+
role: "user" | "admin";
|
|
108
|
+
approvedClients: string[];
|
|
109
|
+
}
|
|
110
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lemondouble/lemon-auth",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"repository": {
|
|
5
|
+
"type": "git",
|
|
6
|
+
"url": "https://github.com/LemonDouble/lemon-auth"
|
|
7
|
+
},
|
|
4
8
|
"description": "Auth library for *.lemondouble.com services",
|
|
5
9
|
"type": "module",
|
|
6
10
|
"exports": {
|