diff --git a/.changeset/quiet-clocks-appear.md b/.changeset/quiet-clocks-appear.md new file mode 100644 index 00000000..68aee486 --- /dev/null +++ b/.changeset/quiet-clocks-appear.md @@ -0,0 +1,5 @@ +--- +"@aeriajs/builtins": patch +--- + +feat: add optional parameter to getactivationlink and getredefinepasswordlink to redirect on success diff --git a/packages/builtins/src/collections/user/getActivationLink.ts b/packages/builtins/src/collections/user/getActivationLink.ts index a252bec8..cf5dde0f 100644 --- a/packages/builtins/src/collections/user/getActivationLink.ts +++ b/packages/builtins/src/collections/user/getActivationLink.ts @@ -20,7 +20,7 @@ export const getActivationToken = async (strId: string, context: Context) => { return token } -export const getActivationLink = async (payload: { userId: ObjectId | string }, context: Context) => { +export const getActivationLink = async (payload: { userId: ObjectId | string, redirect?:string}, context: Context) => { if(!context.config.webPublicUrl){ return context.error(HTTPStatus.BadRequest, { code: ActivationError.InvalidLink, @@ -44,8 +44,15 @@ export const getActivationLink = async (payload: { userId: ObjectId | string }, const activationToken = await getActivationToken(payload.userId.toString(), context) - const url = `${context.config.webPublicUrl}/user/activation?step=password&u=${payload.userId.toString()}&t=${activationToken}` - + //const url = `${context.config.webPublicUrl}/user/activation?step=password&u=${payload.userId.toString()}&t=${activationToken}` + const url = new URL(`${context.config.webPublicUrl}/user/activation`) + url.searchParams.set("step", "password"), + url.searchParams.set("u", payload.userId.toString()) + url.searchParams.set("t", activationToken) + if(payload.redirect){ + url.searchParams.set('next', payload.redirect) + } + return Result.result({ url, }) diff --git a/packages/builtins/src/collections/user/getRedefinePasswordLink.ts b/packages/builtins/src/collections/user/getRedefinePasswordLink.ts index ca350333..a878ff2b 100644 --- a/packages/builtins/src/collections/user/getRedefinePasswordLink.ts +++ b/packages/builtins/src/collections/user/getRedefinePasswordLink.ts @@ -4,7 +4,7 @@ import { Result, HTTPStatus } from '@aeriajs/types' import { ActivationError } from './redefinePassword.js' import { getActivationToken } from './getActivationLink.js' -export const getRedefinePasswordLink = async (payload: { userId: ObjectId | string }, context: Context) => { +export const getRedefinePasswordLink = async (payload: { userId: ObjectId | string, redirect?:string }, context: Context) => { if(!context.config.webPublicUrl){ throw new Error('config.webPublicUrl is not set') } @@ -27,8 +27,13 @@ export const getRedefinePasswordLink = async (payload: { userId: ObjectId | stri const redefineToken = await getActivationToken(payload.userId.toString(), context) - const url = `${context.config.webPublicUrl}/user/redefine-password?step=password&u=${payload.userId.toString()}&t=${redefineToken}` - + const url = new URL(`${context.config.webPublicUrl}/user/redefine-password`) + url.searchParams.set("step", "password"), + url.searchParams.set("u", payload.userId.toString()) + url.searchParams.set("t", redefineToken) + if(payload.redirect){ + url.searchParams.set('next', payload.redirect) + } return Result.result({ url, })