erich_fromm
erich_fromm
Explore posts from servers
KKinde
Created by erich_fromm on 8/7/2024 in #💻┃support
refreshTokens when expired
I'm using Remix.js app, I've been trying to wrap my head around refreshTokens in Kinde and how to use it with remix.js. Since remix doesn't have yet any auth middleware it's necessary for us to run requireAuthSession on loaders in all protected routes. Could you please review my implementation, as I'm not sure it's correct. 1. First of all I check if user isAuthenticated, if this function returns false, then there is no point running token refresh. We just need to redirect user to login page. 2. Then we need to grab access_token. I shouldn't use getToken because this function automatically refresh tokens. I want existing cookie access_token, if it's expired I need to call refreshTokens and redirect to existing route with new headers (cookies). Getting into cookies manually seems odd to me, but it seems like using getToken isn't really feasible if it doesn't return new Headers...
export const requireAuthSession = async (request: Request) => {
const { getUser, refreshTokens, isAuthenticated } =
await getKindeSession(request);
const isAuth = await isAuthenticated(); // 1
const cookies = new Cookies(request.headers.get("Cookie"), { path: "/" }); // 2
const accessToken = cookies.get("access_token"); // 2

if (!isAuth || !accessToken) {
throw redirect(ROUTE.LOGIN);
}
try {
const ver = await jwtVerify(accessToken, JWKS);
console.log({ ver });
} catch (e) {
const headers = await refreshTokens();
if (request.method === "GET" && headers)
throw redirect(request.url, { headers });
}
return getUser();
};
export const requireAuthSession = async (request: Request) => {
const { getUser, refreshTokens, isAuthenticated } =
await getKindeSession(request);
const isAuth = await isAuthenticated(); // 1
const cookies = new Cookies(request.headers.get("Cookie"), { path: "/" }); // 2
const accessToken = cookies.get("access_token"); // 2

if (!isAuth || !accessToken) {
throw redirect(ROUTE.LOGIN);
}
try {
const ver = await jwtVerify(accessToken, JWKS);
console.log({ ver });
} catch (e) {
const headers = await refreshTokens();
if (request.method === "GET" && headers)
throw redirect(request.url, { headers });
}
return getUser();
};
10 replies
PPrisma
Created by erich_fromm on 7/2/2024 in #help-and-questions
What’s a good strategy for migration if downtime is not a problem?
I was thinking to establish something like this in my app. 1. Run npx prisma migrate dev on side branches: • Ensure migrations are always additive (expansion schema). 2. Prepare data migration scripts if needed. 3. Squash migrations before merging to main. 4. Deployment to production: • Start maintenance mode. • Apply migrations. • Run migration scripts if needed. • Verify deployment. • Apply contraction migration. • Verify deployment. • Stop maintenance mode. 5. Merge release branch back to main with contraction migration. How does it sound?
2 replies