2023-07-03 21:22:31 +08:00
|
|
|
import axios from "axios";
|
|
|
|
import AsyncStorage from "@react-native-async-storage/async-storage";
|
|
|
|
import {
|
|
|
|
ActivationParams,
|
|
|
|
LoginParams,
|
|
|
|
RegistrationParams,
|
|
|
|
} from "../../interfaces/Interfaces";
|
|
|
|
|
|
|
|
let debug = true;
|
|
|
|
export let backendURL = "";
|
|
|
|
if (debug) {
|
|
|
|
backendURL = "http://10.0.10.8:8000";
|
|
|
|
} else {
|
|
|
|
backendURL = "https://keannu125.pythonanywhere.com";
|
|
|
|
}
|
|
|
|
|
|
|
|
const instance = axios.create({
|
|
|
|
baseURL: backendURL,
|
|
|
|
timeout: 1000,
|
|
|
|
});
|
|
|
|
|
|
|
|
// App APIs
|
|
|
|
|
2023-07-04 14:18:48 +08:00
|
|
|
// Token Handling
|
|
|
|
export async function getAccessToken() {
|
|
|
|
const accessToken = await AsyncStorage.getItem("access_token");
|
|
|
|
return accessToken;
|
|
|
|
}
|
2023-07-03 21:22:31 +08:00
|
|
|
|
2023-07-04 14:18:48 +08:00
|
|
|
export async function getRefreshToken() {
|
|
|
|
const refreshToken = await AsyncStorage.getItem("refresh_token");
|
|
|
|
return refreshToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function setAccessToken(access: string) {
|
|
|
|
await AsyncStorage.setItem("access_token", access);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function setRefreshToken(refresh: string) {
|
|
|
|
await AsyncStorage.setItem("refresh_token", refresh);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// User APIs
|
2023-07-03 21:22:31 +08:00
|
|
|
export function UserRegister(register: RegistrationParams) {
|
|
|
|
console.log(JSON.stringify(register));
|
|
|
|
return instance
|
|
|
|
.post("/api/v1/accounts/users/", register)
|
|
|
|
.then(async (response) => {
|
2023-07-04 14:34:31 +08:00
|
|
|
return [true, response.status];
|
2023-07-03 21:22:31 +08:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
2023-07-04 14:34:31 +08:00
|
|
|
return [false, error.response.status, error.response.data];
|
2023-07-03 21:22:31 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function UserLogin(user: LoginParams) {
|
|
|
|
return instance
|
2023-07-03 23:19:36 +08:00
|
|
|
.post("/api/v1/accounts/jwt/create/", user)
|
2023-07-03 21:22:31 +08:00
|
|
|
.then(async (response) => {
|
2023-07-04 14:18:48 +08:00
|
|
|
console.log(
|
|
|
|
"Access Token:",
|
|
|
|
response.data.access,
|
|
|
|
"\nRefresh Token:",
|
|
|
|
response.data.refresh
|
2023-07-03 23:19:36 +08:00
|
|
|
);
|
2023-07-04 14:18:48 +08:00
|
|
|
setAccessToken(response.data.access);
|
|
|
|
setRefreshToken(response.data.refresh);
|
2023-07-03 23:19:36 +08:00
|
|
|
return [
|
|
|
|
true,
|
|
|
|
JSON.stringify(response.data.access),
|
|
|
|
JSON.stringify(response.data.refresh),
|
|
|
|
];
|
2023-07-03 21:22:31 +08:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
2023-07-03 23:19:36 +08:00
|
|
|
console.log("Login Failed:" + error.response.data);
|
2023-07-03 22:11:04 +08:00
|
|
|
return [false, error.response.data];
|
2023-07-03 21:22:31 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-04 14:18:48 +08:00
|
|
|
export async function TokenRefresh() {
|
|
|
|
const refresh = await getRefreshToken();
|
|
|
|
// console.log("Refresh token", refresh);
|
2023-07-03 23:19:36 +08:00
|
|
|
return instance
|
2023-07-04 14:18:48 +08:00
|
|
|
.post("/api/v1/accounts/jwt/refresh/", {
|
|
|
|
refresh: refresh,
|
|
|
|
})
|
2023-07-03 23:19:36 +08:00
|
|
|
.then(async (response) => {
|
2023-07-04 14:18:48 +08:00
|
|
|
setAccessToken(response.data.access);
|
|
|
|
/*console.log(
|
|
|
|
"Token refresh success! New Access Token",
|
|
|
|
response.data.access
|
|
|
|
);*/
|
2023-07-04 16:41:29 +08:00
|
|
|
return [true, JSON.stringify(response.data.access)];
|
2023-07-03 23:19:36 +08:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
2023-07-04 14:18:48 +08:00
|
|
|
console.log("Refresh Failed: " + JSON.stringify(error.response.data));
|
|
|
|
return [false, error.response.data];
|
2023-07-03 23:19:36 +08:00
|
|
|
});
|
|
|
|
}
|
2023-07-03 21:22:31 +08:00
|
|
|
export async function UserInfo() {
|
2023-07-04 14:18:48 +08:00
|
|
|
const accessToken = await getAccessToken();
|
2023-07-03 21:22:31 +08:00
|
|
|
return instance
|
|
|
|
.get("/api/v1/accounts/users/me/", {
|
|
|
|
headers: {
|
2023-07-04 14:18:48 +08:00
|
|
|
Authorization: `Bearer ${accessToken}`,
|
2023-07-03 21:22:31 +08:00
|
|
|
},
|
|
|
|
})
|
|
|
|
.then((response) => {
|
2023-07-04 14:26:49 +08:00
|
|
|
// console.log(JSON.stringify(response.data));
|
2023-07-03 21:22:31 +08:00
|
|
|
return response.data;
|
2023-07-04 14:18:48 +08:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.log("User Info Error", error.response.data);
|
|
|
|
return [false, error.response.data];
|
2023-07-03 21:22:31 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function UserActivate(activation: ActivationParams) {
|
|
|
|
return instance
|
|
|
|
.post("/api/v1/accounts/users/activation/", activation)
|
|
|
|
.then(async (response) => {
|
|
|
|
return true;
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|