Skip to content

Commit

Permalink
fix: set expires time in sec
Browse files Browse the repository at this point in the history
  • Loading branch information
solufa committed Jun 21, 2024
1 parent 8d1a9ae commit 1d767ea
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 19 deletions.
17 changes: 7 additions & 10 deletions client/components/Auth/AuthLoader.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fetchAuthSession, signOut } from 'aws-amplify/auth';
import { fetchAuthSession, getCurrentUser, signOut } from 'aws-amplify/auth';
import { Hub } from 'aws-amplify/utils';
import { isAxiosError } from 'axios';
import { useAlert } from 'components/Alert/useAlert';
Expand All @@ -25,14 +25,10 @@ export const AuthLoader = () => {
}, [catchApiErr, setUser]);

useEffect(() => {
const controller = new AbortController();
apiClient.private.me
.$get({ config: { signal: controller.signal } })
.then(setUser)
.catch((e) => (isAxiosError(e) && e.response?.status === 401 ? setUser(null) : null));

return () => controller.abort();
}, [setUser]);
getCurrentUser()
.then(updateCookie)
.catch(() => setUser(null));
}, [setUser, updateCookie]);

useEffect(() => {
const useId = apiAxios.interceptors.response.use(undefined, async (err) => {
Expand Down Expand Up @@ -61,12 +57,13 @@ export const AuthLoader = () => {
break;
case 'signInWithRedirect_failure':
break;
case 'tokenRefresh':
break;
case 'signedOut':
await apiClient.public.session.$delete().catch(catchApiErr);
setUser(null);
break;
case 'signedIn':
case 'tokenRefresh':
await updateCookie().catch(catchApiErr);
break;
case 'tokenRefresh_failure':
Expand Down
2 changes: 1 addition & 1 deletion server/api/private/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default defineHooks(() => ({
try {
await req.jwtVerify({ onlyCookie: true });
} catch (e) {
res.status(401).send();
res.status(401).send((e as Error).message);
return;
}

Expand Down
9 changes: 6 additions & 3 deletions server/api/public/session/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,21 @@ const options: CookieSerializeOptions = {
sameSite: 'none',
};

export default defineController(() => ({
export default defineController((fastify) => ({
post: {
validators: { body: z.object({ jwt: z.string() }) },
hooks: {
preHandler: (req, reply, done) => {
assert(req.body);

const expiresIn = 60 * 60 * 24 * 5 * 1000;
const decoded = z
.object({ payload: z.object({ exp: z.number() }).passthrough() })
.passthrough()
.parse(fastify.jwt.decode(req.body.jwt));

reply.setCookie(COOKIE_NAME, req.body.jwt, {
...options,
expires: new Date(Date.now() + expiresIn),
expires: new Date(decoded.payload.exp * 1000),
});

done();
Expand Down
5 changes: 3 additions & 2 deletions server/domain/user/service/genTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Jwks } from 'api/@types/auth';
import type { EntityId } from 'api/@types/brandedId';
import type { UserEntity } from 'api/@types/user';
import { createSigner } from 'fast-jwt';
import { EXPIRES_SEC } from 'service/constants';
import { PORT } from 'service/envValues';
import type { AccessTokenJwt, IdTokenJwt } from 'service/types';
import { ulid } from 'ulid';
Expand All @@ -17,14 +18,14 @@ export const genTokens = (params: {
aud: params.userPoolClientId,
header: { kid: params.jwks.keys[0].kid, alg: params.jwks.keys[0].alg },
});
const now = Date.now();
const now = Math.floor(Date.now() / 1000);
const comomn = {
sub: params.user.id,
iss: `http://localhost:${PORT}/${params.user.userPoolId}`,
origin_jti: ulid(),
event_id: ulid(),
auth_time: now,
exp: now + 3600 * 1000,
exp: now + EXPIRES_SEC,
iat: now,
jti: ulid(),
};
Expand Down
3 changes: 2 additions & 1 deletion server/domain/user/useCase/authUseCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { genTokens } from 'domain/user/service/genTokens';
import { userPoolQuery } from 'domain/userPool/repository/userPoolQuery';
import { jwtDecode } from 'jwt-decode';
import { cognitoAssert } from 'service/cognitoAssert';
import { EXPIRES_SEC } from 'service/constants';
import { transaction } from 'service/prismaClient';
import type { AccessTokenJwt } from 'service/types';
import { genCodeDeliveryDetails } from '../service/genCodeDeliveryDetails';
Expand Down Expand Up @@ -91,7 +92,7 @@ export const authUseCase = {
jwks,
user,
}),
ExpiresIn: 3600,
ExpiresIn: EXPIRES_SEC,
TokenType: 'Bearer',
},
ChallengeParameters: {},
Expand Down
3 changes: 3 additions & 0 deletions server/service/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
export const COOKIE_NAME = 'session';

export const JWT_PROP_NAME = 'idToken';

export const EXPIRES_SEC = 3600;
5 changes: 3 additions & 2 deletions server/tests/api/public.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { COOKIE_NAME } from 'service/constants';
import { createSigner } from 'fast-jwt';
import { COOKIE_NAME, EXPIRES_SEC } from 'service/constants';
import { DEFAULT_USER_POOL_CLIENT_ID, DEFAULT_USER_POOL_ID } from 'service/envValues';
import { expect, test } from 'vitest';
import { noCookieClient } from './apiClient';
Expand Down Expand Up @@ -38,7 +39,7 @@ test(GET(noCookieClient.public.defaults), async () => {
});

test(POST(noCookieClient.public.session), async () => {
const jwt = 'dummy-jwt';
const jwt = createSigner({ key: 'dummy' })({ exp: Math.floor(Date.now() / 1000) + EXPIRES_SEC });
const res = await noCookieClient.public.session.post({ body: { jwt } });

expect(res.headers['set-cookie'][0].startsWith(`${COOKIE_NAME}=${jwt};`)).toBeTruthy();
Expand Down

0 comments on commit 1d767ea

Please sign in to comment.