budibase/packages/client/src/api/auth.js

37 lines
837 B
JavaScript
Raw Normal View History

import API from "./api"
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) {
return API.error("Please enter your password")
2020-11-11 13:25:50 +01:00
}
return await API.post({
url: "/api/global/auth",
body: { username: email, password },
2020-11-11 13:25:50 +01:00
})
}
/**
* Fetches the currently logged in user object
*/
export const fetchSelf = async () => {
const user = await API.get({ url: "/api/self" })
if (user && user._id) {
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]
}
} else {
return null
}
}