Skip to content

Commit 810e793

Browse files
committed
Create README.md
1 parent 83b6751 commit 810e793

File tree

4 files changed

+108
-7
lines changed

4 files changed

+108
-7
lines changed

README.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# BSM OAuth JS
2+
[BSM Auth](https://github.com/BSSM-BSM/BSM-Auth-Backend-V1)의 OAuth기능을 JS에서 사용하기 쉽게 만들어주는 라이브러리입니다.
3+
4+
## 설치
5+
```bash
6+
$ npm install bsm-oauth
7+
```
8+
9+
## 사용하기
10+
11+
### TypeScript
12+
```typescript
13+
import BsmOauth, { BsmOauthError, BsmOauthErrorType, BsmOauthUserRole, StudentResource, TeacherResource } from "bsm-oauth";
14+
15+
// BSM OAuth 객체 초기화
16+
const bsmOauth: BsmOauth = new BsmOauth(BSM_AUTH_CLIENT_ID, BSM_AUTH_CLIENT_SECRET);
17+
18+
try {
19+
// 인증코드로 토큰 발급
20+
const token: string = await bsmOauth.getToken(authCode);
21+
22+
// 토큰으로 유저 정보 가져오기
23+
const resource: (StudentResource | TeacherResource) = await bsmOauth.getResource(token);
24+
25+
// 가져온 유저 정보 확인
26+
console.log(resource.userCode);
27+
} catch (error) {
28+
if (error instanceof BsmOauthError) {
29+
switch (error.type) {
30+
case BsmOauthErrorType.INVALID_CLIENT: {
31+
// 클라이언트 정보가 잘못되었을 때
32+
break;
33+
}
34+
case BsmOauthErrorType.AUTH_CODE_NOT_FOUND: {
35+
// 인증코드를 찾을 수 없을 때
36+
break;
37+
}
38+
case BsmOauthErrorType.TOKEN_NOT_FOUND: {
39+
// 토큰을 찾을 수 없을 때
40+
break;
41+
}
42+
}
43+
}
44+
}
45+
```
46+
### JavaScript
47+
```typescript
48+
const { BsmOauth, BsmOauthError, BsmOauthErrorType, BsmOauthUserRole, StudentResource, TeacherResource } = require('bsm-oauth');
49+
50+
const bsmOauth = new BsmOauth(BSM_AUTH_CLIENT_ID, BSM_AUTH_CLIENT_SECRET);
51+
52+
(async () => {
53+
try {
54+
// 인증코드로 토큰 발급
55+
const token = await bsmOauth.getToken(authCode);
56+
57+
// 토큰으로 유저 정보 가져오기
58+
const resource = await bsmOauth.getResource(token);
59+
60+
// 가져온 유저 정보 확인
61+
console.log(resource.userCode);
62+
} catch (error) {
63+
if (error instanceof BsmOauthError) {
64+
switch (error.type) {
65+
case BsmOauthErrorType.INVALID_CLIENT: {
66+
// 클라이언트 정보가 잘못되었을 때
67+
break;
68+
}
69+
case BsmOauthErrorType.AUTH_CODE_NOT_FOUND: {
70+
// 인증코드를 찾을 수 없을 때
71+
break;
72+
}
73+
case BsmOauthErrorType.TOKEN_NOT_FOUND: {
74+
// 토큰을 찾을 수 없을 때
75+
break;
76+
}
77+
}
78+
}
79+
}
80+
})();
81+
```
82+
### 학생, 선생님계정 구분하기
83+
role 속성으로 구분할 수 있습니다.
84+
```javascript
85+
// TypeScript에서는 role로 타입을 추론해야 학생 및 선생님 정보에 접근 가능합니다.
86+
switch (resource.role) {
87+
case BsmOauthUserRole.STUDENT: {
88+
// 학생 이름 확인
89+
console.log(resource.student.name);
90+
break;
91+
}
92+
case BsmOauthUserRole.TEACHER: {
93+
// 선생님 이름 확인
94+
console.log(resource.teacher.name);
95+
break;
96+
}
97+
}
98+
```

index.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
import BsmOauth from "./lib/bsmOauth";
2-
import BsmOauthError from "./lib/error";
1+
import BsmOauth from './lib/bsmOauth';
2+
import BsmOauthError from './lib/error';
33
import { default as BsmOauthErrorType } from './lib/types/errorType';
4-
import { default as BsmOauthUserRole } from "./lib/types/userRole";
5-
4+
import { default as BsmOauthUserRole } from './lib/types/userRole';
5+
import { StudentResource } from './lib/types/student';
6+
import { TeacherResource } from './lib/types/teacher';
67

78
export default BsmOauth;
89
export {
910
BsmOauth,
1011
BsmOauthError,
1112
BsmOauthErrorType,
1213
BsmOauthUserRole,
14+
StudentResource,
15+
TeacherResource
1316
}

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bsm-oauth",
3-
"version": "1.0.4",
3+
"version": "1.0.5",
44
"description": "BSM Auth를 JavaScript에서 쉽게 사용할 수 있는 라이브러리",
55
"main": "./cjs/index.js",
66
"module": "./esm/index.js",

0 commit comments

Comments
 (0)