@opencampus/ocid-connect-js 1.0.0

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 ADDED
@@ -0,0 +1,171 @@
1
+
2
+ ## Table of Contents
3
+
4
+ - [Setup](#setup)
5
+ - [React Integration](#react-integration)
6
+ - [Javascript Integration](#javascript-integration)
7
+ - [License](#license)
8
+
9
+ ## Setup
10
+
11
+ **yarn**
12
+
13
+ Install dependencies
14
+ ```bash
15
+ yarn install
16
+ ```
17
+
18
+ Compile & build project
19
+ ```bash
20
+ yarn build
21
+ ```
22
+
23
+ ## React Integration
24
+
25
+ Properties that can be overriden
26
+
27
+ Setup Context to hook up state variables and override configuration
28
+
29
+ ```js
30
+ import { OCConnect } from '@opencampus/ocid-connect-js';
31
+
32
+ const opts = {
33
+ redirectUri: 'http://localhost:3001/redirect',
34
+ }
35
+
36
+ return (
37
+ <div id='app-root'>
38
+ <OCConnect opts={opts} sandboxMode={true}>
39
+ <RouterProvider router={ router } />
40
+ </OCConnect>
41
+ </div>
42
+ )
43
+ ```
44
+
45
+ OCConnect Property
46
+
47
+ | Property | Description |
48
+ | --- | --- |
49
+ | opts | Authentication's properties that can be overriden |
50
+ | sandboxMode | Connect to sandbox if it is set, default to live mode |
51
+
52
+ Opts Property
53
+
54
+ | Property | Description |
55
+ | --- | --- |
56
+ | redirectUri | URL to return after the login process is completed |
57
+
58
+ Setup LoginCallBack to handle flow's result
59
+
60
+ ```js
61
+ import { LoginCallBack } from '@opencampus/ocid-connect-js';
62
+
63
+ const loginSuccess = () => {}
64
+ const loginError = () => {}
65
+
66
+ <Route path="/redirect"
67
+ element={ <LoginCallBack errorCallback={loginError} successCallback={loginSuccess} /> }
68
+ />
69
+ ```
70
+
71
+ It is possible to customize Loading & Error Page
72
+
73
+ ```js
74
+ import { LoginCallBack, useOCAuth } from '@opencampus/ocid-connect-js';
75
+
76
+ export default function CustomErrorComponent ()
77
+ {
78
+ const { authState, ocAuth } = useOCAuth();
79
+
80
+ return (
81
+ <div>Error Logging in: { authState.error.message }</div>
82
+ );
83
+ }
84
+
85
+ export default function CustomLoadingComponent ()
86
+ {
87
+ return (
88
+ <div>Error Logging in: { authState.error.message }</div>
89
+ );
90
+ }
91
+
92
+ <Route path="/redirect"
93
+ element={
94
+ <LoginCallBack
95
+ customErrorComponent={CustomErrorComponent}
96
+ customLoadingComponent={CustomLoadingComponent}
97
+ successCallback={loginSuccess} />
98
+ }
99
+ />
100
+ ```
101
+
102
+ Use useOCAuth hook to read credentials info
103
+
104
+ ```js
105
+ import { useOCAuth } from '@opencampus/ocid-connect-js';
106
+
107
+ const UserTokenPage = (props) => {
108
+ const { authState, ocAuth } = useOCAuth();
109
+
110
+ if (authState.error !== undefined) {
111
+ return <div>Error: {authState.error.message}</div>;
112
+ } else {
113
+ return (
114
+ <div>
115
+ <h4>User Info</h4>
116
+ <pre>
117
+ { JSON.stringify(ocAuth.getAuthInfo(), null, 2) }
118
+ </pre>
119
+ </div>
120
+ );
121
+ }
122
+ };
123
+ ```
124
+
125
+
126
+ ## Javascript Integration
127
+
128
+ Using OCAuthSandbox for testing environment and OCAuthLive to initialize the SDK
129
+
130
+ ```js
131
+ import { OCAuthSandbox } from '@opencampus/ocid-connect-js';
132
+ const authSdk = new OCAuthSandbox();
133
+ ```
134
+
135
+ Main Methods of Auth SDK
136
+
137
+ | Method | Description |
138
+ | --- | --- |
139
+ | signInWithRedirect | Initialize the login process |
140
+ | handleLoginRedirect | Return the auth state of the login process |
141
+ | getAuthInfo | Return auth object { edu_username, eth_address } |
142
+ | getAuthState | Return auth state data { accessToken, idToken, isAuthenticated } |
143
+
144
+ Sample usage
145
+
146
+ ```js
147
+ import { OCAuthSandbox } from '@opencampus/ocid-connect-js';
148
+
149
+ const authSdk = new OCAuthSandbox()
150
+ await authSdk.signInWithRedirect( {
151
+ state: 'opencampus',
152
+ });
153
+ ```
154
+
155
+ Sample usage to handle login response
156
+
157
+ ```js
158
+ try {
159
+ const authState = await ocAuth.handleLoginRedirect();
160
+ if ( authState.idToken ) {
161
+ // login process is completed
162
+ } else {
163
+ // login process is not completed
164
+ }
165
+ } catch ( e ) {
166
+ // there is an exception during login process
167
+ }
168
+ ```
169
+
170
+ ### License
171
+ ocid-connect-js is released under the MIT license.