v1.0.0
This commit is contained in:
@@ -5,10 +5,11 @@ import { AuthResponse, User } from '../types';
|
||||
|
||||
interface AuthContextType {
|
||||
user: User | null;
|
||||
serverAddress: string | null;
|
||||
token: string | null;
|
||||
isAuthenticated: boolean;
|
||||
isLoading: boolean;
|
||||
login: (identity: string, password: string, isSuperuser?: boolean) => Promise<void>;
|
||||
login: (serverAddress: string, identity: string, password: string) => Promise<void>;
|
||||
logout: () => Promise<void>;
|
||||
}
|
||||
|
||||
@@ -16,6 +17,7 @@ const AuthContext = createContext<AuthContextType | undefined>(undefined);
|
||||
|
||||
export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
|
||||
const [user, setUser] = useState<User | null>(null);
|
||||
const [serverAddress, setServerAddress] = useState<string | null>(null);
|
||||
const [token, setToken] = useState<string | null>(null);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
|
||||
@@ -27,12 +29,18 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
|
||||
try {
|
||||
const storedToken = await AsyncStorage.getItem('auth_token');
|
||||
const storedUser = await AsyncStorage.getItem('auth_user');
|
||||
const storedServerAddress = await AsyncStorage.getItem('auth_server_address');
|
||||
|
||||
if (storedToken && storedUser) {
|
||||
setToken(storedToken);
|
||||
api.setToken(storedToken);
|
||||
setUser(JSON.parse(storedUser));
|
||||
}
|
||||
|
||||
if (storedServerAddress) {
|
||||
setServerAddress(storedServerAddress);
|
||||
api.setAddress(storedServerAddress + '/api');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to load auth', error);
|
||||
} finally {
|
||||
@@ -40,15 +48,17 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
|
||||
}
|
||||
};
|
||||
|
||||
const login = async (identity: string, password: string, isSuperuser = false) => {
|
||||
const login = async (serverAddress: string, identity: string, password: string) => {
|
||||
try {
|
||||
const response: AuthResponse = await api.authenticate(identity, password, isSuperuser);
|
||||
const response: AuthResponse = await api.authenticate(serverAddress, identity, password);
|
||||
|
||||
await AsyncStorage.setItem('auth_token', response.token);
|
||||
await AsyncStorage.setItem('auth_user', JSON.stringify(response.record));
|
||||
await AsyncStorage.setItem('auth_server_address', serverAddress);
|
||||
|
||||
setToken(response.token);
|
||||
setUser(response.record);
|
||||
setServerAddress(serverAddress);
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
@@ -60,8 +70,10 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
|
||||
await AsyncStorage.removeItem('auth_user');
|
||||
|
||||
api.clearToken();
|
||||
api.clearAddress();
|
||||
setToken(null);
|
||||
setUser(null);
|
||||
setServerAddress(null);
|
||||
} catch (error) {
|
||||
console.error('Failed to logout', error);
|
||||
}
|
||||
@@ -70,6 +82,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
|
||||
return (
|
||||
<AuthContext.Provider
|
||||
value={{
|
||||
serverAddress,
|
||||
user,
|
||||
token,
|
||||
isAuthenticated: !!token && !!user,
|
||||
|
||||
@@ -1,245 +1,256 @@
|
||||
import { Device, AuthResponse, NetworkScanResult } from '../types';
|
||||
|
||||
const API_BASE_URL = 'https://wol.f6knight.duckdns.org/api';
|
||||
import { Device, AuthResponse, NetworkScanResult } from "../types";
|
||||
|
||||
class UpSnapAPI {
|
||||
private token: string | null = null;
|
||||
private token: string | null = null;
|
||||
private address: string | null = null;
|
||||
|
||||
setToken(token: string) {
|
||||
this.token = token;
|
||||
}
|
||||
setToken(token: string) {
|
||||
this.token = token;
|
||||
}
|
||||
|
||||
getToken(): string | null {
|
||||
return this.token;
|
||||
}
|
||||
getToken(): string | null {
|
||||
return this.token;
|
||||
}
|
||||
|
||||
clearToken() {
|
||||
this.token = null;
|
||||
}
|
||||
clearToken() {
|
||||
this.token = null;
|
||||
}
|
||||
|
||||
private getHeaders(): HeadersInit {
|
||||
const headers: HeadersInit = {
|
||||
'Content-Type': 'application/json',
|
||||
};
|
||||
|
||||
if (this.token) {
|
||||
headers['Authorization'] = `Bearer ${this.token}`;
|
||||
}
|
||||
|
||||
return headers;
|
||||
}
|
||||
setAddress(address: string) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
async authenticate(identity: string, password: string, isSuperuser = false): Promise<AuthResponse> {
|
||||
const endpoint = isSuperuser
|
||||
? `${API_BASE_URL}/collections/_superusers/auth-with-password`
|
||||
: `${API_BASE_URL}/collections/users/auth-with-password`;
|
||||
|
||||
const response = await fetch(endpoint, {
|
||||
method: 'POST',
|
||||
headers: this.getHeaders(),
|
||||
body: JSON.stringify({ identity, password }),
|
||||
});
|
||||
getAddress(): string | null {
|
||||
return this.address;
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
const error = await response.json();
|
||||
throw new Error(error.message || 'Authentication failed');
|
||||
}
|
||||
clearAddress() {
|
||||
this.address = null;
|
||||
}
|
||||
|
||||
const data: AuthResponse = await response.json();
|
||||
this.token = data.token;
|
||||
return data;
|
||||
}
|
||||
private getHeaders(): HeadersInit {
|
||||
const headers: HeadersInit = {
|
||||
"Content-Type": "application/json",
|
||||
};
|
||||
|
||||
async getDevices(page = 1, perPage = 30): Promise<Device[]> {
|
||||
const response = await fetch(
|
||||
`${API_BASE_URL}/collections/devices/records?page=${page}&perPage=${perPage}`,
|
||||
{
|
||||
headers: this.getHeaders(),
|
||||
}
|
||||
);
|
||||
if (this.token) {
|
||||
headers["Authorization"] = `Bearer ${this.token}`;
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to fetch devices');
|
||||
}
|
||||
return headers;
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
return data.items;
|
||||
}
|
||||
async authenticate(
|
||||
serverAddress: string,
|
||||
identity: string,
|
||||
password: string
|
||||
): Promise<AuthResponse> {
|
||||
this.address = serverAddress + "/api";
|
||||
|
||||
async getDevice(id: string): Promise<Device> {
|
||||
const response = await fetch(
|
||||
`${API_BASE_URL}/collections/devices/records/${id}`,
|
||||
{
|
||||
headers: this.getHeaders(),
|
||||
}
|
||||
);
|
||||
const response = await fetch(
|
||||
`${this.address}/collections/users/auth-with-password`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: this.getHeaders(),
|
||||
body: JSON.stringify({ identity, password }),
|
||||
}
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to fetch device');
|
||||
}
|
||||
if (!response.ok) {
|
||||
const response = await fetch(
|
||||
`${this.address}/collections/_superusers/auth-with-password`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: this.getHeaders(),
|
||||
body: JSON.stringify({ identity, password }),
|
||||
}
|
||||
);
|
||||
|
||||
return response.json();
|
||||
}
|
||||
if (!response.ok) {
|
||||
const error = await response.json();
|
||||
throw new Error(error.message || "Authentication failed");
|
||||
}
|
||||
|
||||
async createDevice(device: Partial<Device>): Promise<Device> {
|
||||
const response = await fetch(
|
||||
`${API_BASE_URL}/collections/devices/records`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: this.getHeaders(),
|
||||
body: JSON.stringify(device),
|
||||
}
|
||||
);
|
||||
const data: AuthResponse = await response.json();
|
||||
this.token = data.token;
|
||||
return data;
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
const error = await response.json();
|
||||
throw new Error(error.message || 'Failed to create device');
|
||||
}
|
||||
const data: AuthResponse = await response.json();
|
||||
this.token = data.token;
|
||||
return data;
|
||||
}
|
||||
|
||||
return response.json();
|
||||
}
|
||||
async getDevices(page = 1, perPage = 100): Promise<Device[]> {
|
||||
const response = await fetch(
|
||||
`${this.address}/collections/devices/records?page=${page}&perPage=${perPage}`,
|
||||
{
|
||||
headers: this.getHeaders(),
|
||||
}
|
||||
);
|
||||
|
||||
async updateDevice(id: string, device: Partial<Device>): Promise<Device> {
|
||||
const response = await fetch(
|
||||
`${API_BASE_URL}/collections/devices/records/${id}`,
|
||||
{
|
||||
method: 'PATCH',
|
||||
headers: this.getHeaders(),
|
||||
body: JSON.stringify(device),
|
||||
}
|
||||
);
|
||||
if (!response.ok) {
|
||||
throw new Error("Failed to fetch devices");
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
const error = await response.json();
|
||||
throw new Error(error.message || 'Failed to update device');
|
||||
}
|
||||
const data = await response.json();
|
||||
return data.items;
|
||||
}
|
||||
|
||||
return response.json();
|
||||
}
|
||||
async getDevice(id: string): Promise<Device> {
|
||||
const response = await fetch(
|
||||
`${this.address}/collections/devices/records/${id}`,
|
||||
{
|
||||
headers: this.getHeaders(),
|
||||
}
|
||||
);
|
||||
|
||||
async deleteDevice(id: string): Promise<void> {
|
||||
const response = await fetch(
|
||||
`${API_BASE_URL}/collections/devices/records/${id}`,
|
||||
{
|
||||
method: 'DELETE',
|
||||
headers: this.getHeaders(),
|
||||
}
|
||||
);
|
||||
if (!response.ok) {
|
||||
throw new Error("Failed to fetch device");
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to delete device');
|
||||
}
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async wakeDevice(id: string): Promise<void> {
|
||||
const response = await fetch(
|
||||
`${API_BASE_URL}/upsnap/wake/${id}`,
|
||||
{
|
||||
headers: this.getHeaders(),
|
||||
}
|
||||
);
|
||||
async createDevice(device: Partial<Device>): Promise<Device> {
|
||||
const response = await fetch(
|
||||
`${this.address}/collections/devices/records`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: this.getHeaders(),
|
||||
body: JSON.stringify(device),
|
||||
}
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to wake device');
|
||||
}
|
||||
}
|
||||
if (!response.ok) {
|
||||
const error = await response.json();
|
||||
throw new Error(error.message || "Failed to create device");
|
||||
}
|
||||
|
||||
async wakeGroup(id: string): Promise<void> {
|
||||
const response = await fetch(
|
||||
`${API_BASE_URL}/upsnap/wakegroup/${id}`,
|
||||
{
|
||||
headers: this.getHeaders(),
|
||||
}
|
||||
);
|
||||
return response.json();
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to wake group');
|
||||
}
|
||||
}
|
||||
async updateDevice(id: string, device: Partial<Device>): Promise<Device> {
|
||||
const response = await fetch(
|
||||
`${this.address}/collections/devices/records/${id}`,
|
||||
{
|
||||
method: "PATCH",
|
||||
headers: this.getHeaders(),
|
||||
body: JSON.stringify(device),
|
||||
}
|
||||
);
|
||||
|
||||
async sleepDevice(id: string): Promise<void> {
|
||||
const response = await fetch(
|
||||
`${API_BASE_URL}/upsnap/sleep/${id}`,
|
||||
{
|
||||
headers: this.getHeaders(),
|
||||
}
|
||||
);
|
||||
if (!response.ok) {
|
||||
const error = await response.json();
|
||||
throw new Error(error.message || "Failed to update device");
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to sleep device');
|
||||
}
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async rebootDevice(id: string): Promise<void> {
|
||||
const response = await fetch(
|
||||
`${API_BASE_URL}/upsnap/reboot/${id}`,
|
||||
{
|
||||
headers: this.getHeaders(),
|
||||
}
|
||||
);
|
||||
async deleteDevice(id: string): Promise<void> {
|
||||
const response = await fetch(
|
||||
`${this.address}/collections/devices/records/${id}`,
|
||||
{
|
||||
method: "DELETE",
|
||||
headers: this.getHeaders(),
|
||||
}
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to reboot device');
|
||||
}
|
||||
}
|
||||
if (!response.ok) {
|
||||
throw new Error("Failed to delete device");
|
||||
}
|
||||
}
|
||||
|
||||
async shutdownDevice(id: string): Promise<void> {
|
||||
const response = await fetch(
|
||||
`${API_BASE_URL}/upsnap/shutdown/${id}`,
|
||||
{
|
||||
headers: this.getHeaders(),
|
||||
}
|
||||
);
|
||||
async wakeDevice(id: string): Promise<void> {
|
||||
const response = await fetch(`${this.address}/upsnap/wake/${id}`, {
|
||||
headers: this.getHeaders(),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to shutdown device');
|
||||
}
|
||||
}
|
||||
if (!response.ok) {
|
||||
throw new Error("Failed to wake device");
|
||||
}
|
||||
}
|
||||
|
||||
async scanNetwork(): Promise<NetworkScanResult[]> {
|
||||
const response = await fetch(
|
||||
`${API_BASE_URL}/upsnap/scan`,
|
||||
{
|
||||
headers: this.getHeaders(),
|
||||
}
|
||||
);
|
||||
async wakeGroup(id: string): Promise<void> {
|
||||
const response = await fetch(`${this.address}/upsnap/wakegroup/${id}`, {
|
||||
headers: this.getHeaders(),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to scan network');
|
||||
}
|
||||
if (!response.ok) {
|
||||
throw new Error("Failed to wake group");
|
||||
}
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
console.log('Raw scan data:', data);
|
||||
|
||||
if (data.devices && Array.isArray(data.devices)) {
|
||||
return data.devices.map((item: any) => ({
|
||||
name: item.name || item.hostname || 'Unknown',
|
||||
ip: item.ip || item.ip_address || '',
|
||||
mac: item.mac || item.mac_address || '',
|
||||
mac_vendor: item.mac_vendor || 'Unknown',
|
||||
}));
|
||||
}
|
||||
|
||||
if (Array.isArray(data)) {
|
||||
return data.map((item: any) => ({
|
||||
name: item.name || item.hostname || 'Unknown',
|
||||
ip: item.ip || item.ip_address || '',
|
||||
mac: item.mac || item.mac_address || '',
|
||||
mac_vendor: item.mac_vendor || 'Unknown',
|
||||
}));
|
||||
}
|
||||
|
||||
if (data.items && Array.isArray(data.items)) {
|
||||
return data.items.map((item: any) => ({
|
||||
name: item.name || item.hostname || 'Unknown',
|
||||
ip: item.ip || item.ip_address || '',
|
||||
mac: item.mac || item.mac_address || '',
|
||||
mac_vendor: item.mac_vendor || 'Unknown',
|
||||
}));
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
async sleepDevice(id: string): Promise<void> {
|
||||
const response = await fetch(`${this.address}/upsnap/sleep/${id}`, {
|
||||
headers: this.getHeaders(),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error("Failed to sleep device");
|
||||
}
|
||||
}
|
||||
|
||||
async rebootDevice(id: string): Promise<void> {
|
||||
const response = await fetch(`${this.address}/upsnap/reboot/${id}`, {
|
||||
headers: this.getHeaders(),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error("Failed to reboot device");
|
||||
}
|
||||
}
|
||||
|
||||
async shutdownDevice(id: string): Promise<void> {
|
||||
const response = await fetch(`${this.address}/upsnap/shutdown/${id}`, {
|
||||
headers: this.getHeaders(),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error("Failed to shutdown device");
|
||||
}
|
||||
}
|
||||
|
||||
async scanNetwork(): Promise<NetworkScanResult[]> {
|
||||
const response = await fetch(`${this.address}/upsnap/scan`, {
|
||||
headers: this.getHeaders(),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error("Failed to scan network");
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
if (data.devices && Array.isArray(data.devices)) {
|
||||
return data.devices.map((item: any) => ({
|
||||
name: item.name || item.hostname || "Unknown",
|
||||
ip: item.ip || item.ip_address || "",
|
||||
mac: item.mac || item.mac_address || "",
|
||||
mac_vendor: item.mac_vendor || "Unknown",
|
||||
}));
|
||||
}
|
||||
|
||||
if (Array.isArray(data)) {
|
||||
return data.map((item: any) => ({
|
||||
name: item.name || item.hostname || "Unknown",
|
||||
ip: item.ip || item.ip_address || "",
|
||||
mac: item.mac || item.mac_address || "",
|
||||
mac_vendor: item.mac_vendor || "Unknown",
|
||||
}));
|
||||
}
|
||||
|
||||
if (data.items && Array.isArray(data.items)) {
|
||||
return data.items.map((item: any) => ({
|
||||
name: item.name || item.hostname || "Unknown",
|
||||
ip: item.ip || item.ip_address || "",
|
||||
mac: item.mac || item.mac_address || "",
|
||||
mac_vendor: item.mac_vendor || "Unknown",
|
||||
}));
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
export default new UpSnapAPI();
|
||||
|
||||
@@ -1,50 +1,50 @@
|
||||
export interface Device {
|
||||
id: string;
|
||||
collectionId: string;
|
||||
collectionName: string;
|
||||
name: string;
|
||||
mac: string;
|
||||
ip: string;
|
||||
netmask: string;
|
||||
broadcast: string;
|
||||
secureOnPassword: string;
|
||||
port: number;
|
||||
groups: string[];
|
||||
status: string;
|
||||
created: string;
|
||||
updated: string;
|
||||
id: string;
|
||||
collectionId: string;
|
||||
collectionName: string;
|
||||
name: string;
|
||||
mac: string;
|
||||
ip: string;
|
||||
netmask: string;
|
||||
broadcast: string;
|
||||
secureOnPassword: string;
|
||||
port: number;
|
||||
groups: string[];
|
||||
status: string;
|
||||
created: string;
|
||||
updated: string;
|
||||
}
|
||||
|
||||
export interface AuthResponse {
|
||||
token: string;
|
||||
record: User;
|
||||
token: string;
|
||||
record: User;
|
||||
}
|
||||
|
||||
export interface User {
|
||||
id: string;
|
||||
collectionId: string;
|
||||
collectionName: string;
|
||||
username: string;
|
||||
verified: boolean;
|
||||
emailVisibility: boolean;
|
||||
email: string;
|
||||
created: string;
|
||||
updated: string;
|
||||
name: string;
|
||||
avatar: number;
|
||||
id: string;
|
||||
collectionId: string;
|
||||
collectionName: string;
|
||||
username: string;
|
||||
verified: boolean;
|
||||
emailVisibility: boolean;
|
||||
email: string;
|
||||
created: string;
|
||||
updated: string;
|
||||
name: string;
|
||||
avatar: number;
|
||||
}
|
||||
|
||||
export interface DeviceGroup {
|
||||
id: string;
|
||||
name: string;
|
||||
id: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface NetworkScanResult {
|
||||
name?: string;
|
||||
hostname?: string;
|
||||
ip?: string;
|
||||
ip_address?: string;
|
||||
mac?: string;
|
||||
mac_address?: string;
|
||||
mac_vendor?: string;
|
||||
name?: string;
|
||||
hostname?: string;
|
||||
ip?: string;
|
||||
ip_address?: string;
|
||||
mac?: string;
|
||||
mac_address?: string;
|
||||
mac_vendor?: string;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user