Skip to content

Commit b9d99cb

Browse files
committed
Update README.md
1 parent 85c6d61 commit b9d99cb

File tree

1 file changed

+49
-45
lines changed

1 file changed

+49
-45
lines changed

README.md

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,14 @@ import BsmOauth, {
2424
// BSM OAuth 객체 초기화
2525
const bsmOauth = new BsmOauth(BSM_AUTH_CLIENT_ID, BSM_AUTH_CLIENT_SECRET);
2626

27-
try {
28-
// 인증코드로 토큰 발급
29-
const token = await bsmOauth.getToken(authCode);
27+
// 인증코드로 토큰 발급
28+
const token = await bsmOauth.getToken(authCode);
3029

31-
// 토큰으로 유저 정보 가져오기
32-
const resource = await bsmOauth.getResource(token);
30+
// 토큰으로 유저 정보 가져오기
31+
const resource = await bsmOauth.getResource(token);
3332

34-
// 가져온 유저 정보 확인
35-
console.log(resource.userCode);
36-
} catch (error) {
37-
if (error instanceof BsmOauthError) {
38-
if (error.type === BsmOauthErrorType.INVALID_CLIENT) {
39-
// 클라이언트 정보가 잘못되었을 때
40-
}
41-
if (error.type === BsmOauthErrorType.AUTH_CODE_NOT_FOUND) {
42-
// 인증코드를 찾을 수 없을 때
43-
}
44-
if (error.type === BsmOauthErrorType.TOKEN_NOT_FOUND) {
45-
// 토큰을 찾을 수 없을 때
46-
}
47-
}
48-
}
33+
// 가져온 유저 정보 확인
34+
console.log(resource);
4935
```
5036

5137
### JavaScript
@@ -62,30 +48,35 @@ const {
6248

6349
const bsmOauth = new BsmOauth(BSM_AUTH_CLIENT_ID, BSM_AUTH_CLIENT_SECRET);
6450

65-
(async () => {
66-
try {
67-
// 인증코드로 토큰 발급
68-
const token = await bsmOauth.getToken(authCode);
69-
70-
// 토큰으로 유저 정보 가져오기
71-
const resource = await bsmOauth.getResource(token);
72-
73-
// 가져온 유저 정보 확인
74-
console.log(resource.userCode);
75-
} catch (error) {
76-
if (error instanceof BsmOauthError) {
77-
if (error.type === BsmOauthErrorType.INVALID_CLIENT) {
78-
// 클라이언트 정보가 잘못되었을 때
79-
}
80-
if (error.type === BsmOauthErrorType.AUTH_CODE_NOT_FOUND) {
81-
// 인증코드를 찾을 수 없을 때
82-
}
83-
if (error.type === BsmOauthErrorType.TOKEN_NOT_FOUND) {
84-
// 토큰을 찾을 수 없을 때
85-
}
86-
}
51+
// 인증코드로 토큰 발급
52+
const token = await bsmOauth.getToken(authCode);
53+
54+
// 토큰으로 유저 정보 가져오기
55+
return await bsmOauth.getResource(token);
56+
57+
// 가져온 유저 정보 확인
58+
console.log(resource);
59+
```
60+
61+
### 예외 처리하기
62+
```javascript
63+
try {
64+
const token = await bsmOauth.getToken(authCode);
65+
await bsmOauth.getResource(token);
66+
} catch (error) {
67+
if (!(error instanceof BsmOauthError)) {
68+
return;
69+
}
70+
if (error.type === BsmOauthErrorType.INVALID_CLIENT) {
71+
// 클라이언트 정보가 잘못되었을 때
72+
}
73+
if (error.type === BsmOauthErrorType.AUTH_CODE_NOT_FOUND) {
74+
// 인증코드를 찾을 수 없을 때
8775
}
88-
})();
76+
if (error.type === BsmOauthErrorType.TOKEN_NOT_FOUND) {
77+
// 토큰을 찾을 수 없을 때
78+
}
79+
}
8980
```
9081

9182
### 학생, 선생님계정 구분하기
@@ -95,11 +86,24 @@ role 속성으로 구분할 수 있습니다.
9586
```javascript
9687
// TypeScript에서는 role로 타입을 추론해야 학생 및 선생님 정보에 접근 가능합니다.
9788
if (resource.role === BsmUserRole.STUDENT) {
98-
// 학생 이름 확인
99-
console.log(resource.student.name);
89+
// 학생 이름과 번호 확인
90+
console.log(resource.student.name, resource.student.studentNo);
10091
}
10192
if (resource.role === BsmUserRole.TEACHER) {
10293
// 선생님 이름 확인
10394
console.log(resource.teacher.name);
10495
}
10596
```
97+
98+
### 학생, 선생님의 고유 정보에 바로 접근하기
99+
100+
getRawResource 메서드로 원시 데이터를 가져올 수 있습니다.
101+
102+
```typescript
103+
const rawResource: RawBsmOAuthResource = await bsmOauth.getRawResource(token);
104+
// 학생 또는 선생님의 이름 확인
105+
console.log(rawResource.name);
106+
107+
// 학생의 번호 확인 (만약 resource.role이 TEACHER일 경우 undefined가 출력됩니다.)
108+
console.log(rawResource.studentNo);
109+
```

0 commit comments

Comments
 (0)