-
-
Save toteto/61d651541f8e93e27f4891ebbac33ea1 to your computer and use it in GitHub Desktop.
Zodios Auth API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
accountSchema, | |
authenticationDataSchema, | |
authenticationResultAuthenticationTokensSchema, | |
authenticationResultChallengeSessionSchema, | |
passwordSchema, | |
registeredAccountSchema, | |
} from '@my/types'; | |
import { makeApi, makeEndpoint, parametersBuilder } from '@zodios/core'; | |
import { z } from 'zod'; | |
import { defaultError } from '../../error'; | |
import { | |
PATH_ACCOUNTS_AUTHENTICATE, | |
PATH_ACCOUNTS_AUTHENTICATE_MFA, | |
PATH_ACCOUNTS_FORGOT_PASSWORD, | |
PATH_ACCOUNTS_LOGOUT, | |
PATH_ACCOUNTS_ME, | |
PATH_ACCOUNTS_MFA_FINALIZE, | |
PATH_ACCOUNTS_MFA_INIT, | |
PATH_ACCOUNTS_MFA_REMOVE, | |
PATH_ACCOUNTS_REFRESH, | |
PATH_ACCOUNTS_REGISTER, | |
PATH_ACCOUNTS_RENEW_PASSWORD, | |
PATH_ACCOUNTS_VERIFY, | |
PATH_ACCOUNTS_VERIFY_RESEND, | |
} from './paths'; | |
const verifyAccountSchema = z.object({ | |
email: z.string().email(), | |
verificationCode: z.string().length(6), | |
}); | |
const registerAccountEndpoint = makeEndpoint({ | |
method: 'post', | |
path: PATH_ACCOUNTS_REGISTER, | |
response: z.object({}), | |
parameters: parametersBuilder() | |
.addBody(registeredAccountSchema.extend({ password: passwordSchema })) | |
.build(), | |
alias: 'registerAccount', | |
errors: [defaultError], | |
}); | |
const verifyEndpoint = makeEndpoint({ | |
method: 'post', | |
path: PATH_ACCOUNTS_VERIFY, | |
response: z.object({}), | |
parameters: parametersBuilder().addBody(verifyAccountSchema).build(), | |
alias: 'postVerify', | |
errors: [defaultError], | |
}); | |
const authenticationResultSchema = z.union([ | |
authenticationResultChallengeSessionSchema, | |
authenticationResultAuthenticationTokensSchema.extend({ | |
authenticationTokens: authenticationResultAuthenticationTokensSchema.shape.authenticationTokens.pick({ | |
accessToken: true, | |
}), | |
}), | |
]); | |
const authenticateEndpoint = makeEndpoint({ | |
method: 'post', | |
path: PATH_ACCOUNTS_AUTHENTICATE, | |
response: z.object({ authenticationResult: authenticationResultSchema }), | |
parameters: parametersBuilder() | |
.addBody( | |
authenticationDataSchema.extend({ | |
rememberMe: z.boolean().optional().default(false), | |
}), | |
) | |
.build(), | |
alias: 'authenticateAccount', | |
errors: [defaultError], | |
}); | |
const authenticateMFAEndpoint = makeEndpoint({ | |
method: 'post', | |
path: PATH_ACCOUNTS_AUTHENTICATE_MFA, | |
response: z.object({ authenticationResult: authenticationResultSchema }), | |
parameters: parametersBuilder() | |
.addBody( | |
z.object({ | |
email: z.string().email(), | |
challengeSession: z.string(), | |
mfaCode: z.string().length(6), | |
rememberMe: z.boolean().optional().default(false), | |
}), | |
) | |
.build(), | |
alias: 'authenticateMfaAccount', | |
errors: [defaultError], | |
}); | |
const updateEndpoint = makeEndpoint({ | |
method: 'patch', | |
path: PATH_ACCOUNTS_ME, | |
response: accountSchema, | |
parameters: parametersBuilder() | |
.addBody(accountSchema.omit({ accountId: true, createdAt: true, updatedAt: true }).partial()) | |
.build(), | |
alias: 'updateAccount', | |
}); | |
const meEndpoint = makeEndpoint({ | |
method: 'get', | |
path: PATH_ACCOUNTS_ME, | |
response: accountSchema, | |
alias: 'me', | |
}); | |
const refreshEndpoint = makeEndpoint({ | |
method: 'get', | |
path: PATH_ACCOUNTS_REFRESH, | |
response: z.object({ accessToken: z.string() }), | |
alias: 'refresh', | |
errors: [defaultError], | |
}); | |
const logoutEndpoint = makeEndpoint({ | |
method: 'get', | |
path: PATH_ACCOUNTS_LOGOUT, | |
response: z.object({}), | |
alias: 'logout', | |
}); | |
const forgotPasswordEndpoint = makeEndpoint({ | |
method: 'post', | |
path: PATH_ACCOUNTS_FORGOT_PASSWORD, | |
response: z.object({}), | |
parameters: parametersBuilder() | |
.addBody( | |
z.object({ | |
email: z.string().email(), | |
}), | |
) | |
.build(), | |
alias: 'forgotPassword', | |
errors: [defaultError], | |
}); | |
const renewPasswordEndpoint = makeEndpoint({ | |
method: 'post', | |
path: PATH_ACCOUNTS_RENEW_PASSWORD, | |
response: z.object({}), | |
parameters: parametersBuilder() | |
.addBody( | |
z.object({ | |
email: z.string().email(), | |
code: z.string(), | |
password: passwordSchema, | |
}), | |
) | |
.build(), | |
alias: 'renewPassword', | |
errors: [defaultError], | |
}); | |
const resendVerificationCodeEndpoint = makeEndpoint({ | |
method: 'post', | |
path: PATH_ACCOUNTS_VERIFY_RESEND, | |
response: z.object({}), | |
parameters: parametersBuilder() | |
.addBody( | |
z.object({ | |
email: z.string().email(), | |
}), | |
) | |
.build(), | |
alias: 'resendVerificationPassword', | |
errors: [defaultError], | |
}); | |
const initiateMfaSetupEndpoint = makeEndpoint({ | |
method: 'post', | |
path: PATH_ACCOUNTS_MFA_INIT, | |
response: z.object({ mfaSecret: z.string(), challengeSession: z.string() }), | |
parameters: parametersBuilder().build(), | |
alias: 'initiateMfaSetup', | |
errors: [defaultError], | |
}); | |
const finalizeMfaSetupEndpoint = makeEndpoint({ | |
method: 'post', | |
path: PATH_ACCOUNTS_MFA_FINALIZE, | |
response: z.object({ isSuccess: z.boolean(), challengeSession: z.string().optional() }), | |
parameters: parametersBuilder() | |
.addBody( | |
z.object({ | |
challengeSession: z.string(), | |
mfaCode: z.string(), | |
}), | |
) | |
.build(), | |
alias: 'finalizeMfaSetup', | |
errors: [defaultError], | |
}); | |
const removeMfaAuthEndpoint = makeEndpoint({ | |
method: 'post', | |
path: PATH_ACCOUNTS_MFA_REMOVE, | |
response: z.object({}), | |
parameters: parametersBuilder() | |
.addBody( | |
z.object({ | |
challengeSession: z.string(), | |
mfaCode: z.string(), | |
email: z.string().email(), | |
}), | |
) | |
.build(), | |
alias: 'removeMfaAuth', | |
errors: [defaultError], | |
}); | |
export const accountsEndpoints = makeApi([ | |
authenticateEndpoint, | |
authenticateMFAEndpoint, | |
registerAccountEndpoint, | |
verifyEndpoint, | |
updateEndpoint, | |
meEndpoint, | |
refreshEndpoint, | |
logoutEndpoint, | |
forgotPasswordEndpoint, | |
renewPasswordEndpoint, | |
resendVerificationCodeEndpoint, | |
initiateMfaSetupEndpoint, | |
finalizeMfaSetupEndpoint, | |
removeMfaAuthEndpoint, | |
]); | |
export type AccountsApi = typeof accountsEndpoints; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment