Auth API typing.

This commit is contained in:
mike12345567 2024-12-04 18:34:16 +00:00
parent cb49beb317
commit 8c8a973c91
2 changed files with 40 additions and 12 deletions

View File

@ -3,14 +3,28 @@ export interface LoginRequest {
password: string
}
export interface LogoutResponse {
message: string
}
export interface SetInitInfoRequest extends Record<string, any> {}
export interface GetInitInfoResponse extends Record<string, any> {}
export interface PasswordResetRequest {
email: string
}
export interface PasswordResetResponse {
message: string
}
export interface PasswordResetUpdateRequest {
resetCode: string
password: string
}
export interface PasswordResetUpdateResponse {
message: string
}
export interface UpdateSelfRequest {
firstName?: string

View File

@ -16,8 +16,15 @@ import {
PasswordResetUpdateRequest,
GoogleInnerConfig,
DatasourceAuthCookie,
LogoutResponse,
UserCtx,
SetInitInfoRequest,
GetInitInfoResponse,
PasswordResetResponse,
PasswordResetUpdateResponse,
} from "@budibase/types"
import env from "../../../environment"
import { Next } from "koa"
import * as authSdk from "../../../sdk/auth"
import * as userSdk from "../../../sdk/users"
@ -52,7 +59,7 @@ async function passportCallback(
ctx.set(Header.TOKEN, token)
}
export const login = async (ctx: Ctx<LoginRequest>, next: any) => {
export const login = async (ctx: Ctx<LoginRequest, void>, next: Next) => {
const email = ctx.request.body.username
const user = await userSdk.db.getUserByEmail(email)
@ -72,7 +79,7 @@ export const login = async (ctx: Ctx<LoginRequest>, next: any) => {
)(ctx, next)
}
export const logout = async (ctx: any) => {
export const logout = async (ctx: UserCtx<void, LogoutResponse>) => {
if (ctx.user && ctx.user._id) {
await authSdk.logout({ ctx, userId: ctx.user._id })
}
@ -81,13 +88,13 @@ export const logout = async (ctx: any) => {
// INIT
export const setInitInfo = (ctx: any) => {
export const setInitInfo = (ctx: UserCtx<SetInitInfoRequest, void>) => {
const initInfo = ctx.request.body
setCookie(ctx, initInfo, Cookie.Init)
ctx.status = 200
}
export const getInitInfo = (ctx: any) => {
export const getInitInfo = (ctx: UserCtx<void, GetInitInfoResponse>) => {
try {
ctx.body = getCookie(ctx, Cookie.Init) || {}
} catch (err) {
@ -101,7 +108,9 @@ export const getInitInfo = (ctx: any) => {
/**
* Reset the user password, used as part of a forgotten password flow.
*/
export const reset = async (ctx: Ctx<PasswordResetRequest>) => {
export const reset = async (
ctx: Ctx<PasswordResetRequest, PasswordResetResponse>
) => {
const { email } = ctx.request.body
await authSdk.reset(email)
@ -114,7 +123,9 @@ export const reset = async (ctx: Ctx<PasswordResetRequest>) => {
/**
* Perform the user password update if the provided reset code is valid.
*/
export const resetUpdate = async (ctx: Ctx<PasswordResetUpdateRequest>) => {
export const resetUpdate = async (
ctx: Ctx<PasswordResetUpdateRequest, PasswordResetUpdateResponse>
) => {
const { resetCode, password } = ctx.request.body
try {
await authSdk.resetUpdate(resetCode, password)
@ -130,7 +141,10 @@ export const resetUpdate = async (ctx: Ctx<PasswordResetUpdateRequest>) => {
// DATASOURCE
export const datasourcePreAuth = async (ctx: any, next: any) => {
export const datasourcePreAuth = async (
ctx: UserCtx<void, void>,
next: Next
) => {
const provider = ctx.params.provider
const { middleware } = require(`@budibase/backend-core`)
const handler = middleware.datasource[provider]
@ -147,7 +161,7 @@ export const datasourcePreAuth = async (ctx: any, next: any) => {
return handler.preAuth(passport, ctx, next)
}
export const datasourceAuth = async (ctx: any, next: any) => {
export const datasourceAuth = async (ctx: UserCtx<void, void>, next: Next) => {
const authStateCookie = getCookie<DatasourceAuthCookie>(
ctx,
Cookie.DatasourceAuth
@ -171,7 +185,7 @@ export async function googleCallbackUrl(config?: GoogleInnerConfig) {
* The initial call that google authentication makes to take you to the google login screen.
* On a successful login, you will be redirected to the googleAuth callback route.
*/
export const googlePreAuth = async (ctx: any, next: any) => {
export const googlePreAuth = async (ctx: Ctx<void, void>, next: Next) => {
const config = await configs.getGoogleConfig()
if (!config) {
return ctx.throw(400, "Google config not found")
@ -190,7 +204,7 @@ export const googlePreAuth = async (ctx: any, next: any) => {
})(ctx, next)
}
export const googleCallback = async (ctx: any, next: any) => {
export const googleCallback = async (ctx: Ctx<void, void>, next: Next) => {
const config = await configs.getGoogleConfig()
if (!config) {
return ctx.throw(400, "Google config not found")
@ -241,7 +255,7 @@ export const oidcStrategyFactory = async (ctx: any) => {
* The initial call that OIDC authentication makes to take you to the configured OIDC login screen.
* On a successful login, you will be redirected to the oidcAuth callback route.
*/
export const oidcPreAuth = async (ctx: Ctx, next: any) => {
export const oidcPreAuth = async (ctx: Ctx<void, void>, next: Next) => {
const { configId } = ctx.params
if (!configId) {
ctx.throw(400, "OIDC config id is required")
@ -266,7 +280,7 @@ export const oidcPreAuth = async (ctx: Ctx, next: any) => {
})(ctx, next)
}
export const oidcCallback = async (ctx: any, next: any) => {
export const oidcCallback = async (ctx: Ctx<void, void>, next: Next) => {
const strategy = await oidcStrategyFactory(ctx)
return passport.authenticate(