2023-01-22 19:46:56 +01:00
|
|
|
import axios from 'axios'
|
|
|
|
import process from 'process'
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Helper methods to interact with the FreeSewing backend
|
|
|
|
*/
|
|
|
|
const api = axios.create({
|
|
|
|
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 },
|
|
|
|
}
|
|
|
|
|
|
|
|
const backend = {}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* User signup
|
|
|
|
*/
|
|
|
|
backend.signUp = async ({ email, language }) => {
|
|
|
|
let result
|
|
|
|
try {
|
|
|
|
app.startLoading()
|
|
|
|
result = await api.post('/signup', { email, language })
|
|
|
|
} catch (err) {
|
|
|
|
return err
|
|
|
|
} finally {
|
|
|
|
app.stopLoading()
|
|
|
|
}
|
|
|
|
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-01-22 19:46:56 +01:00
|
|
|
let result
|
|
|
|
try {
|
|
|
|
app.startLoading()
|
|
|
|
result = await api.get(`/confirmations/${id}/${check}`)
|
|
|
|
} catch (err) {
|
|
|
|
return err
|
|
|
|
} finally {
|
|
|
|
app.stopLoading()
|
|
|
|
}
|
|
|
|
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-01-22 19:46:56 +01:00
|
|
|
let result
|
|
|
|
try {
|
|
|
|
app.startLoading()
|
|
|
|
result = await api.post(`/confirm/signup/${id}`, { consent })
|
|
|
|
} catch (err) {
|
|
|
|
return err
|
|
|
|
} finally {
|
|
|
|
app.stopLoading()
|
|
|
|
}
|
|
|
|
if (result && result.status === 200 && result.data) return result.data
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Generic update account method
|
|
|
|
*/
|
|
|
|
backend.updateAccount = async (data) => {
|
|
|
|
let result
|
|
|
|
try {
|
|
|
|
app.startLoading()
|
|
|
|
result = await api.patch(`/account/jwt`, data, auth)
|
|
|
|
} catch (err) {
|
|
|
|
return err
|
|
|
|
} finally {
|
|
|
|
app.stopLoading()
|
|
|
|
}
|
|
|
|
if (result && result.status === 200 && result.data?.account) {
|
|
|
|
app.setAccount(result.data.account)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-01-24 21:17:49 +01:00
|
|
|
/*
|
|
|
|
* Checks whether a username is available
|
|
|
|
*/
|
|
|
|
backend.isUsernameAvailable = async (username) => {
|
|
|
|
try {
|
|
|
|
app.startLoading()
|
2023-01-27 21:09:44 +01:00
|
|
|
await api.post(`/available/username/jwt`, { username }, auth)
|
2023-01-24 21:17:49 +01:00
|
|
|
} catch (err) {
|
|
|
|
// 404 means user is not found, so the username is available
|
|
|
|
if (err.response?.status === 404) return true
|
|
|
|
return false
|
|
|
|
} finally {
|
|
|
|
app.stopLoading()
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2023-01-22 19:46:56 +01:00
|
|
|
|
|
|
|
return backend
|
|
|
|
}
|