2023-01-22 19:46:56 +01:00
|
|
|
import axios from 'axios'
|
|
|
|
import process from 'process'
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Helper methods to interact with the FreeSewing backend
|
|
|
|
*/
|
2023-02-19 20:49:15 +01:00
|
|
|
const apiHandler = axios.create({
|
2023-01-22 19:46:56 +01:00
|
|
|
baseURL: process.env.NEXT_PUBLIC_BACKEND || 'https://backend.freesewing.org',
|
|
|
|
timeout: 3000,
|
|
|
|
})
|
|
|
|
|
2023-01-29 16:44:02 +01:00
|
|
|
export function useBackend(app) {
|
2023-01-22 19:46:56 +01:00
|
|
|
const auth = {
|
|
|
|
headers: { Authorization: 'Bearer ' + app.token },
|
|
|
|
}
|
|
|
|
|
2023-02-19 20:49:15 +01:00
|
|
|
const api = {
|
|
|
|
get: async (uri, config = {}) => {
|
|
|
|
let result
|
|
|
|
try {
|
|
|
|
result = await apiHandler.get(uri, config)
|
|
|
|
return result
|
|
|
|
} catch (err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
post: async (uri, data = null, config = {}) => {
|
|
|
|
let result
|
|
|
|
try {
|
|
|
|
result = await apiHandler.post(uri, data, config)
|
|
|
|
return result
|
|
|
|
} catch (err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
patch: async (uri, data = null, config = {}) => {
|
|
|
|
let result
|
|
|
|
try {
|
|
|
|
result = await apiHandler.patch(uri, data, config)
|
|
|
|
return result
|
|
|
|
} catch (err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
delete: async (uri, config = {}) => {
|
|
|
|
let result
|
|
|
|
try {
|
|
|
|
result = await apiHandler.delete(uri, data, config)
|
|
|
|
return result
|
|
|
|
} catch (err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-01-22 19:46:56 +01:00
|
|
|
const backend = {}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* User signup
|
|
|
|
*/
|
|
|
|
backend.signUp = async ({ email, language }) => {
|
2023-02-19 20:49:15 +01:00
|
|
|
const result = await api.post('/signup', { email, language })
|
2023-01-22 19:46:56 +01:00
|
|
|
if (result && result.status === 201 && result.data) return result.data
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Load confirmation
|
|
|
|
*/
|
2023-01-24 21:17:49 +01:00
|
|
|
backend.loadConfirmation = async ({ id, check }) => {
|
2023-02-19 20:49:15 +01:00
|
|
|
const result = await api.get(`/confirmations/${id}/${check}`)
|
2023-01-22 19:46:56 +01:00
|
|
|
if (result && result.status === 201 && result.data) return result.data
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Confirm signup
|
|
|
|
*/
|
2023-01-24 21:17:49 +01:00
|
|
|
backend.confirmSignup = async ({ id, consent }) => {
|
2023-02-19 20:49:15 +01:00
|
|
|
const result = await api.post(`/confirm/signup/${id}`, { consent })
|
2023-01-22 19:46:56 +01:00
|
|
|
if (result && result.status === 200 && result.data) return result.data
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Generic update account method
|
|
|
|
*/
|
|
|
|
backend.updateAccount = async (data) => {
|
2023-02-19 20:49:15 +01:00
|
|
|
const result = await api.patch(`/account/jwt`, data, auth)
|
2023-01-22 19:46:56 +01:00
|
|
|
if (result && result.status === 200 && result.data?.account) {
|
|
|
|
app.setAccount(result.data.account)
|
|
|
|
return true
|
|
|
|
}
|
2023-02-19 20:49:15 +01:00
|
|
|
console.log('backend result', result)
|
2023-01-22 19:46:56 +01:00
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-01-24 21:17:49 +01:00
|
|
|
/*
|
|
|
|
* Checks whether a username is available
|
|
|
|
*/
|
|
|
|
backend.isUsernameAvailable = async (username) => {
|
2023-02-19 20:49:15 +01:00
|
|
|
const result = await api.post(`/available/username/jwt`, { username }, auth)
|
|
|
|
// 404 means username is available, which is success in this case
|
|
|
|
if (result.response?.status === 404) return true
|
|
|
|
|
2023-01-24 21:17:49 +01:00
|
|
|
return false
|
|
|
|
}
|
2023-01-22 19:46:56 +01:00
|
|
|
|
2023-02-18 16:11:02 +01:00
|
|
|
/*
|
|
|
|
* Remove account method
|
|
|
|
*/
|
2023-02-19 20:49:15 +01:00
|
|
|
backend.removeAccount = async () => {
|
|
|
|
const result = await api.delete(`/account/jwt`, auth)
|
|
|
|
if (result && result.status === 200 && result.data?.account) {
|
|
|
|
app.setAccount(result.data.account)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Enable MFA
|
|
|
|
*/
|
|
|
|
backend.enableMfa = async () => {
|
|
|
|
const result = await api.post(`/account/mfa/jwt`, { mfa: true }, auth)
|
|
|
|
if (result && result.status === 200 && result.data?.mfa) {
|
|
|
|
return result.data.mfa
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Confirm MFA
|
|
|
|
*/
|
|
|
|
backend.confirmMfa = async (data) => {
|
|
|
|
const result = await api.post(`/account/mfa/jwt`, { ...data, mfa: true }, auth)
|
|
|
|
if (result && result.status === 200 && result.data?.account) {
|
|
|
|
app.setAccount(result.data.account)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Disable MFA
|
|
|
|
*/
|
|
|
|
backend.disableMfa = async (data) => {
|
|
|
|
const result = await await api.post(`/account/mfa/jwt`, { ...data, mfa: false }, auth)
|
|
|
|
if (result && result.status === 200 && result.data?.account) {
|
|
|
|
app.setAccount(result.data.account)
|
|
|
|
return true
|
2023-02-18 16:11:02 +01:00
|
|
|
}
|
2023-02-19 20:49:15 +01:00
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reload account
|
|
|
|
*/
|
|
|
|
backend.reloadAccount = async () => {
|
|
|
|
const result = await await api.get(`/whoami/jwt`, auth)
|
2023-02-18 16:11:02 +01:00
|
|
|
if (result && result.status === 200 && result.data?.account) {
|
|
|
|
app.setAccount(result.data.account)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-02-19 20:49:15 +01:00
|
|
|
/*
|
|
|
|
* Create API key
|
|
|
|
*/
|
|
|
|
backend.createApikey = async (data) => {
|
|
|
|
const result = await await api.post(`/apikeys/jwt`, data, auth)
|
|
|
|
if (result && result.status === 201 && result.data?.apikey) {
|
|
|
|
return result.data.apikey
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-01-22 19:46:56 +01:00
|
|
|
return backend
|
|
|
|
}
|