@@ -24,28 +24,14 @@ import BsmOauth, {
24
24
// BSM OAuth 객체 초기화
25
25
const bsmOauth = new BsmOauth (BSM_AUTH_CLIENT_ID , BSM_AUTH_CLIENT_SECRET );
26
26
27
- try {
28
- // 인증코드로 토큰 발급
29
- const token = await bsmOauth .getToken (authCode );
27
+ // 인증코드로 토큰 발급
28
+ const token = await bsmOauth .getToken (authCode );
30
29
31
- // 토큰으로 유저 정보 가져오기
32
- const resource = await bsmOauth .getResource (token );
30
+ // 토큰으로 유저 정보 가져오기
31
+ const resource = await bsmOauth .getResource (token );
33
32
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 );
49
35
```
50
36
51
37
### JavaScript
@@ -62,30 +48,35 @@ const {
62
48
63
49
const bsmOauth = new BsmOauth (BSM_AUTH_CLIENT_ID , BSM_AUTH_CLIENT_SECRET );
64
50
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
+ // 인증코드를 찾을 수 없을 때
87
75
}
88
- })();
76
+ if (error .type === BsmOauthErrorType .TOKEN_NOT_FOUND ) {
77
+ // 토큰을 찾을 수 없을 때
78
+ }
79
+ }
89
80
```
90
81
91
82
### 학생, 선생님계정 구분하기
@@ -95,11 +86,24 @@ role 속성으로 구분할 수 있습니다.
95
86
``` javascript
96
87
// TypeScript에서는 role로 타입을 추론해야 학생 및 선생님 정보에 접근 가능합니다.
97
88
if (resource .role === BsmUserRole .STUDENT ) {
98
- // 학생 이름 확인
99
- console .log (resource .student .name );
89
+ // 학생 이름과 번호 확인
90
+ console .log (resource .student .name , resource . student . studentNo );
100
91
}
101
92
if (resource .role === BsmUserRole .TEACHER ) {
102
93
// 선생님 이름 확인
103
94
console .log (resource .teacher .name );
104
95
}
105
96
```
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