feat(auth): redirect messenger connect to success state
This commit is contained in:
@@ -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