feat(auth): redirect messenger connect to success state
This commit is contained in:
@@ -179,7 +179,7 @@ export function verifyLoginChallengeCode({ challengeToken, code }) {
|
||||
};
|
||||
}
|
||||
|
||||
export function createMessengerStartSession({ channel, email, userId }) {
|
||||
export function createMessengerStartSession({ channel, email, userId, redirectPath }) {
|
||||
purgeExpiredMessengerStartSessions();
|
||||
|
||||
const startToken = crypto.randomBytes(24).toString('base64url');
|
||||
@@ -188,6 +188,7 @@ export function createMessengerStartSession({ channel, email, userId }) {
|
||||
channel,
|
||||
email,
|
||||
userId,
|
||||
redirectPath,
|
||||
expiresAt,
|
||||
});
|
||||
|
||||
@@ -210,6 +211,7 @@ export function consumeMessengerStartSession(startToken) {
|
||||
channel: payload.channel,
|
||||
email: payload.email,
|
||||
userId: payload.userId,
|
||||
redirectPath: payload.redirectPath,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -80,6 +80,14 @@ async function resolveUserForMessenger({ userId, email }) {
|
||||
});
|
||||
}
|
||||
|
||||
function normalizeRedirectPath(value) {
|
||||
const redirectPath = String(value || '').trim();
|
||||
if (!redirectPath.startsWith('/')) {
|
||||
return '';
|
||||
}
|
||||
return redirectPath;
|
||||
}
|
||||
|
||||
async function resolveAuthenticatedUserFromRequest(req) {
|
||||
const authToken = extractAuthTokenFromRequest(req);
|
||||
const auth = verifyAccessToken(authToken);
|
||||
@@ -103,6 +111,7 @@ app.post('/auth/messenger-start', async (req, res) => {
|
||||
const providedEmail = String(req.body?.email || '').trim().toLowerCase();
|
||||
const email = authenticatedUser?.email?.trim().toLowerCase() || providedEmail;
|
||||
const userId = authenticatedUser?.id ?? null;
|
||||
const redirectPath = normalizeRedirectPath(req.body?.redirectPath);
|
||||
|
||||
if (!userId && !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
|
||||
res.status(400).json({ error: 'A valid email is required.' });
|
||||
@@ -113,6 +122,7 @@ app.post('/auth/messenger-start', async (req, res) => {
|
||||
channel,
|
||||
email,
|
||||
userId,
|
||||
redirectPath,
|
||||
});
|
||||
|
||||
res.json({
|
||||
@@ -179,7 +189,18 @@ app.post('/bot/messenger-login', async (req, res) => {
|
||||
process.env.NUXT_PUBLIC_SITE_URL ||
|
||||
'http://localhost:3000'
|
||||
).replace(/\/$/, '');
|
||||
const loginUrl = `${frontendUrl}/login?login_token=${encodeURIComponent(login.loginToken)}`;
|
||||
const nextPath = startSession.redirectPath || (
|
||||
startSession.userId
|
||||
? `/profile/notifications?status=success&connected=${channel.toLowerCase()}`
|
||||
: ''
|
||||
);
|
||||
const loginQuery = new URLSearchParams({
|
||||
login_token: login.loginToken,
|
||||
});
|
||||
if (nextPath) {
|
||||
loginQuery.set('next', nextPath);
|
||||
}
|
||||
const loginUrl = `${frontendUrl}/login?${loginQuery.toString()}`;
|
||||
|
||||
if (!skipDispatch) {
|
||||
const dispatch = await sendMessengerMessage({
|
||||
|
||||
Reference in New Issue
Block a user