2020-11-19 19:39:22 +01:00
|
|
|
import API from "./api"
|
2021-01-28 15:29:35 +01:00
|
|
|
import { enrichRows } from "./rows"
|
|
|
|
import { TableNames } from "../constants"
|
2020-11-11 13:25:50 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs a log in request.
|
|
|
|
*/
|
2020-12-04 13:22:45 +01:00
|
|
|
export const logIn = async ({ email, password }) => {
|
|
|
|
if (!email) {
|
|
|
|
return API.error("Please enter your email")
|
2020-11-11 13:25:50 +01:00
|
|
|
}
|
|
|
|
if (!password) {
|
2020-11-19 19:39:22 +01:00
|
|
|
return API.error("Please enter your password")
|
2020-11-11 13:25:50 +01:00
|
|
|
}
|
2020-11-19 19:39:22 +01:00
|
|
|
return await API.post({
|
2021-08-04 11:02:24 +02:00
|
|
|
url: "/api/admin/auth",
|
2021-04-13 17:56:45 +02:00
|
|
|
body: { username: email, password },
|
2020-11-11 13:25:50 +01:00
|
|
|
})
|
|
|
|
}
|
2021-01-28 15:29:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches the currently logged in user object
|
|
|
|
*/
|
|
|
|
export const fetchSelf = async () => {
|
|
|
|
const user = await API.get({ url: "/api/self" })
|
2021-08-04 11:02:24 +02:00
|
|
|
if (user?._id) {
|
2021-07-07 12:28:35 +02:00
|
|
|
if (user.roleId === "PUBLIC") {
|
|
|
|
// Don't try to enrich a public user as it will 403
|
|
|
|
return user
|
|
|
|
} else {
|
|
|
|
return (await enrichRows([user], TableNames.USERS))[0]
|
|
|
|
}
|
2021-01-28 15:29:35 +01:00
|
|
|
} else {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|