Loading...
Loading...
Personalized links without explicit login for authentication. Apply when implementing authentication flows that don't require traditional login.
npx skill4agent add loxosceles/ai-dev link-based-auth1. Admin generates personalized link with embedded token
2. Visitor clicks link
3. CDN edge function intercepts request
4. Edge function validates token, sets auth cookie
5. Frontend loads with authentication already established
6. API requests use cookie/token for authorizationPersonalized Link (contains token)
↓
CDN Edge Function
├── Valid token → Set cookie, redirect to app
└── No/invalid token → Serve public view
↓
Static Frontend (reads cookie for auth state)
↓
API (validates token from Authorization header)// Edge function (Lambda@Edge, CloudFlare Worker, etc.)
export async function handler(event) {
const request = event.Records[0].cf.request;
const params = new URLSearchParams(request.querystring);
const token = params.get('token');
if (token) {
const isValid = await validateToken(token);
if (isValid) {
return {
status: '302',
headers: {
'location': [{ value: '/' }],
'set-cookie': [{ value: `auth=${token}; Secure; HttpOnly; SameSite=Strict` }],
},
};
}
}
// Check existing session cookie
const cookies = request.headers.cookie?.[0]?.value || '';
if (cookies.includes('auth=')) {
return request; // Authenticated, proceed
}
return request; // Unauthenticated, serve public view
}// lib/auth/auth-context.tsx
export function useAuth() {
const tokens = extractTokensFromCookies();
const environment = detectEnvironment();
const getAuthHeaders = (routeType: 'public' | 'protected') => {
if (environment === 'local') {
return { 'x-api-key': process.env.NEXT_PUBLIC_API_KEY };
}
if (routeType === 'public') {
return { 'x-api-key': process.env.NEXT_PUBLIC_API_KEY };
}
if (tokens.accessToken) {
return { Authorization: `Bearer ${tokens.accessToken}` };
}
return {};
};
return {
isAuthenticated: !!tokens.accessToken,
environment,
getAuthHeaders,
};
}// lib/profile/use-profile.ts
export function useProfile() {
const { isAuthenticated, getAuthHeaders } = useAuth();
const { data, loading } = useQuery(GET_PROFILE, {
skip: !isAuthenticated,
context: { headers: getAuthHeaders('protected') },
});
return { profile: data?.profile, isLoading: loading };
}SecureHttpOnlySameSite=Strictexport function useLocalInterceptor() {
const { environment } = useAuth();
const visitorId = useSearchParams()?.get('visitor');
const shouldIntercept = environment === 'local' && !!visitorId;
return {
shouldIntercept,
getMockData: () => ({
name: `Test Visitor ${visitorId}`,
message: 'Mock data for local development',
}),
};
}function PersonalizedContent() {
const interceptor = useLocalInterceptor();
const { data } = useRealData();
const content = interceptor.shouldIntercept
? interceptor.getMockData()
: data;
return <div>{content.message}</div>;
}