2023-07-13 23:48:44 +12:00
|
|
|
import { Response } from "node-fetch"
|
|
|
|
import AccountInternalAPIClient from "../AccountInternalAPIClient"
|
|
|
|
import { APIRequestOpts } from "../../../types"
|
2023-07-19 08:14:07 +12:00
|
|
|
import BaseAPI from "./BaseAPI"
|
2023-08-01 02:09:45 +12:00
|
|
|
import { Header } from "@budibase/backend-core"
|
2023-07-13 23:48:44 +12:00
|
|
|
|
2023-07-19 08:14:07 +12:00
|
|
|
export default class AuthAPI extends BaseAPI {
|
2023-07-13 23:48:44 +12:00
|
|
|
client: AccountInternalAPIClient
|
|
|
|
|
|
|
|
constructor(client: AccountInternalAPIClient) {
|
2023-07-19 08:14:07 +12:00
|
|
|
super()
|
2023-07-13 23:48:44 +12:00
|
|
|
this.client = client
|
|
|
|
}
|
|
|
|
|
|
|
|
async login(
|
|
|
|
email: string,
|
|
|
|
password: string,
|
2023-07-19 08:14:07 +12:00
|
|
|
opts: APIRequestOpts = { doExpect: true, status: 200 }
|
2023-07-13 23:48:44 +12:00
|
|
|
): Promise<[Response, string]> {
|
2023-07-19 08:14:07 +12:00
|
|
|
return this.doRequest(async () => {
|
2023-07-20 02:42:12 +12:00
|
|
|
const [res] = await this.client.post(`/api/auth/login`, {
|
|
|
|
body: {
|
|
|
|
email: email,
|
|
|
|
password: password,
|
|
|
|
},
|
|
|
|
})
|
2023-07-19 08:14:07 +12:00
|
|
|
const cookie = res.headers.get("set-cookie")
|
|
|
|
return [res, cookie]
|
|
|
|
}, opts)
|
2023-07-13 23:48:44 +12:00
|
|
|
}
|
2023-08-01 02:09:45 +12:00
|
|
|
|
|
|
|
async logout(opts: APIRequestOpts = { status: 200 }) {
|
|
|
|
return this.doRequest(() => {
|
|
|
|
return this.client.post(`/api/auth/logout`)
|
|
|
|
}, opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
async resetPassword(
|
|
|
|
email: string,
|
|
|
|
opts: APIRequestOpts = { status: 200 }
|
|
|
|
): Promise<[Response, string]> {
|
|
|
|
return this.doRequest(async () => {
|
|
|
|
const [response] = await this.client.post(`/api/auth/reset`, {
|
|
|
|
body: { email },
|
|
|
|
headers: {
|
|
|
|
[Header.RETURN_RESET_PASSWORD_CODE]: "1",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
const code = response.headers.get(Header.RESET_PASSWORD_CODE)
|
|
|
|
return [response, code]
|
|
|
|
}, opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
async resetPasswordUpdate(
|
|
|
|
resetCode: string,
|
|
|
|
password: string,
|
|
|
|
opts: APIRequestOpts = { status: 200 }
|
|
|
|
) {
|
|
|
|
return this.doRequest(() => {
|
|
|
|
return this.client.post(`/api/auth/reset/update`, {
|
|
|
|
body: {
|
|
|
|
resetCode: resetCode,
|
|
|
|
password: password,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}, opts)
|
|
|
|
}
|
2023-07-13 23:48:44 +12:00
|
|
|
}
|