@oxyhq/services 0.0.1
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 +1 -0
- package/dist/AuthProvider.d.ts +12 -0
- package/dist/AuthProvider.d.ts.map +1 -0
- package/dist/AuthProvider.js +41 -0
- package/dist/config/index.d.ts +21 -0
- package/dist/config/index.d.ts.map +1 -0
- package/dist/config/index.js +7 -0
- package/dist/hooks/AuthService.d.ts +14 -0
- package/dist/hooks/AuthService.d.ts.map +1 -0
- package/dist/hooks/AuthService.js +43 -0
- package/dist/hooks/getUserById.d.ts +10 -0
- package/dist/hooks/getUserById.d.ts.map +1 -0
- package/dist/hooks/getUserById.js +20 -0
- package/dist/hooks/usePeableSession.d.ts +26 -0
- package/dist/hooks/usePeableSession.d.ts.map +1 -0
- package/dist/hooks/usePeableSession.js +45 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/package.json +40 -0
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# OxyServicesModule
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
interface AuthContextProps {
|
|
3
|
+
user: any;
|
|
4
|
+
login: (token: string) => Promise<void>;
|
|
5
|
+
logout: () => void;
|
|
6
|
+
}
|
|
7
|
+
declare const AuthProvider: React.FC<{
|
|
8
|
+
children: React.ReactNode;
|
|
9
|
+
}>;
|
|
10
|
+
export declare const useAuth: () => AuthContextProps;
|
|
11
|
+
export default AuthProvider;
|
|
12
|
+
//# sourceMappingURL=AuthProvider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AuthProvider.d.ts","sourceRoot":"","sources":["../src/AuthProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAyD,MAAM,OAAO,CAAC;AAI9E,UAAU,gBAAgB;IACxB,IAAI,EAAE,GAAG,CAAC;IACV,KAAK,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IACxC,MAAM,EAAE,MAAM,IAAI,CAAC;CACpB;AAID,QAAA,MAAM,YAAY,EAAE,KAAK,CAAC,EAAE,CAAC;IAAE,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAA;CAAE,CAwCzD,CAAC;AAEF,eAAO,MAAM,OAAO,QAAO,gBAM1B,CAAC;AAEF,eAAe,YAAY,CAAC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import React, { createContext, useContext, useState, useEffect } from "react";
|
|
2
|
+
import { useRouter } from "next/router";
|
|
3
|
+
import axios from "axios";
|
|
4
|
+
const AuthContext = createContext(undefined);
|
|
5
|
+
const AuthProvider = ({ children, }) => {
|
|
6
|
+
const [user, setUser] = useState(null);
|
|
7
|
+
const router = useRouter();
|
|
8
|
+
useEffect(() => {
|
|
9
|
+
const token = localStorage.getItem("authToken");
|
|
10
|
+
if (token) {
|
|
11
|
+
axios
|
|
12
|
+
.post("https://auth.oxy.so/api/auth/validate", { token })
|
|
13
|
+
.then((response) => setUser(response.data.user))
|
|
14
|
+
.catch(() => router.push("/login"));
|
|
15
|
+
}
|
|
16
|
+
}, []);
|
|
17
|
+
const login = async (token) => {
|
|
18
|
+
try {
|
|
19
|
+
const response = await axios.post("https://auth.oxy.so/api/auth/validate", { token });
|
|
20
|
+
localStorage.setItem("authToken", token);
|
|
21
|
+
setUser(response.data.user);
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
console.error("Login failed", error);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
const logout = () => {
|
|
28
|
+
localStorage.removeItem("authToken");
|
|
29
|
+
setUser(null);
|
|
30
|
+
router.push("/login");
|
|
31
|
+
};
|
|
32
|
+
return (React.createElement(AuthContext.Provider, { value: { user, login, logout } }, children));
|
|
33
|
+
};
|
|
34
|
+
export const useAuth = () => {
|
|
35
|
+
const context = useContext(AuthContext);
|
|
36
|
+
if (!context) {
|
|
37
|
+
throw new Error("useAuth must be used within an AuthProvider");
|
|
38
|
+
}
|
|
39
|
+
return context;
|
|
40
|
+
};
|
|
41
|
+
export default AuthProvider;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
declare const envSchema: z.ZodObject<{
|
|
3
|
+
NODE_ENV: z.ZodEnum<["development", "production"]>;
|
|
4
|
+
OXY_AUTH_URL: z.ZodString;
|
|
5
|
+
}, "strip", z.ZodTypeAny, {
|
|
6
|
+
NODE_ENV: "development" | "production";
|
|
7
|
+
OXY_AUTH_URL: string;
|
|
8
|
+
}, {
|
|
9
|
+
NODE_ENV: "development" | "production";
|
|
10
|
+
OXY_AUTH_URL: string;
|
|
11
|
+
}>;
|
|
12
|
+
declare global {
|
|
13
|
+
namespace NodeJS {
|
|
14
|
+
interface ProcessEnv extends z.infer<typeof envSchema> {
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
export declare const NODE_ENV: "development" | "production";
|
|
19
|
+
export declare const OXY_AUTH_URL: string;
|
|
20
|
+
export {};
|
|
21
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/config/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,QAAA,MAAM,SAAS;;;;;;;;;EAGb,CAAC;AAEH,OAAO,CAAC,MAAM,CAAC;IAEb,UAAU,MAAM,CAAC;QACf,UAAU,UAAW,SAAQ,CAAC,CAAC,KAAK,CAAC,OAAO,SAAS,CAAC;SAAG;KAC1D;CACF;AAED,eAAO,MAAM,QAAQ,8BAAuB,CAAC;AAC7C,eAAO,MAAM,YAAY,QACoD,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
const envSchema = z.object({
|
|
3
|
+
NODE_ENV: z.enum(["development", "production"]),
|
|
4
|
+
OXY_AUTH_URL: z.string().url(),
|
|
5
|
+
});
|
|
6
|
+
export const NODE_ENV = process.env.NODE_ENV;
|
|
7
|
+
export const OXY_AUTH_URL = NODE_ENV === "production" ? "https://auth.oxy.so" : "http://localhost:3001";
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { ReactNode } from "react";
|
|
3
|
+
interface AuthContextProps {
|
|
4
|
+
user: any;
|
|
5
|
+
login: () => void;
|
|
6
|
+
logout: () => void;
|
|
7
|
+
isAuthenticated: boolean;
|
|
8
|
+
}
|
|
9
|
+
export declare const AuthProvider: ({ children }: {
|
|
10
|
+
children: ReactNode;
|
|
11
|
+
}) => React.JSX.Element;
|
|
12
|
+
export declare const useAuth: () => AuthContextProps;
|
|
13
|
+
export {};
|
|
14
|
+
//# sourceMappingURL=AuthService.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AuthService.d.ts","sourceRoot":"","sources":["../../src/hooks/AuthService.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAIL,SAAS,EAEV,MAAM,OAAO,CAAC;AAGf,UAAU,gBAAgB;IACxB,IAAI,EAAE,GAAG,CAAC;IACV,KAAK,EAAE,MAAM,IAAI,CAAC;IAClB,MAAM,EAAE,MAAM,IAAI,CAAC;IACnB,eAAe,EAAE,OAAO,CAAC;CAC1B;AAID,eAAO,MAAM,YAAY;cAA8B,SAAS;uBA0C/D,CAAC;AAEF,eAAO,MAAM,OAAO,wBAMnB,CAAC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { createContext, useContext, useState, useEffect, } from "react";
|
|
3
|
+
import axios from "axios";
|
|
4
|
+
const AuthContext = createContext(undefined);
|
|
5
|
+
export const AuthProvider = ({ children }) => {
|
|
6
|
+
const [user, setUser] = useState(null);
|
|
7
|
+
const [isAuthenticated, setIsAuthenticated] = useState(false);
|
|
8
|
+
useEffect(() => {
|
|
9
|
+
// Check if user session exists on mount
|
|
10
|
+
const token = localStorage.getItem("authToken");
|
|
11
|
+
if (token) {
|
|
12
|
+
authenticateToken(token);
|
|
13
|
+
}
|
|
14
|
+
}, []);
|
|
15
|
+
const authenticateToken = async (token) => {
|
|
16
|
+
try {
|
|
17
|
+
const response = await axios.post("https://auth.oxy.so/api/auth/validate", { token });
|
|
18
|
+
setUser(response.data.user);
|
|
19
|
+
setIsAuthenticated(true);
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
setIsAuthenticated(false);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
const login = () => {
|
|
26
|
+
const redirectUrl = `${window.location.origin}/auth/callback`;
|
|
27
|
+
window.location.href = `https://auth.oxy.so/api/auth/login?redirectUrl=${redirectUrl}`;
|
|
28
|
+
};
|
|
29
|
+
const logout = () => {
|
|
30
|
+
localStorage.removeItem("authToken");
|
|
31
|
+
setUser(null);
|
|
32
|
+
setIsAuthenticated(false);
|
|
33
|
+
window.location.href = `https://auth.oxy.so/api/auth/logout?redirectUrl=${window.location.origin}`;
|
|
34
|
+
};
|
|
35
|
+
return (React.createElement(AuthContext.Provider, { value: { user, login, logout, isAuthenticated } }, children));
|
|
36
|
+
};
|
|
37
|
+
export const useAuth = () => {
|
|
38
|
+
const context = useContext(AuthContext);
|
|
39
|
+
if (context === undefined) {
|
|
40
|
+
throw new Error("useAuth must be used within an AuthProvider");
|
|
41
|
+
}
|
|
42
|
+
return context;
|
|
43
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
interface User {
|
|
2
|
+
id: string;
|
|
3
|
+
name: string;
|
|
4
|
+
email: string;
|
|
5
|
+
role: string;
|
|
6
|
+
avatar: string;
|
|
7
|
+
}
|
|
8
|
+
declare const getUserById: (id?: string[], fields?: (keyof User)[]) => Promise<any>;
|
|
9
|
+
export default getUserById;
|
|
10
|
+
//# sourceMappingURL=getUserById.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getUserById.d.ts","sourceRoot":"","sources":["../../src/hooks/getUserById.ts"],"names":[],"mappings":"AAGA,UAAU,IAAI;IACZ,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,QAAA,MAAM,WAAW,QAAe,MAAM,EAAE,WAAW,CAAC,MAAM,IAAI,CAAC,EAAE,iBAiBhE,CAAC;AAEF,eAAe,WAAW,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import axios from "axios";
|
|
2
|
+
import { OXY_AUTH_URL } from "../config";
|
|
3
|
+
const getUserById = async (id, fields) => {
|
|
4
|
+
try {
|
|
5
|
+
const response = await axios.get(OXY_AUTH_URL + "/api/users/${id}");
|
|
6
|
+
const user = fields
|
|
7
|
+
? fields.reduce((obj, key) => ({ ...obj, [key]: response.data[key] }), {})
|
|
8
|
+
: response.data;
|
|
9
|
+
return user;
|
|
10
|
+
}
|
|
11
|
+
catch (error) {
|
|
12
|
+
if (axios.isAxiosError(error)) {
|
|
13
|
+
throw new Error(error.message);
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
throw new Error("An unknown error occurred while loading the user data.");
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
export default getUserById;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
interface SessionModel {
|
|
2
|
+
user: {
|
|
3
|
+
id: string;
|
|
4
|
+
username: string;
|
|
5
|
+
name: string;
|
|
6
|
+
lastname: string;
|
|
7
|
+
email: string;
|
|
8
|
+
verified: boolean;
|
|
9
|
+
profile_image_url: string;
|
|
10
|
+
created_at: string;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
type SessionState = {
|
|
14
|
+
session: SessionModel | null;
|
|
15
|
+
status: string;
|
|
16
|
+
error: any;
|
|
17
|
+
fetchSessionData: () => void;
|
|
18
|
+
};
|
|
19
|
+
export declare const useSessionStore: import("zustand").UseBoundStore<import("zustand").StoreApi<SessionState>>;
|
|
20
|
+
declare function usePeableSession(): {
|
|
21
|
+
session: SessionModel | null;
|
|
22
|
+
status: string;
|
|
23
|
+
error: any;
|
|
24
|
+
};
|
|
25
|
+
export default usePeableSession;
|
|
26
|
+
//# sourceMappingURL=usePeableSession.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"usePeableSession.d.ts","sourceRoot":"","sources":["../../src/hooks/usePeableSession.ts"],"names":[],"mappings":"AAOA,UAAU,YAAY;IACpB,IAAI,EAAE;QACJ,EAAE,EAAE,MAAM,CAAC;QACX,QAAQ,EAAE,MAAM,CAAC;QACjB,IAAI,EAAE,MAAM,CAAC;QACb,QAAQ,EAAE,MAAM,CAAC;QACjB,KAAK,EAAE,MAAM,CAAC;QACd,QAAQ,EAAE,OAAO,CAAC;QAClB,iBAAiB,EAAE,MAAM,CAAC;QAC1B,UAAU,EAAE,MAAM,CAAC;KACpB,CAAC;CACH;AAED,KAAK,YAAY,GAAG;IAClB,OAAO,EAAE,YAAY,GAAG,IAAI,CAAC;IAC7B,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,GAAG,CAAC;IACX,gBAAgB,EAAE,MAAM,IAAI,CAAC;CAC9B,CAAC;AAEF,eAAO,MAAM,eAAe,2EAmC1B,CAAC;AAEH,iBAAS,gBAAgB;;;;EAQxB;AAED,eAAe,gBAAgB,CAAC"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { useEffect } from "react";
|
|
2
|
+
import axios from "axios";
|
|
3
|
+
import { create } from "zustand";
|
|
4
|
+
import localforage from "localforage";
|
|
5
|
+
import { OXY_AUTH_URL } from "../config";
|
|
6
|
+
export const useSessionStore = create((set) => {
|
|
7
|
+
let isFirstFetch = true;
|
|
8
|
+
return {
|
|
9
|
+
session: null,
|
|
10
|
+
status: "loading",
|
|
11
|
+
error: null,
|
|
12
|
+
fetchSessionData: async () => {
|
|
13
|
+
try {
|
|
14
|
+
if (isFirstFetch) {
|
|
15
|
+
set({ status: "loading" });
|
|
16
|
+
isFirstFetch = false;
|
|
17
|
+
}
|
|
18
|
+
// Get the session ID from the URL parameters if it exists
|
|
19
|
+
const urlParams = new URLSearchParams(window.location.search);
|
|
20
|
+
let clientKey = urlParams.get("clientKey");
|
|
21
|
+
// If the session ID was not found in the URL parameters, get it from local storage
|
|
22
|
+
if (!clientKey) {
|
|
23
|
+
clientKey = await localforage.getItem("clientKey");
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
// If the session ID was found in the URL parameters, set it in local storage
|
|
27
|
+
await localforage.setItem("clientKey", clientKey);
|
|
28
|
+
}
|
|
29
|
+
const response = await axios.get(OXY_AUTH_URL + "/api/session/" + clientKey);
|
|
30
|
+
set({ session: response.data, status: "success" });
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
set({ error, status: "error" });
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
});
|
|
38
|
+
function usePeableSession() {
|
|
39
|
+
const { session, status, error, fetchSessionData } = useSessionStore();
|
|
40
|
+
useEffect(() => {
|
|
41
|
+
fetchSessionData();
|
|
42
|
+
}, []);
|
|
43
|
+
return { session, status, error };
|
|
44
|
+
}
|
|
45
|
+
export default usePeableSession;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,gBAAgB,MAAM,0BAA0B,CAAC;AACxD,OAAO,WAAW,MAAM,qBAAqB,CAAC;AAE9C,OAAO,EAAE,gBAAgB,EAAE,WAAW,EAAE,CAAC"}
|
package/dist/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@oxyhq/services",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsc",
|
|
12
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
13
|
+
"lint": "eslint src/**/*.{ts,tsx}"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [],
|
|
16
|
+
"author": "Albert Isern Alvarez",
|
|
17
|
+
"company": "Oxy",
|
|
18
|
+
"license": "ISC",
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@types/next-auth": "^3.15.0",
|
|
21
|
+
"@types/node": "^20.11.17",
|
|
22
|
+
"@types/react": "^18.2.55",
|
|
23
|
+
"@typescript-eslint/eslint-plugin": "^7.0.1",
|
|
24
|
+
"@typescript-eslint/parser": "^7.0.1",
|
|
25
|
+
"autoprefixer": "^10.4.17",
|
|
26
|
+
"eslint": "^8.56.0",
|
|
27
|
+
"postcss": "^8.4.35",
|
|
28
|
+
"tailwindcss": "^3.4.1",
|
|
29
|
+
"typescript": "^5.3.3"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"axios": "^1.6.8",
|
|
33
|
+
"dotenv": "^16.4.5",
|
|
34
|
+
"localforage": "^1.10.0",
|
|
35
|
+
"react": "^18.2.0",
|
|
36
|
+
"react-dom": "^18.2.0",
|
|
37
|
+
"zod": "^3.23.8",
|
|
38
|
+
"zustand": "^4.5.2"
|
|
39
|
+
}
|
|
40
|
+
}
|