authly-me 0.0.4 → 0.0.6

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.
Files changed (3) hide show
  1. package/config.js +7 -0
  2. package/index.js +37 -14
  3. package/package.json +1 -1
package/config.js ADDED
@@ -0,0 +1,7 @@
1
+ const app_key_header = "App-Key";
2
+
3
+ const config = {
4
+ app_key_header
5
+ };
6
+
7
+ export default config;
package/index.js CHANGED
@@ -1,26 +1,33 @@
1
1
  const axios = require('axios');
2
+ const { default: config } = require('./config');
2
3
 
3
4
  class Authly {
4
5
 
5
- constructor(server_url, key, options={}) {
6
+ constructor(server_url, app_key, options={}) {
6
7
 
7
8
  if (!server_url || server_url === '' || server_url.trim() === '') {
8
9
  throw new Error('Server is required');
9
10
  }
10
11
 
11
- if (!key || key === '' || key.trim() === '') {
12
- throw new Error('Key is required');
12
+ if (!app_key || app_key === '' || app_key.trim() === '') {
13
+ throw new Error('App Key is required');
13
14
  }
14
- this.key = key;
15
+ this.app_key = app_key;
15
16
  this.server_url = server_url;
16
17
  this.callback_url = options?.callback_url ?? null;
17
18
  }
18
19
 
20
+
21
+ getHeaders() {
22
+ return {
23
+ [config.app_key_header]: this.app_key
24
+ }
25
+ }
26
+
19
27
  // Generate App Auth Link
20
28
  async generateAppAuthLink() {
21
- const payload = {
22
- key: this.key
23
- };
29
+
30
+ const headers = this.getHeaders();
24
31
 
25
32
  if (this.callback_url) {
26
33
  payload.callback_url = this.callback_url;
@@ -28,7 +35,7 @@ class Authly {
28
35
 
29
36
  let response;
30
37
  try {
31
- response = await axios.post(`${this.server_url}/v1/auth/app/generate-link`, payload);
38
+ response = await axios.post(`${this.server_url}/v1/auth/app/generate-link`, {}, { headers });
32
39
  return response.data;
33
40
  } catch (error) {
34
41
  return error.response.data;
@@ -41,26 +48,42 @@ class Authly {
41
48
  throw new Error('Token is required');
42
49
  }
43
50
 
51
+ const headers = this.getHeaders();
52
+
44
53
  const payload = {
45
- key: this.key,
46
54
  token: token
47
55
  };
48
56
 
49
57
  let response;
50
58
  try {
51
- response = await axios.post(`${this.server_url}/v1/auth/app/verify-token`, payload);
59
+ response = await axios.post(`${this.server_url}/v1/auth/app/verify-token`, payload, { headers });
52
60
  return response.data;
53
61
  } catch (error) {
54
62
  return error.response.data;
55
63
  }
56
64
  }
57
65
 
58
- // TODO : Get available credits ( company level )
66
+ // Logout Session
67
+ async logout(token) {
68
+ if (!token || token === '' || token.trim() === '') {
69
+ throw new Error('Token is required');
70
+ }
71
+
72
+ const headers = this.getHeaders();
59
73
 
60
- // TODO : Get credits used ( app level )
74
+ const payload = {
75
+ token: token
76
+ };
61
77
 
62
- // TODO : Logout ( given the token )
78
+ let response;
79
+ try {
80
+ response = await axios.post(`${this.server_url}/v1/auth/app/logout-session`, payload, { headers });
81
+ return response.data;
82
+ } catch (error) {
83
+ return error.response.data;
84
+ }
85
+ }
63
86
 
64
87
  }
65
88
 
66
- module.exports = Authly;
89
+ module.exports = Authly;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "authly-me",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"