2024-12-02 11:02:30 +01:00
|
|
|
import {
|
2024-12-10 13:47:27 +01:00
|
|
|
GetInitInfoResponse,
|
2024-12-02 11:02:30 +01:00
|
|
|
LoginRequest,
|
2024-12-16 17:56:52 +01:00
|
|
|
LoginResponse,
|
2024-12-10 13:47:27 +01:00
|
|
|
LogoutResponse,
|
2024-12-02 11:02:30 +01:00
|
|
|
PasswordResetRequest,
|
2024-12-10 13:47:27 +01:00
|
|
|
PasswordResetResponse,
|
2024-12-02 11:02:30 +01:00
|
|
|
PasswordResetUpdateRequest,
|
2024-12-10 13:47:27 +01:00
|
|
|
PasswordResetUpdateResponse,
|
|
|
|
SetInitInfoRequest,
|
2024-12-16 17:56:52 +01:00
|
|
|
SetInitInfoResponse,
|
2024-12-02 11:02:30 +01:00
|
|
|
} from "@budibase/types"
|
2024-11-27 12:02:16 +01:00
|
|
|
import { BaseAPIClient } from "./types"
|
|
|
|
|
|
|
|
export interface AuthEndpoints {
|
2024-12-16 17:56:52 +01:00
|
|
|
logIn: (
|
|
|
|
tenantId: string,
|
|
|
|
username: string,
|
|
|
|
password: string
|
|
|
|
) => Promise<LoginResponse>
|
2024-12-10 13:47:27 +01:00
|
|
|
logOut: () => Promise<LogoutResponse>
|
2024-11-27 12:02:16 +01:00
|
|
|
requestForgotPassword: (
|
|
|
|
tenantId: string,
|
|
|
|
email: string
|
2024-12-10 13:47:27 +01:00
|
|
|
) => Promise<PasswordResetResponse>
|
2024-11-27 12:02:16 +01:00
|
|
|
resetPassword: (
|
|
|
|
tenantId: string,
|
|
|
|
password: string,
|
|
|
|
resetCode: string
|
2024-12-10 13:47:27 +01:00
|
|
|
) => Promise<PasswordResetUpdateResponse>
|
2024-12-16 17:56:52 +01:00
|
|
|
setInitInfo: (info: SetInitInfoRequest) => Promise<SetInitInfoResponse>
|
2024-12-10 13:47:27 +01:00
|
|
|
getInitInfo: () => Promise<GetInitInfoResponse>
|
2024-11-27 12:02:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export const buildAuthEndpoints = (API: BaseAPIClient): AuthEndpoints => ({
|
2022-01-20 10:40:53 +01:00
|
|
|
/**
|
2022-01-24 15:32:27 +01:00
|
|
|
* Performs a login request.
|
|
|
|
* @param tenantId the ID of the tenant to log in to
|
|
|
|
* @param username the username (email)
|
|
|
|
* @param password the password
|
2022-01-20 10:40:53 +01:00
|
|
|
*/
|
2024-11-27 12:02:16 +01:00
|
|
|
logIn: async (tenantId, username, password) => {
|
2024-12-16 17:56:52 +01:00
|
|
|
return await API.post<LoginRequest, LoginResponse>({
|
2022-01-24 15:32:27 +01:00
|
|
|
url: `/api/global/auth/${tenantId}/login`,
|
2022-01-20 10:40:53 +01:00
|
|
|
body: {
|
2022-01-24 15:32:27 +01:00
|
|
|
username,
|
2022-01-20 10:40:53 +01:00
|
|
|
password,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2022-01-20 10:43:23 +01:00
|
|
|
/**
|
|
|
|
* Logs the user out and invalidates their session.
|
|
|
|
*/
|
|
|
|
logOut: async () => {
|
|
|
|
return await API.post({
|
|
|
|
url: "/api/global/auth/logout",
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2022-01-20 10:40:53 +01:00
|
|
|
/**
|
2022-01-24 15:32:27 +01:00
|
|
|
* Sets initialisation info.
|
|
|
|
* @param info the info to set
|
2022-01-20 10:40:53 +01:00
|
|
|
*/
|
2022-01-24 15:32:27 +01:00
|
|
|
setInitInfo: async info => {
|
2022-01-21 10:10:59 +01:00
|
|
|
return await API.post({
|
2022-01-24 15:32:27 +01:00
|
|
|
url: "/api/global/auth/init",
|
|
|
|
body: info,
|
2022-01-21 10:10:59 +01:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2022-01-24 15:32:27 +01:00
|
|
|
* Gets the initialisation info.
|
2022-01-21 10:10:59 +01:00
|
|
|
*/
|
2022-01-24 15:32:27 +01:00
|
|
|
getInitInfo: async () => {
|
2022-01-21 10:10:59 +01:00
|
|
|
return await API.get({
|
2022-01-24 15:32:27 +01:00
|
|
|
url: "/api/global/auth/init",
|
2022-01-21 10:10:59 +01:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2022-01-24 15:32:27 +01:00
|
|
|
* Sends a password reset email.
|
|
|
|
* @param tenantId the ID of the tenant the user is in
|
|
|
|
* @param email the email address of the user
|
2022-01-21 10:10:59 +01:00
|
|
|
*/
|
2024-11-27 12:02:16 +01:00
|
|
|
requestForgotPassword: async (tenantId, email) => {
|
2024-12-10 13:47:27 +01:00
|
|
|
return await API.post<PasswordResetRequest, PasswordResetResponse>({
|
2022-01-24 15:32:27 +01:00
|
|
|
url: `/api/global/auth/${tenantId}/reset`,
|
|
|
|
body: {
|
|
|
|
email,
|
|
|
|
},
|
2022-01-21 16:09:27 +01:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2022-01-24 15:32:27 +01:00
|
|
|
* Resets a user's password.
|
|
|
|
* @param tenantId the ID of the tenant the user is in
|
|
|
|
* @param password the new password to set
|
|
|
|
* @param resetCode the reset code to authenticate the request
|
2022-01-21 16:09:27 +01:00
|
|
|
*/
|
2024-11-27 12:02:16 +01:00
|
|
|
resetPassword: async (tenantId, password, resetCode) => {
|
2024-12-10 13:47:27 +01:00
|
|
|
return await API.post<
|
|
|
|
PasswordResetUpdateRequest,
|
|
|
|
PasswordResetUpdateResponse
|
|
|
|
>({
|
2022-01-24 15:32:27 +01:00
|
|
|
url: `/api/global/auth/${tenantId}/reset/update`,
|
|
|
|
body: {
|
|
|
|
password,
|
|
|
|
resetCode,
|
|
|
|
},
|
2022-01-21 16:09:27 +01:00
|
|
|
})
|
|
|
|
},
|
2022-01-20 10:40:53 +01:00
|
|
|
})
|