UNPKG

5.21 kBMarkdownView Raw
1<p style="text-align: center;">
2 <a href="https://oneidtech.com">
3 <img width="101" height="101" src="https://i.postimg.cc/XYwGw2WQ/One-ID-logo-Icon-PNG.png" alt="OneID" loading="lazy" /></a>
4</p>
5
6# OneID SDK for JavaScript
7
8[![NPM](https://img.shields.io/npm/v/oneid-sdk.svg)](https://www.npmjs.com/package/oneid-sdk)
9
10<p style="text-align: center;">
11 A library that gives you access to OneID SSO services from your JavaScript app. <a href="https://console.oneidtech.com">Create A Developer Account Here</a>
12</p>
13
14<br>
15
16For more information on OneID and its features, see [the documentation](https://developer.oneidtech.com).
17
18# 🚀 Getting Started
19
20The easiest way to integrate the OneID SDK into your JavaScript project is through the [npm module](https://npmjs.org/oneid-sdk).
21However, if you want to use a pre-compiled file, you can fetch it from [unpkg](https://unpkg.com). The development version is available at [https://unpkg.com/browse/oneid-sdk@1.0.13/src/index.ts](https://unpkg.com/browse/oneid-sdk@1.0.13/src/index.ts).
22
23# 📲 Installation
24You can easily install this package from NPM by running the following command in your terrminal:
25```console
26$ npm install --save oneid-sdk
27```
28
29Or including the script in your HTML file via UNPKG:
30```HTML
31<script src="https://unpkg.com/browse/oneid-sdk@1.0.13/src/index.ts"></script>
32```
33
34To use the npm modules for a browser based application, include it as you normally would:
35
36```js
37// ES5 with module loaders/bundlers
38const OneId = require('oneid-sdk');
39// ES6 with module loaders/bundlers
40import OneId from 'oneid-sdk'
41```
42# 🎁Features
43The OneID JavaScript SDK currently offers the following features:
44- [Initialize SDK](#oneidstartoptions)
45- SSO/ Authentication
46 - [Authenticate user](#oneidhandleauthoptions)
47 - [Check if there is an authenticated user](#oneidisauthenticated)
48 - [Get the profile of the current user](#oneidcurrentuser)
49 - [Log user out of the SDK](#oneidlogout)
50
51# 💻 Usage
52## OneId.start(options)
53The initialization of OneId comes first. This is done at the highest level of your application to guarantee that the OneId SDk's global instance executes correctly.
54To get the SDK started, use the `start()` method as follows:
55
56### Example code:
57
58```javascript
59const options = {
60 apiKey: "YOUR_API_KEY",
61 siteDomain: "YOUR_SITE_DOMAIN starting with http:// or https://"
62}
63window.addEventListener('load', () => {
64 OneId.start(options)
65}
66```
67
68| options | Description |
69| ------- | ----------- |
70| apiKey | This is a private key assigned to each developer's account. Generate one [here](https://business.oneidtech.com/developer/create-business-account).|
71| siteDomain | This a url of the `same-origin` with your application starting with `http://` or `https://`|
72
73## OneId.handleAuth(options)
74Using the SDK for user authentication is really simple. When you call the `handleAuth()` method, a window will pop up which processes the authentication and then returns you to the application.
75<!-- The user's profile and token are stored using `localstorage`. -->
76
77### Example code:
78
79```javascript
80
81btn.addEventListener('click', (e) => {
82 e.preventDefault()
83
84 OneId.handleAuth().then((data) => {
85 console.log(data);
86 }).catch(error => {
87 console.log(error);
88 })
89})
90```
91
92<!-- | options | Description |
93| ------- | ----------- |
94| type | OneId allows 2 types of authentication for now: `login` and `signup` |
95| scope | OneId provides user data in 3 scopes: `profile`, `basic` and `advance` |
96
97## OneId.isAuthenticated()
98`isAuthenticated` returns a `Boolean(true || false)` value of whether a user is authenticated or not.
99
100### Example code:
101
102```javascript
103window.addEventListener('load', () => {
104 if(!OneId.isAuthenticated()) {
105 window.location.assign("/login")
106 }
107})
108```
109
110## OneId.currentUser()
111To get the user data of the currently authenticated user, call the `currentUser()` method. This method returns a object containing the `user` object and `token`.
112
113### Example code:
114
115```javascript
116window.addEventListener('load', () => {
117 console.log(OneId.currentUser());
118})
119```
120
121| options | Data type | Description |
122| ------- | --------- | ----------- |
123| user | `Object` | The user object contains the `_id`, `email`, `oneId`(User's OneID ID), `profile`(User's OneID profile object), `username` |
124| token | `String` | The authenticated string assign to each user for signing requests made to the OneID API |
125
126## OneId.Logout()
127You can use the `Logout()` method to unregister a user from the OneID instance on your application while the OneID SDK is still running and initialized but the user is disconnect from the OneID API.
128
129### Example code:
130
131```javascript
132logoutBtn.addEventListener('click', (e) => {
133 e.preventDefault()
134
135 OneId.Logout()
136})
137``` -->
138
139# 🤝Support
140If you have any problems when using this SDK, please file a bug report on this repository by creating an issue and the development team will look into it as soon as possible.
141
142## ⌨ Typescript
143This library offers first-class Typescript support.
144
145
146# ✔ License
147MIT &copy; [wolzcodelife.web.app](wolzcodelife.web.app) . GitHub [@wolz-codelife](https://github.com/wolz-codelife)
148
\No newline at end of file