Skip to content

Commit c3b36c3

Browse files
authored
Improve/auth + ActionButton/Icon/Input Component (#226)
* added ActionButton, Icon, Input Signed-off-by: Abhinav Kumar <abhinav@avitechlab.com> * improve auth Signed-off-by: Abhinav Kumar <abhinav@avitechlab.com> --------- Signed-off-by: Abhinav Kumar <abhinav@avitechlab.com>
1 parent f311a0b commit c3b36c3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+7803
-485
lines changed

packages/auth/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"devDependencies": {
2020
"parcel": "^2.9.1",
2121
"rollup": "^3.23.0",
22-
"rollup-plugin-dts": "^5.3.0",
22+
"rollup-plugin-dts": "5.3.1",
2323
"rollup-plugin-esbuild": "^5.0.0"
2424
}
2525
}

packages/auth/playground/playground.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
import { rocketChatAuth } from '../src'
22

3+
async function saveToken (token) {
4+
localStorage.setItem("ec_token", token);
5+
}
6+
7+
async function getToken() {
8+
return localStorage.getItem("ec_token");
9+
}
10+
11+
async function deleteToken() {
12+
localStorage.removeItem("ec_token");
13+
}
14+
315
let auth = rocketChatAuth({
4-
host: "http://localhost:3000"
16+
host: "http://localhost:3000",
17+
getToken,
18+
saveToken,
19+
deleteToken,
520
});
621

722
async function loginWithPassword() {
@@ -37,7 +52,10 @@ window.document.body.onload = () => {
3752
const hostInput = document.getElementById("hostUrl")
3853
const host = hostInput.value;
3954
auth = rocketChatAuth({
40-
host
55+
host,
56+
saveToken,
57+
getToken,
58+
deleteToken,
4159
});
4260
hostInput.addEventListener("change", (e) => {
4361
auth = rocketChatAuth({

packages/auth/src/Api.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import axios, { AxiosRequestConfig } from "axios";
2+
3+
export class Api {
4+
baseUrl: string;
5+
constructor(baseUrl: string) {
6+
this.baseUrl = baseUrl;
7+
}
8+
getAxiosConfig = (config: AxiosRequestConfig) => {
9+
const headers = {
10+
'Content-Type': 'application/json',
11+
...(config?.headers || {})
12+
}
13+
const axiosConfig: AxiosRequestConfig = {
14+
...config,
15+
headers,
16+
baseURL: this.baseUrl
17+
}
18+
return axiosConfig;
19+
}
20+
async post(endpoint: string, data: any, config: AxiosRequestConfig = {}) {
21+
return axios.post(endpoint, data, this.getAxiosConfig(config));
22+
}
23+
async get(endpoint: string, config: AxiosRequestConfig = {}) {
24+
return axios.get(endpoint, this.getAxiosConfig(config));
25+
}
26+
async put(endpoint: string, data: any, config: AxiosRequestConfig = {}) {
27+
return axios.put(endpoint, data, this.getAxiosConfig(config));
28+
}
29+
async delete(endpoint: string, config: AxiosRequestConfig = {}) {
30+
return axios.delete(endpoint, this.getAxiosConfig(config));
31+
}
32+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export interface IRocketChatAuthOptions {
2+
host: string;
3+
saveToken: (token: string) => Promise<void>;
4+
getToken: () => Promise<string>;
5+
deleteToken: () => Promise<void>;
6+
autoLogin?: boolean;
7+
}

packages/auth/src/RocketChatAuth.ts

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
1-
import axios, { Axios, AxiosError } from "axios";
1+
import { AxiosError } from "axios";
22
import loginWithPassword from "./loginWithPassword";
33
import loginWithOAuthService from "./loginWithOAuthService";
44
import loginWithOAuthServiceToken from "./loginWithOAuthServiceToken";
55
import loginWithResumeToken from "./loginWithResumeToken";
6+
import { IRocketChatAuthOptions } from "./IRocketChatAuthOptions";
7+
import { Api } from "./Api";
68

79
class RocketChatAuth {
810
host: string;
9-
api: Axios;
11+
api: Api;
1012
currentUser: any;
1113
lastFetched: Date;
1214
authListeners: ((user: object | null) => void )[] = [];
13-
constructor(host: string) {
15+
deleteToken: () => Promise<void>;
16+
saveToken: (token: string) => Promise<void>;
17+
getToken: () => Promise<string>;
18+
constructor({ host, saveToken, getToken, deleteToken, autoLogin = true }: IRocketChatAuthOptions) {
1419
this.host = host;
15-
this.api = axios.create({
16-
baseURL: host,
17-
headers: {
18-
"Content-Type": "application/json"
19-
}
20-
});
20+
this.api = new Api(host);
2121
this.lastFetched = new Date(0);
2222
this.currentUser = null;
23-
this.load();
23+
this.getToken = getToken;
24+
this.saveToken = saveToken;
25+
this.deleteToken = deleteToken;
26+
if (autoLogin) {
27+
this.load();
28+
}
2429
}
2530

2631
/**
@@ -63,9 +68,12 @@ class RocketChatAuth {
6368
* @param oauthService
6469
*/
6570
async loginWithOAuthService(oauthService: any) {
66-
const response = await loginWithOAuthService({
67-
api: this.api
68-
}, oauthService);
71+
const response = await loginWithOAuthService(
72+
{
73+
api: this.api,
74+
},
75+
oauthService
76+
);
6977
}
7078

7179
/**
@@ -74,10 +82,12 @@ class RocketChatAuth {
7482
* @returns
7583
*/
7684
async loginWithOAuthServiceToken(credentials: { [key: string]: string; service: string; access_token: string; }) {
77-
const response = await loginWithOAuthServiceToken({
78-
api: this.api,
79-
},
80-
credentials)
85+
const response = await loginWithOAuthServiceToken(
86+
{
87+
api: this.api,
88+
},
89+
credentials
90+
);
8191
this.setUser(response.data)
8292
return this.currentUser;
8393
}
@@ -127,31 +137,31 @@ class RocketChatAuth {
127137
async setUser(user: object) {
128138
this.lastFetched = new Date();
129139
this.currentUser = user;
130-
this.save()
140+
await this.save()
131141
}
132142

133-
/**
134-
* Save current user to localStorage
135-
*/
136-
save() {
137-
localStorage.setItem("ec_user", JSON.stringify({
138-
user: this.currentUser,
139-
lastFetched: this.lastFetched
140-
}))
143+
async save() {
144+
// localStorage.setItem("ec_user", JSON.stringify({
145+
// user: this.currentUser,
146+
// lastFetched: this.lastFetched
147+
// }))
148+
await this.saveToken(this.currentUser.authToken)
141149
this.notifyAuthListeners();
142150
}
143151

144152
/**
145153
* Load current user from localStorage
146154
*/
147155
async load() {
148-
const {user, lastFetched} = JSON.parse(localStorage.getItem("ec_user") || "{}");
156+
// const {user, lastFetched} = JSON.parse(localStorage.getItem("ec_user") || "{}");
157+
const token = await this.getToken();
158+
const user = await this.loginWithResumeToken(token);
149159
if (user) {
150-
this.lastFetched = new Date(lastFetched);
160+
this.lastFetched = new Date();
151161
this.setUser(user);
152162
await this.getCurrentUser(); // refresh the token if needed
153163
}
154-
this.notifyAuthListeners()
164+
this.notifyAuthListeners();
155165
}
156166

157167
/**
@@ -167,8 +177,10 @@ class RocketChatAuth {
167177
});
168178
} catch (err) {
169179
console.error(err);
170-
}
171-
localStorage.removeItem("ec_user");
180+
} finally {
181+
await this.deleteToken();
182+
}
183+
// localStorage.removeItem("ec_user");
172184
this.lastFetched = new Date(0);
173185
this.currentUser = null;
174186
this.notifyAuthListeners();

packages/auth/src/auth.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
1-
import { Axios } from 'axios';
1+
import { IRocketChatAuthOptions } from './IRocketChatAuthOptions';
22
import RocketChatAuth from './RocketChatAuth';
33

44
const rocketChatAuth = ({
5-
host
6-
}: {
7-
host: string;
8-
}) => {
9-
return new RocketChatAuth(host);
5+
host,
6+
saveToken,
7+
getToken,
8+
deleteToken,
9+
autoLogin,
10+
}: IRocketChatAuthOptions) => {
11+
return new RocketChatAuth({
12+
host,
13+
saveToken,
14+
getToken,
15+
deleteToken,
16+
autoLogin,
17+
});
1018
}
1119

1220
export {

packages/auth/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './auth';
22
export { default as RocketChatAuth } from './RocketChatAuth';
3+
export * from './IRocketChatAuthOptions';

packages/auth/src/loginWithOAuthService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { Axios } from "axios";
1+
import { Api } from "./Api";
22
import getAuthorizationUrl from "./getAuthorizationUrl";
33

44
const loginWithOAuthService = async (
55
config: {
6-
api: Axios
6+
api: Api
77
},
88
oauthService: any
99
) => {

packages/auth/src/loginWithOAuthServiceToken.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { Axios } from "axios";
2-
import getAuthorizationUrl from "./getAuthorizationUrl";
1+
import { Api } from "./Api";
32

43
const loginWithOAuthServiceToken = async (
54
config: {
6-
api: Axios
5+
api: Api
76
},
87
credentials: {
98
service: string;

packages/auth/src/loginWithPassword.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Axios } from "axios"
1+
import { Api } from "./Api";
22

33
const loginWithPassword = async (
44
config : {
5-
api: Axios;
5+
api: Api;
66
}, {
77
user,
88
password,

0 commit comments

Comments
 (0)