Skip to content

Connect a Frontend to Your Self-Managed Headless Project #632

@solutegrate

Description

@solutegrate

I have went through all of your documentation.
I have worked with multiple LLMs with you documentation and my code.

I know you are going to refer me to support, but please just tell me if something is wrong in how I'm doing this in AWS Lambda function.
I can get the visitor token.
I can get the member details.
But the memberTokens are returning 'unauthorized_client'.
I have checked the client multiple times and created news as well.

`const { createClient, OAuthStrategy, ApiKeyStrategy } = require('@wix/sdk');
const { members } = require('@wix/members');

async function getWixTokens(email, context) {
let wixTokens = null;
let wixMember = null;

try {
    const oauth = getWixClient({ mode: 'visitor', ...context });
    const vt = await oauth.auth.generateVisitorTokens();

    console.log('visitor tokens role:', vt?.refreshToken?.role);

    const admin = getWixClient({ mode: 'admin', ...context });
    const { items } = await admin.members.queryMembers().eq('loginEmail', email).find();

    wixMember = items[0] || (await admin.members.createMember({ member: { loginEmail: email, status: 'APPROVED', privacyStatus: 'PRIVATE' } })).member;

    console.log("✅ Wix member fetched/created for:", wixMember._id || wixMember.id, email);
    wixTokens = await oauth.auth.getMemberTokensForExternalLogin(wixMember._id || wixMember.id, context.apiKey);
    console.log("✅ Wix tokens fetched for:", email);
} catch (err) {
    console.error("❌ Failed to fetch Wix tokens:", err.message);
    if (context.fromWix && context.isLogin) throw err;
}

return { wixTokens, wixMember };

}

function getWixClient(opts = {}) {
const mode = (opts.mode || 'visitor').toLowerCase();

// pull from opts first, then context.secrets, then env (as a last resort)
const siteId = opts.siteId;

if (!siteId) {
    throw new Error('getWixClient: siteId is required (opts.siteId or context.secrets.WIX_SITE_ID)');
}

if (mode === 'admin') {
    const apiKey = opts.apiKey;
    if (!apiKey) throw new Error('getWixClient(admin): apiKey is required (opts.apiKey or context.secrets.WIX_API_KEY)');

    const accountId = opts.accountId;
    if (!accountId) throw new Error('getWixClient(admin): accountId is required (opts.accountId or context.secrets.WIX_ACCOUNT_ID)');

    return createClient({
        auth: ApiKeyStrategy({ apiKey, siteId, accountId }),
        modules: { members },
    });
}

// default: visitor (OAuthStrategy)
const clientId = opts.clientId;
if (!clientId) throw new Error('getWixClient(visitor): clientId is required (opts.clientId or context.secrets.WIX_CLIENT_ID)');
return createClient({
    auth: OAuthStrategy({ clientId }),
    modules: { members },
});

}`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions