Skip to content

Commit fa6b7a0

Browse files
authored
Merge pull request kookmin-sw#40 from capstone-maru/dev
release: version 0.3.0
2 parents c2c0004 + adffe33 commit fa6b7a0

File tree

13 files changed

+61
-15
lines changed

13 files changed

+61
-15
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "maru",
3-
"version": "0.2.0",
3+
"version": "0.3.0",
44
"private": true,
55
"scripts": {
66
"dev": "next dev",

src/app/lib/providers/AuthProvider.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
useAuthActions,
99
useAuthValue,
1010
} from '@/features/auth';
11-
import { load } from '@/shared/persist';
11+
import { load } from '@/shared/storage';
1212

1313
export function AuthProvider({ children }: { children: React.ReactNode }) {
1414
const auth = useAuthValue();

src/app/pages/main-page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export function MainPage() {
5757
const { data } = useQuery({
5858
queryKey: ['/api/auth/initial/info'],
5959
queryFn: getUserData,
60-
enabled: auth?.accessToken !== undefined,
60+
enabled: auth !== null,
6161
});
6262

6363
useEffect(() => {

src/app/pages/shared-posts.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export function SharedPostsPage() {
5252
const { data } = useQuery({
5353
queryKey: ['/api/auth/initial/info'],
5454
queryFn: getUserData,
55-
enabled: auth?.accessToken !== undefined,
55+
enabled: auth !== null,
5656
});
5757

5858
useEffect(() => {

src/components/NavigationBar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
useAuthIsLogin,
1313
useAuthValue,
1414
} from '@/features/auth';
15-
import { load } from '@/shared/persist';
15+
import { load } from '@/shared/storage';
1616

1717
const styles = {
1818
container: styled.nav`

src/features/auth/auth.action.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,21 @@ import { authState } from './auth.atom';
66
import { type Auth } from './auth.model';
77

88
import { type User } from '@/entities/user';
9-
import { remove, save } from '@/shared/persist';
9+
import { remove, save } from '@/shared/storage';
1010

1111
export const useAuthActions = () => {
1212
const [, setAuth] = useRecoilState(authState);
1313

1414
const login = useCallback(
15-
(auth: Auth) => {
15+
(auth: Auth & { accessToken: string }) => {
1616
axios.defaults.headers.common.Authorization = `Bearer ${auth.accessToken}`;
1717
save({ type: 'local', key: 'refreshToken', value: auth.refreshToken });
1818
save({ type: 'local', key: 'expiresIn', value: `${auth.expiresIn}` });
19-
setAuth(auth);
19+
setAuth({
20+
expiresIn: auth.expiresIn,
21+
refreshToken: auth.refreshToken,
22+
user: auth.user,
23+
});
2024
},
2125
[setAuth],
2226
);

src/features/auth/auth.atom.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@ import { atom } from 'recoil';
22

33
import { type Auth } from './auth.model';
44

5+
import { storageEffect } from '@/shared/persist';
6+
57
export const authState = atom<Auth | null>({
68
key: 'authState',
79
default: null,
10+
effects: [
11+
storageEffect({
12+
key: 'auth-state',
13+
storageType: 'session',
14+
}),
15+
],
816
});

src/features/auth/auth.model.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { type User } from '@/entities/user';
22

33
export interface Auth {
4-
accessToken: string;
54
refreshToken: string;
65
expiresIn: number;
76
user?: User;

src/shared/persist/effect.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { type AtomEffect, type DefaultValue } from 'recoil';
2+
3+
import { load, remove, save, type StorageType } from '@/shared/storage';
4+
5+
export const storageEffect =
6+
<StoredType>({
7+
key,
8+
storageType,
9+
}: {
10+
key: string;
11+
storageType: StorageType;
12+
}): AtomEffect<StoredType> =>
13+
({ setSelf, onSet }) => {
14+
if (typeof window === 'undefined') return;
15+
16+
const savedValue = load({ type: storageType, key });
17+
if (savedValue != null) {
18+
setSelf(load({ type: storageType, key }) as StoredType);
19+
}
20+
21+
onSet((newValue: StoredType | DefaultValue, _, isReset: boolean) => {
22+
if (isReset) {
23+
remove({ type: storageType, key });
24+
} else {
25+
save({ type: storageType, key, value: JSON.stringify(newValue) });
26+
}
27+
});
28+
};

0 commit comments

Comments
 (0)